Saturday, June 11, 2011

3 D Plots in Matlab

3 D Plots in Matlab

To create 3D plots , first of all we need to create 3 variables. x,y and z.

Create the x and y  arrays:-
>> x=-3:0.25:3;
>> y=-3:0.25:3;

Create the meshgrid:-

>> [X Y]=meshgrid(x,y);


Define:-
Z=sqrt(X.^2 + Y.^2);

mesh(X,Y,Z);





>>surf(X,Y,Z);



>>surf(X,Y,-Z);





>>waterfall(X,Y,Z):-
 >>contour(x,y,z):
 >>>> surfc(X,Y,Z):-
 Now we arrive to the view part:
View is used to give the viewing angle of the plot. the syntax of the command is :-
view(azimuth,elevation);
where  the azimuth is an angle measured in degrees  in the x-y plane and is measured relative to the negative y axis and positive as always in the anti clockwise direction.

The elevation is the angle measured from the x-y plane in the direction of the z - axis.
default values are azimuth=-37.5 and elevation =35.
Here are the results of applying different views to the last plot:-

>> view(-65,30);

 >> view(-37.5,40);

Next we shall move to interpolation using Matlab.

Matrices in Matlab

Matrices in Matlab

Matrix manipulation is probably the most important part of Matlab. It is what most people will use it for. In this post we shall learn how to create matrices, perform basic manipulations, and to solve simultaneous equations using Matlab.

Creating a Matrix:- 

>> a=[1,4,-1;1,1,-6;3,-1,-1];
a

a =

     1     4    -1
     1     1    -6
     3    -1    -1

>>

Transposing the matrix :-

>> a'

ans =

     1     1     3
     4     1    -1
    -1    -6    -1

>>

Inverse of a Matrix:-

>> inv(a)

ans =

    0.0986   -0.0704    0.3239
    0.2394   -0.0282   -0.0704
    0.0563   -0.1831    0.0423

>>

>> a*inv(a)

ans =

    1.0000   -0.0000         0
   -0.0000    1.0000         0
    0.0000    0.0000    1.0000

>> The identity matrix as you would expect.

Now let's try and solve the given equation using Matlab:-

1) x + y + z =9
2)2x - 3y + 4z =13
3)3x + 4y + 5z=40

Create two matrices a & b:-

>> a=[1,1,1;2,-3,4;3,4,5]

a =

     1     1     1
     2    -3     4
     3     4     5

>> b=[9;13;40]

b =

     9
    13
    40

>>
This equation can be represented as 
AX=B
=>inv(A) * A * X= inv(A) * B multiplying both sides by inv(A)
=> X=inv(A)* B 

>> inv(a)*b

ans =

    1.0000
    3.0000
    5.0000

>>
which means 
x=1
y=3
z=5

Now, let's try it the other way round.

The same equation can be represented as X'*A'=B' where ' represents the Transpose.
multiplying both sides by inv(A')
=>X'*A'*inv(A')=B'*inv(A')
=>X'=B'*inv(A')


>> b'*inv(a')

ans =

    1.0000    3.0000    5.0000

which means 
x=1
y=3
z=5