0% found this document useful (0 votes)
120 views

A Concise Introduction To MATLAB

Solved exercises from the book A Concise Introduction to MATLAB by William J palm Jr

Uploaded by

JAYAPREETHI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

A Concise Introduction To MATLAB

Solved exercises from the book A Concise Introduction to MATLAB by William J palm Jr

Uploaded by

JAYAPREETHI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

A Concise Introduction to MATLAB – William J.

Palm III

Chapter 5 Advanced Plotting and Model Building


5.1 xy Plotting Functions
Plots of complex Numbers
>> z=0.1+0.9i;
>> n=[0:0.01:10];
>> plot(z.^n),xlabel('Real'),ylabel('Imaginary')

The Function Plot Command fplot

>> f=@(x) (cos(tan(x))-tan(sin(x)));


>> fplot(f,[1 2])
Plotting Polynomials
>> x=[-6:0.01:6];
>> p=[3,2,-100,2,-7,90];
>> plot(x,polyval(p,x)),xlabel('x'),ylabel('p')
T5.1-1 Plot the equation y  0.4 1.8 x for 0  x  35 and 0  y  3.5

>> 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

T 5.1-2 use fplot command to plot the function


tan(cos( x))  sin(tan( x)) for 0  x  2

>> f=@(x)(tan(cos(x))-sin(tan(x)));
>> fplot(f,[0 2*pi])

T5.1-3 Plot (0.2  0.8i)n for 0  n  20

>> 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+Ssubplt1

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+Ssubplt2

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

100(1  0.01x 2  0.02 x 2


y 0.1  x  100
(1  x 2 )2  0.1x 2

>> 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')

title('First four members of the Hermite family')

legend('Her 0','Her 1','Her 2','Her 3')


Script File:lagu.m

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')

title('Members of Laguerre family')

legend('Lag 0','Lag 1','Lag 2','Lag 3')

Published with MATLAB® R2018a


>> x1=0:0.001:100;
>> y1=sin(x1);
>> y2=tan(x1);
>> plotyy(x1,y1,x1,y2)
Plotting Orbits

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

Chapter 16 – Numerical Methods Using MATLAB


1. Introduction
>> x=17
x =
17

>> 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=[1 2 3 4;2 3 4 5;5 6 7 8;3 5 2 6]


A =
1 2 3 4
2 3 4 5
5 6 7 8
3 5 2 6

>> 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

>> A=[1 2 3;3 4 5]


A =
1 2 3
3 4 5

>> 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

>> A=[2 4 6;1 2 5]


A =
2 4 6
1 2 5

>> B=[1 7 8;3 2 7]


B =
1 7 8
3 2 7

>> 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

>> A=[2 3 8;3 2 5];


>> B=[2 4 6;2 4 7];
>> (A>B)|(B<A)
ans =
2×3 logical array
0 0 1
1 0 0

>> x=0:pi/100:2*pi;
>> y=sin(x);
>> plot(x,y)

You might also like