Matlab-03
Matlab-03
Goal:
(1)To learn how to open MATLAB.
(2)Do a few trivalcalculation.(3)Quit MATLAB.
MATLAB commands:
(1) Plot==creat a 2-D line plot.
(2) Axes==changes the aspect the ratio of x and y axes.
(3) X label==annotates the x axis.
(4) Y label==annotates the y axis.
(5) Title==puts a title on the plot.
(6) Print==prints a hardcopy of the plot.
WHAT WE ARE GOING TO LEARN:
*How to do simple arithmetic calculation.
* How to assign values to variables.
*How to suppers screen output.
*How to quit matlab.
Creating and working arrays and numbers:
***some example :
(1) compute y=22+lnπ sinx
Ans:
>> x=30;
>> y=2^2+log(pi)*sin(x)
y = 2.8690
Ans:>> 2^5/(2^5-1)
ans = 1.0323
>> (1-1/(2^5+1))^-1
ans = 1.0313
Ans:>> 3*(sqrt(5)-1)/(sqrt(5)+1)^2-1
ans =-0.6459
Ans:>> r=(pi)^(1/3)-1;
>> A=pi*r^2
A = 0.6781
(6)sin2π/6+cos2π/6
Ans:>> (sin(pi/6))^2+(cos(pi/6))^2
ans = 1
Ans:>> x=32*pi;
>> y=(cosh(x))^2-(sinh(x))^2
y= 0
Ans:>> (1+3i)/(1-3i)
Ans:>>cos(pi/4)+i*sin(pi/4)
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 creat row and column vectors.
(2)how to creat avector of n numbers linearly spaced between two given numbers
a and b.
(3)how to do simple arithmetic operation on vector.
>>x=[ ]
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 .
***Create a column vector Ѳ with the values 0,π/4,π/2,3π/4,π and finally
5π/4,take r=2,and compute the column vectors x and y. check that x and y indeed
satisfy the equation of circle by computing the radius r=√(x2+y2).
Solution:
>>theta=[0;pi/2;pi/4;(3*pi)/2;pi;(5*pi)/2];
>> r=2;
>> x=r*cos(theta);
>> y=r*sin(theta);
>> r=sqrt(x.^2+y.^2)
>>plot(x,y)
r= 2
2
2
2
2
2
Figure:
2
1.5
0.5
-0.5
-1
-1.5
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
***Geometric series:
The sum of geometric serics 1+r+r2+r3+………..+rn
1)Approches the limit 1/1-r for r<1
2)Take r=0.5 and create another vector x=[r0,r1,r2……..rn]
3)Now the sum of the vector with command S=s(sum).
4)Calculate the limit 1/1-r and compare the S(sum).
5)Repeat the produce taking from 0 to 50 and the from 0 to 100.
Ans
2) >> n=linspace(0,10,11)
n=0 1 2 3 4 5 6 7 8 9 10
2) >> r=0.5;
>> x=(r.^n)
x = 1.0000 0.5000 0.2500 0.1250 0.0625 0.0313 0.0156
0.0078 0.0039 0.0020 0.0010
3) >> S=sum(x)
S = 1.9990
4) >> p=1/(1-r)
p=2
5) >>n=linspace(0,50,51);
>> r=0.5;
>> x=(r.^n)
x = Columns 1 through 16
1.0000 0.5000 0.2500 0.1250 0.0625 0.0313 0.0156 0.0078
0.0039 0.0020 0.0010 0.0005 0.0002 0.0001 0.0001 0.0000
Columns 17 through 32
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Columns 33 through 48
0.8
0.6
0.4
0.2
0
Y
-0.2
-0.4
-0.6
-0.8
-1 -0.5 0 0.5 1
X
Graph:
Graph of sinx is created by 3rd batch
1
0.8
0.6
0.4
0.2
y axis
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
x axis
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
Space
curve: Use the command plot 3(x,y,z) to the plots circular helix.
X(t)=sint, y(t)=cost, z(t)=t, o
Ans:
>>linspace(0,20,21);
x=sin(t);
y=cos(t);
z=t;
plot3(x,y,z)
xlabel('X')
ylabel('Y')
t=linspace(0,20,21);
x=sin(t);
y=cos(t);
z=t;plot(x,y,x,z)
>>linspace(0,20,21);20,21);
Graph:
20
15
10
-5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
****Overlay plot:
plot y=cosx and z=1-x2/2+x4/4 for 0
same plot
Solution:
>>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');
the graph of cosx and z
45
cos(x)
Graph:
40 z
35
30
25
20
Y
15
10
-5
0 0.5 1 1.5 2 2.5 3 3.5
X
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.
2)Ans:
>> p=1000;
>> i=.06;
>> n=5
>> m=2;
>> a=p*(1+i/m)^(m*n)
>>a = 1.3439e+003
3)Ans:
>> p=1000;
>> i=.06;
>> n=5
>> m=4;
>> a=p*(1+i/m)^(n*m)
>>a = 1.3469e+003
4)Ans:
>> p=1000;
>> i=.06;
>> n=5
> m=12;
>> a=p*(1+i/m)^(m*n)
a = 1.3489e+003
5)Ans:
>> p=1000;
>> i=.06;
>> n=5;
>> m=365;
>> a=p*(1+i/m)^(m*n)
a = 1.3498e+003
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 5;3 9 0]
2) B=2xlnx+siny
5i 3+2i
Ans:>>B=[2*x log(x)+sin(y);5*i 3+(2*i)]
3)A=[ ]
A =[ ]
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 matrics can be deleted by setting the row or
column to a null vector.
A(2,:)=*+→Delets the 2nd row a matrix A.
A(:,3)=*+→Delets the 3rd column of a matrix A.
A(:,3,5)=*+→Delets the 3rd through 5th column of a matrix A.
A(*1,3+,:)=*+→Delets the 1st and 3rd row of a matrix A.
A(:,*1,3+)=→Delets 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 9×4 or 3×12 matrix .
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,9,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
1 1 1 1
1 1 1 1
Similarly>> reshape (A,3,12)
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 2 4 5 7];
>>diag(v)
ans = -1 0 0 0 0
0 2 0 0 0
0 0 4 0 0
0 0 0 5 0
0 0 0 0 7
** tril(A)→extract the lower triangular part matrix A of a.
**triu(A)→extract the upper triangular part matrix A of a.
***MATRIX FUNCTION :
(4)>>G(13)
ans = 0.9572
(5)>>G(12,1)=1
G = 0.8147 0.1576 0.6557 0.7060 0.4387 0.2760 0.7513 0.8407 0.3517 0.0759
0.9058 0.9706 0.0357 0.0318 0.3816 0.6797 0.2551 0.2543 0.8308 0.0540
0.1270 0.9572 0.8491 0.2769 0.7655 0.6551 0.5060 0.8143 0.5853 0.5308
0.9134 0.4854 0.9340 0.0462 0.7952 0.1626 0.6991 0.2435 0.5497 0.7792
0.6324 0.8003 0.6787 0.0971 4.0000 0.1190 0.8909 0.9293 0.9172 0.9340
0.0975 0.1419 0.7577 0.8235 0.4898 0.4984 0.9593 0.3500 0.2858 0.1299
0.2785 0.4218 0.7431 0.6948 0.4456 0.9597 0.5472 0.1966 0.7572 0.5688
0.5469 0.9157 0.3922 0.3171 0.6463 0.3404 0.1386 0.2511 0.7537 0.4694
0.9575 0.7922 0.6555 0.9502 0.7094 0.5853 0.1493 0.6160 0.3804 0.0119
0.9649 0.9595 0.1712 0.0344 0.7547 0.2238 0.2575 0.4733 0.5678 0.3371
0 0 0 0 0 0 0 0 0 0
1.0000 0 0 0 0 0 0 0 0 0
Soln:
>>b=[10;20;9];
>>X=[x;y;z];
>> X=inv(A)*b
X=
3.4442
3.1982
1.1868
***By using left division :
>>X=A\b
X =3.4442
3.1982
1.1868
By using gauss Jordan elimination:
>> c=[A b];
>> g=rref(c)
g =1.0000 0 0 3.4442
0 1.0000 0 3.1982
0 0 1.0000 1.1868
By using creamers rule:
>>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 =3.4442
A(1:3,2)=b
A = 5 10 2
-3 20 4
2 9 -9
>>A2=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>y=det(A2)/det(A)
y = 3.1982
>>A(1:3,3)=b
A = 5 -3 10
-3 8 20
2 4 9
>>A3=A;
>>A=[5 -3 2;-3 8 4;2 4 -9 ];
>>z=det(A3)/det(A)
z = 1.1868
2-D GRAPHICS
Plot(x,y)-Plots y vs x with a solid line.
Plot(x,y,’--’)-Plots y vs x with a dashed line.
Plot(x)-Plots the elements of x against their row index.
***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.
***The style option is made up of either the color option, the line style
option or a combination of the two.
Examples:
1)Plot(x,y,’r’)-plot y vs x with a red solid line.
2)Plot(x,y,’:’)-Plot y vs x with a doted line.
3)Plot(x,y,’b--’)-Plot y vs x with a blue-dashed line.
4)Plot(x,y,’+’)-Plot y vs x unconnected points marked by +.
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
Figure:line command
Graph:
graph of fplot
30
20
10
0
Y
-10
-20
-30
0 5 10 15 20 25 30
X
4
Y
0
-3 -2 -1 0
10 10 10 10
X
150 30
0.5
180 0
Y
210 330
240 300
270
0.5
Y axis
-0.5
-1
-1.5
-1.5 -1 -0.5 0 0.5 1 1.5
X axis
Axis control:
1) Axis([-5 10 2 2])-sets the x-axis from-5 to 10 as y-axis from 2 to2.
2) Axis([-5 10 –infinf])-sets the x-axis limit at -5to 10 and the y-axis limit
set automatically.
***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:
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
Solution:
>> t=linspace(0,2*pi,200);
>> y1=sin(t);
>> y2=t;
>> y3=t-(t.^3)/6+(t.^5)/120;
>>plot(t,y1)
>>plot(t,y1,t,y2,'r',t,y3,'k')
>>axis([0 5 -1 5])
>>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
linear approximation
0 sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
4
approximation of sin(t)
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t
>> t=linspace(0,2*pi,200);
>> r=sqrt(abs(2*sin(5*t)));
>> x=r.*cos(t);
>> y=r.*sin(t);
>>bar(t,y)
>>axis([0 pi 0 inf])
>>xlabel('X axis');
>>ylabel('Y axis');
>>title('graph of bar command')
Graph:
graph of bar command
1.4
1.2
0.8
Y axis
0.6
0.4
0.2
0
0 0.5 1 1.5 2 2.5 3
X axis
Solution:
>>cont=char('Asia','Europe','Africa','N.America','S.America');
>>pop=[3332;696;694;434;307];
>>barh(pop)
>>for i=1:5;
>>gtext(cont(i,:));
End
>>xlabel('population');
>>ylabel('Asia,Europe,Africa,N.America,S.America');
>>title('graph of bar command')
Graph:
graph of bar command
5
Asia,Europe,Africa,N.America,S.America
Solution:
>>cont=char('ASia','Europe','Africa','N.America','S.America');
>>pop=[3332;696;694;437;307];
>>pie(pop)
>>for i=1:5,
>>gtext(cont(i,:));
end
>>xlabel('Poplution');
>>ylabel('country');
>>title('graph of world')
Graph:
6%
8%
Europe
Africa
13%
N.America
ASia
S.America
61%
13%
Solution:
>> t=linspace(0,1,100);
>> x=t;
>> y=t.^2;
>> z=t.^3;
>> plot3(x,y,z),grid
>>xlabel('x(t)=t')
>>ylabel('y(t)=t2')
>>zlabel('y(t)=t3')
>>xlabel('axis X');
>>ylabel('axis Y');
>>zlabel('axis Z');
>>title('graph of parametric space curve')
Graph:
graph of parametric space curve
0.8
0.6
axis Z
0.4
0.2
0
1
1
0.8
0.5 0.6
0.4
0.2
axis Y 0 0
axis X
Solution:
>> u=-5:.2:5;
>> [X,Y]=meshgrid(u,u);
>> Z=cos(X).*cos(Y).*exp(-sqrt(X.^2+Y.^2)/4);
>>surf(X,Y,Z)
>>xlabel('asxis X');
>>ylabel('axis Y');
>>zlabel('axis Z');
>>title('graph of surf command')
Graph:
graph of surf command
0.5
axis Z
-0.5
5
5
0
0
axis Y -5 -5
asxis X
0.5
axis Z
-0.5
60
60
40
40
20
20
axis Y 0 0
axis X
Solution:
>> u=-5:.2:5;
>> u=-5:.2:5;
>> [X,Y]=meshgrid(u,u);
>> Z=cos(X).*cos(Y).*exp(-sqrt(X.^2+Y.^2)/4);
>>surfl(Z)
>>xlabel('axix X');
>>ylabel('axis Y');
>>zlabel('axis Z');
>>title('graph of surfl command')
Graph:
graph of surfl command
0.5
axis Z
-0.5
60
60
40
40
20
20
axis Y 0 0
axix X
Solution:
>> x=linspace(-3,3,50);
>> y=x;
>> [x,y]=meshgrid(x,y);
>> z=-5./(1+(x.^2)+(y.^2));
>>mesh(z)
>>xlabel('axis X');
>>ylabel('axis Y');
>>zlabel('axis z');
>>title('graph of mash command')
Graph:
graph of mash command
-1
-2
axis z
-3
-4
-5
60
50
40
40
30
20 20
10
axis Y 0 0
axis X
1
axis Z
-1
-2
3
2
4
1
2
0
-1 0
-2 -2
axis Y -3 -4 axis X
Solution:
>> x=linspace(-3,3,50);
>> y=x;
>> [x,y]=meshgrid(x,y);
>> z=-5./(1+x.^2+y.^2);
>>waterfall(z)
>> hidden off
>>xlabel('axis X');
>>ylabel('axis Y');
>>zlabel('axis z');
>>title('graph of waterfall command')
>>
Graph:
graph of waterfall command
-1
-2
axis z
-3
-4
-5
60
50
40
40
30
20 20
10
axis Y 0 0
axis X
CALCULAS
***limx+cosx/x
x→α
Solution:
>>syms x
>> f=(x+cos(x))/x;
>> LHL=limit(f,x,inf,'left')
LHL =1
>> RHL=limit(f,x,inf,'right')
RHL =1
***lim x2+sin5x/7x
x→-1
Solution:
>>syms x
>> f=(x^2+sin(5*x))/7*x;
>> LHL=limit(f,x,-1,'left')
LHL =sin(5)/7 - 1/7
>> RHL=limit(f,x,-1,'right')
RHL =sin(5)/7 - 1/7
***lim√(1+x)-1/x
x→0
solution:
>>syms x
>> f=(sqrt(1+x)-1)/x;
>> LHL=limit(f,x,0,'left')
LHL =1/2
>> RHL=limit(f,x,0,'right')
RHL =1/2
***lim 1/5+e(1/x-2)
x→2
Slution:
>>syms x
>> f=1/(5+exp(1/x-2));
>> LHL=limit(f,x,2,'left')
LHL =1/(1/exp(3/2) + 5)
>> RHL=limit(f,x,2,'right')
RHL =1/(1/exp(3/2) + 5)
***Piece wise function: A function f(x) is defined as follows
f(x)={
Solution:
>>syms x
>> LHL=limit(x^2,x,1,'left')
LHL =1
>> RHL=limit(x^2+2,x,1,'right')
RHL =3
LHL and RHL are not equal .so the limit does not exist.
F(x)={
***Deravitave of constant:
**sin5t
Solution:
>>syms s t
>> f=sin(s*t)
>>diff(f,t)
ans =s*cos(s*t)
**sin(ax+b)
Solution:
>>syms a x b
>> f=sin((a*x)+b)
f =sin(b + a*x)
>>diff(f)
ans =a*cos(b + a*x)
INTEGRATION
Normal command MATLAB command
n
∫ dx Int(x^n) or int(x^n,x)
∫ ( ) dx Int(f,a,b) or int(f,x,a,b)
∫ ( ) dx Int(f) or int(f,x)
DIFFERENTIAL EQUATION
Normal command MATLAB command
D
D2
Dy
D2 y
-6
-7
-8
-9
-10
-11
-12
0 1 2 3 4 5 6 7 8 9 10
(4)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
(2)d2y/dx2+4y=e-2x,y(0)=0,y’(π)=0
>>syms x y
>>dsolve('D2y+4*y=exp(-2*x)','y(0)=0','Dy(pi)=0','x')
ans =sin(2*x)/(8*exp(2*pi)) - cos(2*x)/8 - (sin(2*x)*(cos(2*x) -
sin(2*x)))/(8*exp(2*x)) + (cos(2*x)*(cos(2*x) + sin(2*x)))/(8*exp(2*x))
-6
-7
-8
-9
-10
-11
-12
0 1 2 3 4 5 6 7 8 9 10
(4)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