Partially Updated Matlab
Partially Updated Matlab
1.1 Introduction
MATLAB is a globally available standard computational tool for engineers and scientists. The
name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide easy access
to matrix software developed by the LINPACK (linear system package) and EISPACK (Eigen system
package) projects and was introduced by Cleave Molar. The software package has been commercially
available since 1984 and is now considered as a standard tool in industry and academia globally.
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. It has powerful built-in routines that enable a very wide variety of computations. It also
has easy to use graphics commands that make the visualization of results immediately available. For
various problems related to signal processing, control theory, simulation, optimization, symbolic
computation, and several other fields of applied science and engineering can easily be computed
numerically and graphically with the help of built-in tools and routines.
Originally, the manual contains 10 computer laboratory sessions (labs). These 10 lab sessions
include not only the basic concepts of MATLAB, but also an introduction to scientific computing, in
which they will be useful for the upcoming engineering courses. The terminology, syntax, and the use
of the programming language are well defined to the beginners and the engineering graduates starting
level.
Matlab Windows:
On almost all systems, mat lab works through 3 basic windows i.e. command window,
figure window and editor window.
Matlab desktop:
This is where mat lab puts you when you launch it which, by default, consists of the
following sub windows.
1) Command window: This is the main window. It is characterized by the mat lab
command prompt (>>). All commands, including those for running user-return programs
are typed in this window at the mat lab prompt. The other sub windows which appear
with the command window are:
a) Current directory: This is where all your files from the current directory are
listed where you can do file navigation. You also have several options of what we
can do with a file once you select it i.e. you can run M.files, rename them, delete
them, etc
b) Workspace: This sub window lists all variables that you have generated so far
and shows their type and size.
c) Command history: All commands typed on the mat lab prompt in the
command window get recorded, even across multiple sessions i.e. you worked in
Monday, then on Thursday, and then on next Wednesday, so on in this window.
2) Figure Window: The output of all graphics commands typed in the command window
are flushed to the graphics or figure window, a separate gray window with white back
ground colour.
3) Editor window: This is where you write, edit, create, and save your own programs in
files called M.files. You can use any text editor to carry out these tasks
However, the figure and editor windows appear only when invoked with the appropriate
commands.
As an example of a simple interactive calculation, just type the expression you want to
evaluate.
>> 1+2
Ans=
3
>> 3*6
Ans=
18
Character Description
Command Description
cd Changes current directory
clc Clears the command window
clear Removes all variables from the
clearx memory
Removes variable x
fclose Closes a file
fopen Opens a file
global Declares global variables
lookfor Search for specified word
who Displays variables currently in the
whos memoryon variables in
Displays information
the memory
Predefined variables:-
Variabl Description
e
ans Value of last expression
eps The smallest difference between
inf two nos.
Infinity
NaN Not a number
Pi The no. π
Math functions:-
Function Description
exp Exponential
cos Cosine
sin Sine
cot Cotangent
tan Tangent
MATLAB variables are created with an assignment statement. The syntax of variable assignment is
For example,
>> x = expression
We illustrate here some simple examples which related to the elementary functions previously defined.
For example, the value of the expression y =sin(x) + ex, for x= π
>> x=pi;
>> y = sin(x)+exp(x);
y=
23.1699
The subsequent examples are
>> log(142)
ans =
4.9558
>> log10(142)
ans =
2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm (base
10) log10(x).
syms command
syms - Creates symbolic variables and functions.
syms var1 var2 …..varN – creates symbolic variables var1,var2,…,varN.
Separate variables by spaces.
syms clears all assumptions from the variable.
Example1: syms x y
creates symbolic variables x and y
Example2: syms x y integer
create symbolic variables x and y and assume that they are integers.
>> clear
The command clear or clear all removes all variables from the workspace. This
frees up system memory. In order to display a list of the variables currently in the memory,
type
>> who
while, whos will give more details which include size, space allocation, and class of the
variables.
Experiment 0:
To find the derivatives of a function the command diff ( ) is used
Syntax:
diff(f): differentiates f with respect to the variable determined by symvar(f,1)
diff(f,var): differentiates f with respect to the variable var
diff (f,n): computes the nth order derivative of f with respect to the variable
determined by symvar(f,1)
diff (f,var,n): computes the nth order derivative of f with respect to the variable
var
diff(f,var1,var2…,varn): differentiates f with respect to the variables var1,var2,
…,varn determined by symvar(f,1)
Example 1:
Find the first order derivative of f(x)=sinx 2 and find f1(2)
>>syms f(x)
>>f(x)=sin(x^2);
>>df=diff(f,x)
df(x)=
2*x*cos(x^2)
>>df2=df(2)
df2=
4*cos(4)
>>double(df2)
ans=
-2.6146
Example 2:
>>syms x t
>>diff(sin(x*t^2))
ans=
t^2*cos(x*t^2)
Because we did not specify the differentiation variables, diff uses the default
variable defined by symvar. For this expression the default variable is x.
Example 3:
To find the fourth order derivative of t6
>>syms t
>>d4=diff(t^6,4)
d4=
360*t^2
Example 4:
To find the first and second derivative of the function f(x,y)=xcos(xy)
>>syms f(x,y)
>>fx=diff(f,x)
fx=cos(x*y)-x*y*sin(x*y)
>>fy=diff(f,y)
fy= -x^2*sin(x*y)
>>fxx=diff(f,x,2)
fxx= -2*y*sin(x*y)-x*y^2*cos(x*y)
>>fxy=diff(f,x,y)
fxy=-2*x*sin(x*y)-x^2*y*cos(x*y)
>>fyy=diff(f,y,2)
fyy=-x^3*cos(x*y)
Example 5:
To find the second order derivatives of the function f(x,y)=x 2y2+2xy at the point
(1,2)
>>syms f(x,y)
>>f(x,y)=x^2*y^2+2*x*y;
>>fxx=diff(f,x,2);
>>fxx(1,2)
ans=
8
>>fxy=diff(f,y,2);
>>fyy(1,2)
ans=
-cos(2)
>>double(ans)
ans=
0.4161
Example-3
ax 2 2
If y=e sin bx , then prove that
y 2 −2 ay 1 +( a +b ) y=0
>> syms a b f(x)
>>f(x)= exp(a*x)*sin(b*x);
>>E=diff(f,x,2)-2*a*diff(f,x)+(a^2+b^2)*f;
>>simplify(E)
Exercise Problems
d 2 y dy
( x +1) 2 +x =m2 y
2
EXPERIMENT-2
Solving Ordinary Differential equations
The command dsolve ( ) is used to solve ordinary differential equations
Syntax:
dsolve(ode,var); will solve the differential equation w.r.t the independent
variable var
dsolve(ode,var,condition); solve the differential equation w.r.t the variable
var and the given condition
dsolve(ode,var,condition1,condition2); solve a second order differential
equation w.r.t the variable var and the given conditions
Example 1:
d3 y 2
To solve the differential equation 3
=a x
dx
>>syms a x
>>dsolve(‘D3y=a*x^2’,’x’)
Example 2:
dy
To solve =ay
dt
>>syms a
>>sol=dsolve(‘Dy=a*y’)
Sol=
C1*exp(a*t)
Note that the independent variable is not mentioned in the dsolve command. In this case
the default independent variable is ‘t’.
Alternative Method:
syms a y(t)
eqn=diff(y,t)==a*y;
dsolve(eqn)
Alternative Method:
>>syms a b y(t)
>>eqn=diff(y,t,2)=a^2*y;
>>dy=diff(y,t);
>>cond=[y(0)==b,dy(0)==1]
>>dsolve(eqn,cond)
Example 5:
d2 y ( 2 dy
Solve the differential equation 2
= 1− y ) − y
dx dx
>>syms y(x)
>>eqn=diff(y,x,2)==(1-y^2)*diff(y,x)-y;
>>dsolve(eqn)
Warning: Explicit solution could not be found.
Example:
Solve y ''−3 y' +2 y=4 t+e 3 t given that y(0)=1, y' (0)=-1.
>>clear all;
>>syms x y;
>>k=dsolve(‘D2y-3*Dy+2*y-4*t-exp(3*t)’,’y(0)=1,Dy(0)=-1’,’t’)
>>pretty(k)
Exercise Problems
d 2 y dy
+2 −3 y =sin t
1. Solve dt 2 dt given that y = y' =0 when t=0.
d 2 y dy
+2 +5 y=e−t sin t
2. Solve dt 2 dt given that y(0)=0, y' (0)=1 when t=0.
2
d y dy dy
+ 4 −5 y =5
3. Solve dt 2 dt given that y= dt =2 when t=0.
d2 y dy dy
2
−10 +24 y=24 x
4. Solve dx dx given that y= dt =0 when x=0.
d2 x dx dx
2
−2 +x=e−t
6. Solve dt dt given that x=2; dt = -1 when t=0.
Experiment - 3
Aim: Creating simple, basic and specialized two dimensional plots
Example
1. plot y=sin x , o ≤ x ≤ 2π , taking 100 linearly spaced points in the given interval . label
the axes and put plot created by your name in the title
Syntax:
>>syms x;
>>x=linspace(0,2*pi,100);
>>y=sin(x);
>> xlabel(‘X-axis’)
>>ylabel(‘Y-axis’)
>>title(‘RAMA’)
>>plot(x,y)
Ouput:
x 2 −x +1
f (x )=
13. Plot the function x 2 + x+ 1 for −10 ≤ x ≤ 10.
14. the gate way arch of St.Louis is shaped according to the equation
>>p=diff(f,x)
>>q=diff(f,y)
>>r=diff(p,x) or diff(diff(f,x),x);
>>t=diff(q,y) or diff(diff(f,y),y)
>>diff(u)
Example 1: Use the differential dz to approximate the change in Z=√ (4−x 2− y 2) as (x,y)
moves from the point(1,1) to the point (1.01,0.97). Compare this approximation with the
exact change in Z.
clc
syms x y
z = @(x,y)sqrt(4-x^2-y^2);
pzx=diff(z,1,x);
pzy=diff(z,1,y);
dx=0.01;
dy=-0.03;
dz=(pzx*dx+pzy*dy)
dz=subs(subs(dz,x,1),y,1)
delz=z(1.01,0.97)-z(1,1)
fprintf('difference between the approximate change and exact change is %0.2f\n',(delz-dz))
Example 2: The possible error involved in measuring each dimension of rectangular box is ± 0.1
milimeter. The dimensions of the box are x=50cm, y=20 cm, z=15cm. Use dV to estimate the
propagated error and the relative error in the calculated volume of the box.
clc
syms x y z
v=@(x,y,z)x*y*z;
pvx=diff(v,x);
pvy=diff(v,y);
pvz=diff(v,z);
dx=0.01;dy=0.01;dz=0.01;
dv=pvx*dx+pvy*dy+pvz*dz;
dv=subs(subs(subs(dv,x,50),y,20),z,15);
volume=v(50,20,15)
rel_volume=double(dv/volume)
Exercise Problems
1. A company manufactures two types of wood-burning stoves: a freestanding model and a
fireplace-insert model . The cost function for the producing x freestanding and y fireplace-
3. The altitude of a right circular cone is 10cm and increasing at rate of 0.1cm/sec. The radius of
the base is 5cm and is decreasing at a rate of 0.2cm/sec. How fast is the volume changing.
dw
, w x, y sin 2 x 3 y , x s t , y 2t.
4. Determine dt where
5. The radius of a right circular cylinder is increasing at a rate of 8 mm/s and the height is
decreasing at a rate of 15 mm/s. Obtain the rate at which the volume is changing in cm 3/s
when the radius is 40mm and the height is 150 mm.
df
, f x, y, z xyz , x t 2 , y 2t , z e t .
6. Determine dt where
7. One side of a plane triangle is 8 feet long and increasing 4 inches per second, another side is 5
feet, and decreasing 2 inches per second. An included angle is 600 and increasing 20 per
second. At what rate is the area of triangle increasing?
dz
, z x, y s x 2 y 2 , x s cos t , y s sin t.
8. Determine dt where
9. Determine the rate of change of z when x is 3 units and y is 2 units when x is decreasing at 5
Experiment-4
Jacobian and Taylor’s series for functions of two variables
J=
-------
>>det(J)
Ans=
------
end
Exercise Problems
3. Apply Taylor’s theorem to expand the function f(x, y) = eax cos(by) in powers of x and y upto
third degree terms.
y
4. Expand f (x , y )=e log(1+x) in powers of x and y up to the terms of second degree by
applying Taylor’s theorem.
Experiment-5
Determine maxima and minima for function of two and three variables
Example :
Determine the maximum and minimum values of the function f(x,y)=x 3y2(1-
x-y)
syms f(x,y)
f(x,y)=x^3*y^2*(1-x-y);
p=diff(f,x);
q=diff(f,y);
r=diff(f,x,x);
s=diff(f,x,y);
t=diff(f,y,y);
sol=solve(p,q);
[sol.x';sol.y']'
n=length(sol.x);
k=0;
for i=1:n
if(isreal(sol.x(i))&&isreal(sol.y(i)))
k=k+1;
x1(k)=double(sol.x(i));
y1(k)=double(sol.y(i));
end
end
for i=1:k
r1(i)=double(r(x1(i),y1(i)));
s1(i)=double(s(x1(i),y1(i)));
t1(i)=double(t(x1(i),y1(i)));
v(i)=r1(i)*t1(i)-s1(i)^2;
if (v(i)<0) fprintf('saddle point at (%f,%f)\n',x1(i),y1(i))
else
if (v(i)>0 && r1(i)>0)
fmin=double(f(x1(i),y1(i)));
fprintf('Min value occurs at (%f,%f)and the minmum value
is %f\n\n',x1(i),y1(i),fmin)
else
if (v(i)>0 && r1(i)<0)
fmax=double(f(x1(i),y1(i)));
fprintf('Max value occurs at (%f,%f)and the maximum value
is %f\n\n',x1(i),y1(i),fmax)
else
if (v(i)==0) fprintf('At the point(%f,%f)further
investigation is needed\n\n',x1(i),y1(i))
end
end
end
end
end
end
double(sol.x1(i)),double(sol.y1(i)),double(sol.z1(i)),double(value(i))
);
end
Exercise Problems
The int function is used to integrate a symbolic expression “f “ . The int function can be
used in the following forms
>> int(f) returns integral of the expression “f” with respect to the default independent
variable
>>int(f,’t’) returns the integral of the expression “f” with respect to the variable “t”
>>int(f,a,b) returns the integral of the expression “f” with respect to the default
independent ariable evaluated over the interval [a,b] , where a and b are the numeric
expressions
>>int(f,t,a,b) with respect to t with in a and b
>>int(f,’m’,’n’) returns the integral of the expression “f’ with respect to the default
independent variable evaluated over the interval of [m,n] where m and n are
symbolic expressions.
Example 1) ∫ xn dx
Matlab solution
>>syms x
>>f=x^n
>>int(f)
2
Example 2) ∫ e−x dx
2
• In this example , e−x there is no formula for the integral involving standard calculus
expressions . in this case matlab returns an answer interms of the error function erf.
• If matlab is unable to find an answer to the integral of a function “f” . it just returns
int(f)
Double integration
y−max x−max
∫ ∫ f ( x , y)dx dy
To evaluate integrals of the form y−min x−min
Matlab solution:
>>syms variables
>> f=given function
>>s=int(int(f,’x’,xmin,xmax),’y’,ymin,ymax)
1 2
f =∫ ∫ 1−6 x 2 dx dy
Example 1 −1 0
>>syms x y
>>f=1-6*x^2*y
>>s=int(int(f,’x’,0,2),’y’,-1,1)
π sin x
32
f =∫ ∫ ( x 2 + y 2 ) dx dy=π 2 −
Example2 0 0 9
>>syms x y
>>f=x^2+y^2
>>s=int(int(f,’y’,0,sin(x)),’x’,0,pi)
I 6 x 2 y dA
2
Working:
Syntax:
>> clc
>>syms x y
>>f= 6*x+2*(y^2);
>> int(int(f,’x’,y^2,(2-y)),’y’,-2,1)
Ans
4 4 x
xy dx dy
1 0
Example 4: Evaluate the integral
>> clc;
>>syms x y
>>f= x*y;
>> int(int(f,’y’,0,sqrt(4-x)),’x’,1,4)
Ans= 9/2
Example 5: Change the order of integration and evaluate the iterated integral:
1 1
xy sin x dxdy
4
0 y1/3
Sol: From the figure we see that the region D is bounded above by y x and below by
y 3
0 . The
projection of D onto the x-axis the interval 0 x 1. Using the order of integration dydx we have
5 1
cos 1
16 4
MATLAB code:
>> clc
>>syms x y
>>f= (x*y + sin (x^4);
>> int(int(f,’x’,(y^1/3),1),’y’,0,1)
z dV
where A is the region inside the sphere x y z 2, inside the
2 2 2
Example 6: Compute A
Working:
2 1 2r 2
zdV
A
0
0 0
z r dz dr d
Syntax:
>> clc;
>>syms z r t
>>f= z*r;
>>int( int(int(f,’z’,0,sqrt(2-r^2)),’r’,0,1),’t’,0,2*pi)
Ans=
Tripleintegration
z−max y−max x−max
∫ ∫ ∫ f ( x, y ,z )dx dy dz
To evaluate integrals of the form z−min y−min x−min
>>syms x y z
>>f=given function
>>s=int (int (int(f,’x’,x ,x ),’y’,y ,y ),’z’,z ,z )
min max min max min max
Example 1:
1 1 1
f =∫∫∫ xyz dx dy dz
0 0 0
>> syms x y z
>>f=x*y*z
>>s=int(int(int(f,’x’,0,1),’y’,0,1),’z’,0,1)
Exercise Problems
Using matlab symbolic functions determine the values of the following
π
2
∫ x2 dx ∫ sin 2 x dx
1. 2. 0
∫ √ x 2−sin4 x dx
3. ∫ cos(at+b)dt 4. 0
1 1 1+x
∫e x
dx ∫ ∫ ( x 2+ y )dydx
5. −∞ 6. −1 /2 −x
4 2 3 3−x 3−x−y
2 z yz a x y+ x
1 2−x
∫ ∫ xydxdy .
1. Evaluate the integral by reversing the order of integration 0 1
2
1 √ 2− x
x
∫ ∫ dydx
2. Evaluate 0 x x + y2
2
by changing to polar coordinates.
1 y
10 xz 3 dV x 2 y 2 z 2 16 and z 0
9. Evaluate E where E is the region bounded by
by applying spherical polar coordinates.
Experiment-7
Determine area, volume, mass and moment of inertia by double and
triple integrals
Syntax for evaluating Area:
>>syms x y;
>> f=1;
Area=
----------
>> f=1;
Volume=
----------
Mass=
--------
Ix=
--------
Iy=
--------
>>Io=Ix+Iy
Io=
---------
>> f=1;
Volume=
---------
Example: Find the volume of the solid in the first octant bounded by the planes
x 0, y 0, z 0, x 2 y z 6
Working:
6 6 x / 2 6 x 2 y
V dV dz dy dx
0 0 0
D
Syntax:
>> clc
>>syms x y z
>>f= 1;
>>int( int(int(f,’z’,0,6-x-2*y),’y’,0,(6-x)\2),’x’,0,6)
Ans=
Exercise Problems
1. Use double integration to find the area of the region in the xy-plane bounded by the
given curves
y x and y x 4
4. Find the volume of the solid in the first octant bounded by the planes
x 0, y 0, z 0, x 2 y z 6
Experiment-8
Determine gradient, divergence and curl of vector point functions
Syntax for gradient of a Scalar point function :
syms x y z
f= given spf;
F=gradient(f,[x,y,z]);
F_val=subs(F,[x,y,z],[a, b, c])
Example 2: If f x, y , z 3 x y
2
y 3 z 2 , Compute grad f at the point 1,2,1
>>syms x y z
>> f=3*x^2*y-y^3*z^2;
>> k=gradient(f)
>>k = 6*x*y
3*x^2 - 3*y^2*z^2
-2*y^3*z
>> syms x y z
>>f =[ 3*x*z 2*x*y -y*z^2 ] ;
>> divergence(f,[x y z])
>>ans =
6*y + 6*x*z
Syntax for curl of a Vector point function :
syms x y z
f=[x_component y_component z_component];
F= curl (f,[x,y,z]);
F_val=subs(F,[x,y,z],[a, b, c])
clc
syms x y z
f=[x^2 y^2 x^2];
F= curl (f,[x,y,z]);
F_val=subs(F,[x,y,z],[1,-1,2])
ans =
0
0
0
>> if f==0 disp(‘irrotational’)
Else disp(‘not irrotational’)
End
Ans=
irrotational
Exercise Problems
1. Determine grad
, where f is given by x 3 y 3 xz 2 at the point 1,1,2 .
3.Given the velocity potential f =x 2−6 x− y 2 of a flow, determine the velocity v
of the field and its value at P (−1,5 )
−1
4.. Determine the force in an electrostatic field given by 2 2 2 2
f =( x + y + z ) at P ( 12,0,16 )
Output: 24*pi
Output: 24*pi
Exercise Problems
2
1) If F=( 5 xy−6 x ) i+ ( 2 y−4 x ) j,
2) Evaluate ∫ c F . dr along the curve C in the xy – plane, y = x3 from (1, 1) to (2, 8).
3) Using the line integral, compute the work done by the force
F=( 2 y+ 3 ) i+ xzj+ ( yz −x ) k when it moves a particle ¿ the point ¿)
¿ the point ( 2,1,1 ) along the curvex=2 t 2 , y=t , z=t 3
4) If F=2 yi−3 j+ x 2 k ∧S isthe surface of the parabolic cylinder
y 2=8 x ∈the first octant bounded by the planes y=4∧z=6 , ¿ˇ
∫ s F . Nds=132
5) If F=2xz i-x j+y2k , evaluate ∭ V FdV where V is the region bounded by the surfaces
x=0,x=2,y=0,y=6,z=x2,z=4
Experiment-10
Application of Green’s, Gauss Divergence’s and Stoke’s theorems
GAUSS DIVERGENCE THEOREM
If F is a continuously differentiable vector function in the region E bounded by the closed surface
S, then
∫ S F . Nds= ∫ E ÷F dv , where N is theunit external normal vector .
Syntax:
syms x y z
f=[x_component y_component z_component];
F= divergence (f,[x,y,z]);
I=int(int(int(F,’x’,xmin,xmax),’y’,ymin,ymax),’z’,zmin,zmax)
I=
-------
Syntax:
syms x y
phi= declare phi function;
sye= declare sye function;
f = diff(sye,x)-diff(phi,y);
I=int(int(f,’x’,xmin,xmax),’y’,ymin,ymax)
I=
-------
STOKE’S THEOREM
If S be a surface bounded by a closed curve C and F=f 1 i+f 2 j+ f 3 k be any continuously
differentiable vector point function, then ∫ c F . d r= ∫ s curl F . NdsWhere N is unit external normal
at any point S.
Syntax:
Exercise Problems
1) Determine ∬ F . ndA ,where F= (2x2-3z) i-2xy j-4x k, S the surface of the region
x=0,y=0,z=0,2x+2y+z=4
2) Evaluate ∬ F . ndA ,where F= xy i+z 2 j+2yz k, over the tetrahedron bounded by
x=0,y=0,z=0,x+y+z=1.
3) Evaluate ∫ C ( 3 x−8 y 2 ) dx + ( 4
y−6 xy ) dy
where C is theboundary of the region bounded by x=0 , y=0∧x + y=1
4) Use Green’s theorem in a plane to find the finite area enclosed by the parabolas y 2= 4ax
and x2=4ay.
3
5) Determine ∫ C F . d r for a vector field defined by F=− yi+ x 3 j∈theregion x 2 + y 2 ≤ 1 , z=0
3 3
6) Determine ∫ C F . d r where F= y i+ x z j+ z y k where C isthe ¿˚
x 2+ y 2=4 , z=1.5
Experiment-10
Solve PDE by Finite Difference Method
Syntax: