Matlab 04
Matlab 04
MATLAB:
MATLAB is a software package for high performance numerical computation and
visualization .it provides an interactive environment with hundreds of build in
function for technical computation,graphics and animation.
Best of all it also provides easy extensibilitywith its own high level programming
language. The name MATLAB stands for MATrixLABoratory.
Application in Mathematics:
MATLABs build in function provides excellent tolls for linear algebra
(computations,data analysis,optimization,numerical solution of ordinary
differential equations (ode’s),quadratic and many other type of scientific
computation.
There are numerous for 2-D and 3-D graphics as well as for animation. Also for
those who can not do with their fortran or c codes.
(9)Find the value of eiπ/4 ,(with & without Euler’s law and compare with them).
(without Euler’s law) Ans:
>>exp(i*pi/4))
Ans= 0.7071 + 0.7071i
(With Euler’s law) Ans:
>>cos(pi/4)+i*sin(pi/4)
ans = 0.7071 + 0.7071i
Here both results are same .
ARRAY:
An array is a list of numbers or expression arranged in horizontal row and vertical
columns.
*When an array has only one row or column then it is called vector.
*An array with M row and N columns is called a matrix of size (M*N).
What we are going to learn:
(1) How to create row and column vectors.
(2) How to create avector of n numbers linearly spaced between two given
numbers a and b.
(3) How to do simple arithmetic operation on vector.
Ans: x=[ ]
>>x=linspace(0,10,5);
>>z=sin(x).*sqrt(x)
Ans :
z = 0 0.9463 -2.1442 2.5688 -1.7203
Here Both results are same .
##.If m=0.5, c=-2, compute y=mx+c at the following x co-ordinates
X=0,1.5,3,4,5,7,8,10
Solution:-
>> m=.5;
>> c=-2;
>> x=[0,1.5,3,4,5,7,9,10];
>> y=m*x+c
y = -2.0000 -1.2500 -0.5000 0 0.5000 1.5000 2.5000 3.0000
>> z=t-x
z =-14.0000 -14.1111 -14.2222 -14.3333 -14.4444 -14.5556 -14.6667 -14.7778
-14.8889 -15.0000
Points on a circle:
All points with coordinates x=rcosѲ and y=rsinѲwhere r is a
constant lie on a circle with radius r,that is the satisfied equation
x2+y2=r2 .
## Draw the circle of unit radious by using the parametric equation of
unit circle:
x=cosθ ,y=sinθ , 0≤ θ ≤ 2π .
Solution:
>>theta=linspace(0,2*pi,100);
>>x=cos(theta);
>>y=sin(theta);
>>plot(x,y)
>>title('Circle of unit radious');
>>title('Circle of unit radious');
>>axis('equal');
>>xlabel('X-Axis');
>>ylabel('Y-Axis');
Figure:
## Line style:
Make the same plots as above but rather than displayeding the graphs
as a curve , show the unconnected data points, to display the data
points with small circles ,use plots (x,y,’o’).Now combine the two plots
with the command plot (x,y,x,y.’o’).to show the line through the data
points as well as distinct data points.
Example: As exponentially decaying sin plot;
0.5
0.4
0.3
0.2
Y
0.1
-0.1
-0.2
0 2 4 6 8 10 12 14
X
## Plot y1= Sin(t) ,y2 = Cos(t) and y3= Tan(t) in the same diagram :
Solution :
>>t=0:15:90;
>>y1=sin(t);
>>y2=cos(t);
>>y3=tan(t);
>>plot(t,y1,'o',t,y2,'--',t,y3,'*')
>>axis('equal');
>>xlabel('X-Axis');
>>ylabel('Y-Axis');
>>text(1.0,0,'cos(t)');
>>gtext('Sin(t)');
>>gtext('Cos(t)');
>>gtext('Tan(t)');
>>legend('sin(t)','cos(t)','tan(t)');
Figure:
## Overlay plot:
## Plot y1=Sin(t) , y2 = Cos(t),y3 =Sin(t)+Cos(t) in a same diagram :
Solution:
>>t=linspace(0,2*pi,100);
>>y1=sin(t);
>>y2=cos(t);
>>y3=sin(t)+cos(t);
>>plot(t,y1,'o',t,y2,'--',t,y3,'*')
>>title('Graph of the trigonometric function Sin(t) , Cos(t) and >>Sin(t)+Cos(t)');
>>axis('equal');
>>xlabel('x-axis');
>>ylabel('y-axis');
>>text(4.0,0,'sin(t)');
>>gtext('sin-curve');
>>gtext('cos-curve');
>>gtext('multi curve');
>>legend('sin(t)','cos(t)','multi graph')
Figure:
>>x=linspace(0,pi,100 )
>>y=cos(x);
>>z=1-x.^2/2+x.^4/2;
>>plot(x,y,x,z);
>>plot(x,y,x,z,'*');
>>xlabel('X');
>>ylabel('Y');
>>title('the graph of cosx and z');
>>legend('cos(x),'z');
Figure:
## Variable in workspace:
(1) Type “who” see the variables in your workspace.
(2) Type ‘’whos’’ to get more information
(3) Type *theta, x, y+ to see the value of Ѳ, x, y listed as these
commands .
(4) To transpose them type a single right quote (‘) after their name.
## Write a MATLAB command to convert the temperature from
Celcius to Fahrenhite .
Sol: >> c=8;
>> F=9*c/5+32
F =46.4000
MATRIX:
A matrix is entered row wise with consecutive elements of a row,
separated by a space or a command and the columns are separated by
semicolons . The entire matrix must be enclosed within square
brackets.
## Write a MATLAB input command for the following matrices.
1)A=[ ]
Ans:>>A=[1 2 3;4 5 6]
2) B=lnyCosy
5i+lnxSinx
Ans:>>B=[log(y)cos(y);5*i+log(x) sin(x)]
3)A=[ ]
## Indexing(or subscripting):
A(I,j)=refers to the element aij of the matrix.
A(m:n,k:l)=specifies rows m to n and columns k to l of a matrix A.
A(:,5:20)= Refers to the elements in columns 5 through 20 of all rows of
A.
A(2:10,:)= Refers to the elements in row 2 through 10 of all columns of
matrix A.
## Dimension:size(A) →gives the dimension of A [m,n]
B=[ ]
B(2,3)=5
B=[ ]
C(3,1;3)=[1 2 3]
Produces C=[ ]
## sample question:
# Use (A (3,1:3) such as command) to change the row and column.
2)ans;>> y=[A v]
>>y =[ ]
3)>> u'
ans = [ ]
z =[ ]
4) Ans: A=[A u]
??? Error using ==>horzcat
CAT arguments dimensions are not consistent.
It is not possible in MATLAB, because the dimensions of the matrix are
not acceptable for this operation.
5)Ans:
>>T=[];
>>S=[1 6 9];
>>M=[T;S]
M=[1 6 9 ]
##Deleting a row or column :
Any row or columns of a matrix can be deleted by setting the row or
column to a null vector.
A(2,:)=*+→Delete the 2nd row a matrix A.
A(:,3)=*+→Delete the 3rd column of a matrix A.
A(:,3,5)=*+→Delete the 3rd through 5th column of a matrix A.
A(*1,3+,:)=*+→Delete the 1st and 3rd row of a matrix A.
A(:,*1,3+)=→Delete the 1st and 3rd column of a matrix A.
Sample example:
>>Q=[1 2 3 4 5;5 4 3 2 1;4 5 6 7 8;3 4 5 6 6;1 3 5 8 9]
Q=1 2 3 4 5
5 4 3 2 1
4 5 6 7 8
3 4 5 6 6
1 3 5 8 9
>>Q(v,:)
ans = 1 2 3 4 5
3 4 5 6 6
1 3 5 8 9
>>Q(:,v)
ans = 1 4 5
5 2 1
4 7 8
3 6 6
1 8 9
## Reshaping matrix(as a vector):
Write a matlab command s.t all the element of a matrix A can be strung
into single column vector b.
Ex:>> A=ones(2)
A=1 1
1 1
>> b=A(:)
b=1
1
1
1
## As a differently sized matrix:
If matrix A is an m×n matrix it can be reshaped into p×q matrix as long
as m×n=p×q ,with the command reshape (a,p,q).
## Example: Transform a 6×6 matrix A into a 7×4 or 5×10matrix .
Ans:>> A=ones(6)
A=1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
>>reshape(A,7,4)
Ans = 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
Similarly>> reshape (A,5,10)
Transpose:
1)B=A’ produces B=AT if A is a real matrix
2)B=A’ produces B= AT if A is a real complex matrix.
## Utility matrix :eye(m,n)→returns an m×n matrix with 1st on the main
diagonal.
zeros(m,n)→returns an m×n matrix of zeros.
ones(m,n)→returns an m×n matrix of ones.
rand(m,n)→returns an m×n matrix of random numbers.
diag(V)→Generates a diagonal matrix with vector v on the diagonal.
Example:
v=[1 7 3 2 5];
>>diag(v)
ans = 1 0 0 0 0
0 7 0 0 0
0 0 3 0 0
0 0 0 2 0
0 0 0 0 5
## tril(A)→extract the lower triangular part matrix A of a.
##triu(A)→extract the upper triangular part matrix A of a.
## Matrix and array operation :
A+B → is valid if A and B are of same size.
A-B → is valid if A and B are of same size.
A*B → is valid if A number of column equal B number of rows.
A/B → is valid if (and equal A.B-1) for the same size square matrix A and B.
A^2 →makes sense only if A is a square matrix.
A+α(α sclar) →adds αto each element of A.
A*α(α*A) →multiplies of each element of A by α.
Array operation :If u , v are two vectors then,
u.*v → produces element by element multiplication.
u./v → produces element by element division.
u.^v→ produces element by element exponentiation.
## MATRIX FUNCTION :
>>X=A\b
X = 1.6902
1.5695
0.6287
By using gauss Jordan elimination:
>>A(1:3,1)=b
A = 10 -3 2
20 8 4
9 4 -9
>>A1=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>x=det(A1)/det(A)
x = 1.6902
A(1:3,2)=b
A= 5 5 2
-3 10 4
2 4 -9
>>A2=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>y=det(A2)/det(A)
y = 1.5695
>>A(1:3,3)=b
A = 5 -3 5
-3 8 10
2 4 4
>>A3=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>z=det(A3)/det(A)
z =0.6287
## If a=(1 2 ;4 -3) and f(x)=2x3-4x+5 then find f(A).
Solution:
>> A=[1 2;4 -3];
>> g=2*A^3-4*A+5*eye(2)
g = -13 52
104 -117
## write down the compound to find the eigenvalues and eigenvectors of A.also
verify the Cayley Hamilton theorem:
Here A = 41 1
25 -2
1 1 2
Solution:
>>A=[4,1,1;2,5,-2;1,1,2]
>>[V,D]=eig(A)
>>P=poly(A)
2-D GRAPHICS
##Style line: The style option in the plot command is a character string
that consists of 1,2
or character specify the color and the line style. There are several color,
line and marker
style option.
Solution:
>> t=linspace(0,2*pi,100);
>> y1=sin(t);
>> y2=t;
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y1)
>>line(t,y2,'linestyle','--')
>>line(t,y3,'marker','o')
>>axis([0 5 -1 5])
>>xlabel('t')
>>ylabel('approximations of sin(t)')
>>title('graph of sin(t)')
Graph:
fun with sin(t)
5
4
approximations of sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
## OVERLAY PLOT :
The command to generate overlay plots:
Plot y1=sint,y2=t and y3=t-t3/3!+t5/5!
Solution:
>> t=linspace(0,2*pi,100);
>> y1=sin(y);
>> y1=sin(t);
>> y2=t;
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y1,t,y2,'--',t,y3,'o')
>>axis([0 5 -2 5])
>>xlabel('t')
>>ylabel('approximation of sin(t)')
>>title('fun with sin(t)')
>>text(3.5,0,'sin(t)')
>>gtext('linear approximation')
>>gtext('first 3 terms')
>>gtext('in taylor series')
Graph:
fun with sin(t)
5
3
approximation of sin(t)
0 sin(t)
-1
-2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
4
approximation of sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
## DIFFERENTIAL EQUATION :
Dy
D2 y
Graph:
-5
-6
-7
-8
-9
-10
-11
-12
0 1 2 3 4 5 6 7 8 9 10
(3)Solve :
Solution:
>>syms x
>>dsolve('Dy=asin(2*x)/(1+(x^2))','y(0)=1','x')
ans =int(asin(2*y)/(y^2 + 1), y = 0..x) + 1
THE END