A Concise Introduction To MATLAB
A Concise Introduction To MATLAB
Palm III
>> x=0:0.01:35;
>> y=0.4.*sqrt(1.8*x);
>>plot(x,y),xlabel('x'),ylabel('y'),title('y=0.4sqrt(1.8x)'),
axis([0 35 0 35]),grid
>> x=0:0.01:35;
>> y=0.4.*sqrt(1.8*x);
>> plot(x,y),xlabel('x'),ylabel('y'),title('Function y'),grid
>> f=@(x)(tan(cos(x))-sin(tan(x)));
>> fplot(f,[0 2*pi])
>> x=0.2+0.8i;
>> n=0:0.001:20;
>> plot(x.^n),xlabel('Real'),ylabel('Imaginary'),title('Complex
Plot'),axis([0 20 0 x.^n])
5.2 Additional Commands and Plot types – Subplots
>> cd F:\MATLAB_PRA
>> edit
x=[0:0.01:5];
y=exp(-1.2*x).*sin(10*x+5);
subplot(1,2,1),plot(x,y),xlabel('x'),ylabel('y'),axis([0 5 -1 1])
x=[-6:0.01:6];
y=abs(x.^3-100);
subplot(1,2,2),plot(x,y),xlabel('x'),ylabel('y'),axis([-6 6 0 350])
Ctrl+Ssubplt1
To execute
>> splt1
T5.2-1
>> cd F:\MATLAB_PRA
>> edit
t=0:0.001:8;
v=-8:0.001:8;
z=exp(-0.5*t).*cos(20*t-6);
u=6.*log10(v.^2+20);
subplot(2,1,1),plot(t,z),xlabel('x'),ylabel('z'),title('Graph1'),grid
subplot(2,1,2),plot(v,u),xlabel('x'),ylabel('u'),title('Graph2'),grid
Ctrl+Ssubplt2
To execute
>> suplt2
Labeling Curves and Data
>> cd F:\MATLAB_PRA
>> x=[0:0.01:2];
>> y=sinh(x);
>> z=tanh(x);
>> plot(x,y,x,z,'--'),xlabel('x'),ylabel('sinh and
tanh'),legend('sinh(x)','tanh(x)')
The hold command
>> x=[-1:0.01:1];
>> n=[0:0.01:10];
>> y1=3+exp(-x).*sin(6*x);
>> y2=4+exp(-x).*cos(6*x);z=0.1+0.9i;
>>
plot(y1,y2),xlabel('x'),ylabel('y'),hold,plot(z.^n),title('Two
plots'),gtext('y2,y1 plot'),gtext('Complex plots')
Current plot held
T5.2-2
>> x=[0,1,2,3,4,5];
>> y1=[11,13,8,7,5,9];
>> y2=[2,4,5,3,2,4];
>> plot(x,y1,'-o',x,y2,'--
d'),xlabel('x'),ylabel('y1&Y2'),title('Two
curves'),legend('y1','y2'),grid
T5.2-3
>> x=0:0.001:2;
>> y1=cosh(x);
>> y2=0.5*exp(x);
>> plot(x,y1,x,y2,'--'),xlabel('x'),ylabel('y1&y2'),title('Two
Curves'),legend('cosh(x)','0.5.e^{x}'),grid
T5.2-4
>> x=0:0.001:2;
>> y1=sinh(x);
>> y2=0.5*exp(-x);
>>
plot(x,y1,'r',x,y2,'k'),ylabel('x'),ylabel('y1&y2'),title('Two
Curves'),title('0.5.e^{x}'),text(1,8,'exp
curve'),gtext('sinhx'),grid
T5.2-5
>> x=0:0.001:1;
>> y1=sin(x);
>> y2=x-(x.^3./3);
>>
plot(x,y1),xlabel('x'),ylabel('y1,y2'),hold,plot(x,y2,'k'),gtext
('y1'),gtext('y2'),title('Holding Graphs'),grid
Current plot held
Log log plot of the function
>> x=logspace(-1,2,500);
>> u=x.^2;
>> num=100*(1-0.01*u).^2+0.02*u;
>> den=(1-u).^2+0.1*u;
>> y=sqrt(num./den);
>> loglog(x,y),xlabel('x'),ylabel('y')
Script file:Hermite
Contents
Define the range
Hermite family
Plots
Define the range
t=-5:0.1:5;
Hermite family
H0=exp(-t.^2./4);
H1=t.*exp(-t.^2./4);
H2=(t.^2-1).*exp(-t.^2./4);
H3=(t.^4-6*t.^2+3).*exp(-t.^2./4);
Plots
plot(t,H0,'*:',t,H1,'d-.',t,H2,'h--',t,H3,'s-')
xlabel('t'),ylabel('Amplitude')
Contents
Define time range
Laguerre polynomials
Plots
Define time range
t=0:0.1:15;
Laguerre polynomials
L0=exp(-t./2);
L1=(1-t).*t.*exp(-t./2);
L2=(1-2.*t+0.5.*t.^2).*exp(-t./2);
L3=(t.^3-3.*t).*exp(-t.^2/.4);
Plots
plot(t,L0,'*:',t,L1,'d-.',t,L2,'s-',t,L3,'p:')
xlabel('t'),ylabel('Amplitude')
p
r
1 cos
>> p=2;
>> e=0.5;
>> theta=[0:pi/90:2*pi];
>> r=p./(1-e.*cos(theta));
>> polar(theta,r),title('Orbital Eccentricity=0.5')
>> x=0:0.001:1.5;
>> y1=2.*x.^(-0.5);
>> y2=10.^(1-x);
>>
subplot(2,2,1),plot(x,y1,x,y2),xlabel('x'),ylabel('y'),gtext('Ex
ponential'),gtext('power'),subplot(2,2,2),semilogy(x,y1,x,y2),xl
abel('x'),ylabel('y'),gtext('Exponential'),gtext('power'),subplo
t(2,2,3),loglog(x,y1,x,y2),xlabel('x'),ylabel('y'),gtext('Expone
ntial'),gtext('power')
>> theta=[0:pi/90:4*pi];
>> a=2;
>> r=a.*theta;
>> polar(theta,r),title('Spiral of Archimedes')
>> contour(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')
>> meshc(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')
>> Z=X.*exp(-(X.^2+Y.^2));
>>
subplot(2,2,1),mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s
ubplot(2,2,2),meshc(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s
ubplot(2,2,3),meshz(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s
ubplot(2,2,4),waterfall(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z
')
Contents
Script file:sincn.m
Plots
Script file:sincn.m
% Define time range
t=-5*pi:0.25:5*pi;
% sinc functions
s0=sin(t)./(pi.*t);
s1=sin(t-pi)./(pi*(t-pi));
s2=sin(t-2*pi)./(pi*(t-2*pi));
s3=sin(t-3*pi)./(pi*(t-3*pi));
Plots
plot(t,s0,'*:',t,s1,'d-.',t,s2,'h--',t,s3,'s-')
xlabel('t'),ylabel('Amplitude')
title('Members of sinc family')
legend('sinc 0','sinc 1','sinc 2','sinc 3')
>> image(magic(10)),title('Image pattern of Magic Square')
----------------------------------------------------------------
Numerical Methods in Engineering and Science with
Programs in C, C++ and MATLAB
>> x='hat'
x =
'hat'
>> x=[3*4,pi/2]
x =
12.0000 1.5708
>> y=3*sin(x)
y =
-1.6097 3.0000
>> a=[1:2:10]
a =
1 3 5 7 9
>> a=[1:5]
a =
1 2 3 4 5
>> A(3,3)
ans =
7
>> A(2:4,3:4)
ans =
4 5
7 8
2 6
>> A=eye(3)
A =
1 0 0
0 1 0
0 0 1
>> B=zeros(2,3)
B =
0 0 0
0 0 0
>> C=ones(3,4)
C =
1 1 1 1
1 1 1 1
1 1 1 1
>> size(C)
ans =
3 4
>> B=A'
B =
1 3
2 4
3 5
>> A=[2 5 3;3 4 6];
>> B=[3 4 8;3 5 6];
>> C=A.*B
C =
6 20 24
9 20 36
>> C=A+B
C =
3 11 14
4 4 12
>> D=A*B'
D =
78 56
55 42
>> A=[2 6 4;3 7 5];
>> B=[8 4 3;2 9 7];
>> A>B
ans =
2×3 logical array
0 1 1
1 0 0
>> x=0:pi/100:2*pi;
>> y=sin(x);
>> plot(x,y)