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

Partially Updated Matlab

The document describes a lab session list for an introduction to MATLAB course. The list includes 11 experiments covering basic MATLAB commands, solving equations, plotting, calculus topics like derivatives, integrals, and PDEs. Each experiment is assigned to one of 4 Course Objectives to help students learn key MATLAB and math skills.

Uploaded by

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

Partially Updated Matlab

The document describes a lab session list for an introduction to MATLAB course. The list includes 11 experiments covering basic MATLAB commands, solving equations, plotting, calculus topics like derivatives, integrals, and PDEs. Each experiment is assigned to one of 4 Course Objectives to help students learn key MATLAB and math skills.

Uploaded by

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

Lab

Session List of Experiments CO- Mapping


No
1 Introduction MATLAB with basic commands All CO’s
2 Solving linear and nonlinear equations All CO’s
3 Plots All CO’s
4 Finding partial, total derivatives and chain rule. CO-1
5 Jacobian and Taylor’s series for functions of two
variables. CO-1
6 Determine maxima and minima for function of two and CO-1
three variables.
7 Evaluation of double and triple integrals CO-2

8 Determine area, volume, mass and moment of inertia CO-2


by double and triple integrals.
In Sem- Lab exam
9 Determine gradient, divergence and curl of vector point CO-3
functions.
10 Application of Green’s, Gauss Divergence’s and CO-3
Stoke’s theorems.
11 Solving PDE by finite difference method CO-4
12 Solving PDE by finite difference method CO-4

Introduction to MATLAB for Engineering Students

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 a high-performance language for technical computing. It integrates computation,


visualization, and programming environment. Furthermore, MATLAB is a modern programming
language environment: it has sophisticated ‘data structures’, contains built-in editing and debugging
tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for
teaching and research in the fields of science and engineering. MATLAB has many advantages
compared to conventional computer languages viz., C, FORTRAN for solving technical problems.

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.

“Introduction to MATLAB for Engineering Students" is a manual used for an introductory


course in MATLAB and technical computing. The availability of technical computing environment using
MATLAB is facilitating to involve students in doing projects in computer science and in more intense
problem-solving experience in various Engineering fields like to conduct numerical experiments and to
tackle realistic and more complicated problems.

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.

1.2 A minimum MATLAB session:

The goal of this minimum session is to learn the following steps:


 How to log on
 Invoke MATLAB
 Do a few simple calculations
 How to quit MATLAB

1.3 Starting MATLAB


After logging into your account, you can enter MATLAB by double-clicking on the MATLAB shortcut
icon on your Windows desktop. When you start MATLAB, a
special window called the MATLAB desktop appears. The desktop is a window that contains other
windows. The major tools within or accessible from the desktop are:

 The Command Window


 The Command History
 The Workspace
 The Current Directory
 The Help Browser
 The Start button

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.

1.4 Using MATLAB as a calculator

As an example of a simple interactive calculation, just type the expression you want to
evaluate.
>> 1+2
Ans=
3
>> 3*6
Ans=
18

Basic arithmetic operators

1.5 Quitting MATLAB


To end your MATLAB session, type quit in the Command Window, or select File →Exit
MATLAB in the desktop main menu.

1.6 SUMMARY OF CHARACTERS, COMMANDS AND FUNCTIONS:-

Characters and Arithmetic operations:-

Character Description

* Scalar and array multiplication


.* Element by element
multiplication
/ Right division
\ Left division
./ Element by element right
.\ division left division
Element by element
^ Exponentiation
.^ Element by element
: Createexponentiation
vectors with equally
= space elements
Assignment operator
; Suppresses display, ends row
… in arrayof line
Continuation
Managing Commands:-

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

abs Absolute value

exp Exponential

factorial The factorial function

log Natural logarithm

log10 Base10 logarithm

sqrt Square root

cos Cosine

sin Sine

cot Cotangent
tan Tangent

acos Inverse cosine

acot Inverse cotangent

atan Inverse tangent

cosh Hyperbolic cosine

coth Hyperbolic cotangent

ceil Round towards infinity

fix Round towards zero

round Round to the nearest integer

disp Displays output

fprintf Displays or saves output

input Prompts for user input


helpdesk Online HTML help

demo Run demo programme

ver Matlab version info

1.7 Creating MATLAB variables

MATLAB variables are created with an assignment statement. The syntax of variable assignment is

variable name = a value (or an expression)

For example,
>> x = expression

where expression is a combination of numerical values, mathematical operators, variables,


and function calls. On other words, expression can involve:
1. manual entry
2. built-in functions
3. user-defined functions

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.

1.8 Managing the workspace


The contents of the workspace persist between the executions of separate commands.
Therefore, it is possible for the results of one problem to have an effect on the next one. To avoid this
possibility, it is a good idea to issue a clear command at the start of each new
independent calculation.

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

1. If y=sinh [m log( x+ √ x 2 +1)] then show that dx dx .


2 2
d y dy
2. If
−1
y=tan (sinh x) , prove that dx 2
+tan y
[ ]
dx
=0
.
10 9 10 11
3. y=(2−3 x) find y , y and y .

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)

Example 3: To solve the initial value problem dy/dt=ay, y(0)=5


syms a y(t)
eqn=diff(y,t)==a*y;
cond=y(0)==5;
dsolve(eqn,cond)

Example 4: To solve the initial value problem d2y/dt2=a2y, y(0)=b, y’(0)=1


syms a y(t)
eqn=diff(y,t,2)==a*y;
dy=diff(y,t);
cond1=y(0)==b;
cond2=dy(0)=1;
s=dsolve (eqn, cond1, cond2)

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.

5. Solve y ''−3 y' +2 y=4 e2 t given that y(0)= - 3 , y' (0)=5.

d2 x dx dx
2
−2 +x=e−t
6. Solve dt dt given that x=2; dt = -1 when t=0.

7. Solve y ''+ y'−2 y=t given that y(0)=1, y' (0)=1.


dy d2 y
8. Solve y '''−3 y''+3 y '− y=t 2 et given that y=1, dt =0, dt 2 = -2 at t=0.

Experiment - 3
Aim: Creating simple, basic and specialized two dimensional plots

Few commands and their description


Plot( ) command is used to create two dimensional plots.
plot(y) – elements of y are plotted against their indexes.
Example 1:
>>plot(rand(1,20))
Plots 20 random numbers against the integers 1 to 20 joining successive points with
straight lines.
Example 2:
>>x=0:pi/40:4*pi;
>>plot(x,sin(x))
Example 3:
Draw a straight line joining the points (0,1) and (4,3)
>>plot([0 4],[1 3])
[0 4] contains x-co-ordinates and [1 3] contains y-co-ordinates

>>plot(x,y) : where the arguments x and y are each a vector.


Both must have the same number of elements.
>>plot(x ,y, ‘line specifier’)
>>plot(x , y) : A blue solid line connects the points with no
marks(default)
>>plot(x, y, ‘r’) : A red line connects the points.
>>plot(x, y, ‘—y’) : A yellow dashed line connects the points.
>>plot(x ,y, ‘*’) :The points are marked with * .
>>xlabel(‘p length’) : Label the x-axis with p length.
>>ylabel(‘fluid pre’) : Label the y-axis with fluid pre
>>title(‘pre variation’) : Title the plot with pre variation
>>text(2,6, ‘note’) : writes “note” at the location (2,6) in the plot
coordinates
Axis control:
axis([xmin xmax ymin ymax])
Examples:
>>axis([-5 10 2 22]) :Sets the x-axis from -5 to 10, y-axis from 2 to 22.
>>axis(‘equal’) :Sets the equal scale on both the axis
>>axis(‘square’) :Sets the default rectangular frame to square
>>axis(‘normal’) :Resets the axis to default values
>>axis(‘axis’) :Freezes the current axis limits
>>axis(‘off’) :Remove the surrounding frame and the thick marks
Note : axis command must come after plot command
Fplot command:
fplot(‘function’, [limits], ‘line specifier’)
Multiple graphs in the same plot:
>>plot(x,y,u,v,t,h) : Creates three graphs x vs y, u vs v, t vs h all in the same
Plot.
>>plot(x,y, ‘-b’,u,v, ‘—r’,t,h, ‘m’ ): Also we can add line specifiers.
>>gtext(‘taylor series’) : Writes the specified string at a location clicked with the
mouse in the graphic window.
Plots with logarithmic axes:
>>semilogy(x,y) : plots y verses x with a log(base10) scale for the y-axis and linear
scale for the x-axis
>>semilogx(x,y) : plots y verses x with a log(base10) scale for the x-axis and linear
scale for the y-axis
>>loglog(x,y) : plot y verses x with a log(base10) scale for both the axes

List of other functions commonly used for plotting x,y data

>>area(x,y) : creates a filled area plot


>>bar(x,y) : creates a bar graph
>>barh(x,y) : creates a horizontal bar graph
>>comet(x,y) : makes an animated 2D-plot
>>compass(z) : creates a row graph for complex numbers
>>feather(x,y) : makes a feather plot
>>fill(x,y, ‘k’) : draws filled polygons of specified color
>>hist(y) : when y=randn(50,1); makes histograms
>>pie(x) : creates a pie chart
>>polor(theta,radius) : plots curve in polar co-ordinates
>>stairs(x,y) : plots a stair graph
>>stem(x,y) : plots a stem graph

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:

Plot for a given polynomial :


Example: x3-5x+1=0
>>p= [1 0 -5 1];
>>x=[-5:0.1:5];
>>y=polyval(p,x);
>>plot(x,y),grid

Plot for a given transcendental equation


Example: sin2x
>>x= [0:0.1:5]
>>y=sin(2*x)
>>plot(x,y),grid

NOTE: We can plot both the equations(polynomial and transcendental) by using


>>ezplot(‘given equation’),grid
Exercise Problems
2. plot y = e−0.4x sin x,0 ≤ x ≤ 4π taking 10, 50 and 100 points in the interval
3. Draw a circle of unit radius using the parametric equation of a unit circle x = cosθ , y
= sinθ ,0 ≤θ ≤ 2π
4. Use the command plot3(x,y,z) to plot the circular helix (space curve) x(t) = sint , y(t) =
cost , z(t) = t ,0 ≤ t ≤ 20
5. Type the following programme in command window and observe the output of overlay
plot with
hold on command.
>>x=linspace(0,2*pi,100)
>>y1=sin(x)
>>plot(x,y1)
>>hold on
>>y2=x
>>plot(x,y2,‘-‘ )
>>y3=x-(x.^3)./6+(x.^5)/120
>>plot(x,y3, ‘0’)
>>axis([0 5 -1 5])
>>hold off
6. Use the fplot command and plot f (t) = t sin t , 0 ≤ t ≤ 10π
7. Use semilogx command to plot x = e−t , y = t , 0 ≤ t ≤ 2π
8. Use semilogy command to plot x = t , y = et , 0 ≤ t ≤ 2π
9. Use loglog command to plot x = et , y = 100 + e2t , 0 ≤ t ≤ 2π
10. Use polar command to plot r2 = 2sin 5t , 0 ≤ t ≤ 2π
11. Observe the nature of fill command
>>t=linspace(0,2*pi,200)
>>r=sqrt(abs(2*sin(5*t)))
>>x=r.*cos(t)
>>y=r.*sin(t)
>>fill(x,y, ‘k’)
>>axis(‘square’)
12. Plot the function y = 3x3 − 26x +10 and its first and second derivatives for − 2≤ x
≤ 4 all in the same plot.

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

y=693 . 8−68. 8 cosh (99x. 7 ) ft. Make a plot of the arch.


Experiment-3
Finding Partial, total derivatives and chain rule

Syntax for partial derivatives:


>> syms independent vars,arbitrary constants;

>>f= given function;

>>p=diff(f,x)

>>q=diff(f,y)

>>r=diff(p,x) or diff(diff(f,x),x);

>>s=diff(p,y) or diff(diff(f,x),y) or diff(q,x)

>>t=diff(q,y) or diff(diff(f,y),y)

Syntax for Total derivatives:


If u=f(x,y) where x=g(t) and y=h(t) then to find du/dt, the required matlab code is:
>> syms independent vars,arbitrary constants;

>>x= given function;

>>y= given function;

>>u= given function;

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

insert stoves is C=32 √ xy+175 x+205 y+1050. Then evaluate


∂C ∂C
and
(i) The marginal costs ( ∂x ∂y
) when x = 80 and y = 20.
(ii) When additional production is requried, which model of stove results in the cost increasing
at a higher rate ? How can this be determined from the cost model?
2. At what rate is the area of a rectangle changing if its length is 15m and decreasing at a rate of
3 ms-1 while its width is 6m and increasing at a rate of 2ms -1

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

units/s and y is increasing at 2.5 units/s for the function


z  3x 2 y 5 .
dy
10. Determine given y3+y2-5y-x2+4=0.
dx
∂(u , v , w)
11. If u=x+3 y 2−z 3 , v=4 x 2 yz , w=2 z2− xy , Evaluate at (−1,1,0 ) .
∂(x, y ,z)
∂w ∂w
12. Use the chain rule to determine and for w= 2xy where x= s2+t2 and y= s/t.
∂s ∂t

Experiment-4
Jacobian and Taylor’s series for functions of two variables

Syntax for Jacobian:

>> syms independent vars,arbitrary constants;

>>u= given function;

>>v= given function;

>>J=Jacobian( [ u v], [x y])

J=

-------

>>det(J)

Ans=

------

if det(J==0) disp(‘u and v are functionally depent’)

else disp(‘u and v are not functionally depent’)

end

Syntax for Taylor’s series for functions of two variables:

>> syms independent vars,arbitrary constants;


>>f= given function;

>>taylor(f,[x,y],[a,b],’order’,n) % n represents (n-1)th partial derivative

Note: taylor(f,[x,y],[a,b]) %displays complete seris

Exercise Problems

1. Determine t h e JacobianJ ( xr ,, θy ) , for polar coordinates x=r cosθ ,


y=r sinθ .
2. In spherical polar coordinates x= rsinθ cos ∅ , y=rsinθ sin ∅ , z =rcosθthen
∂( x , y , z)
= θ
∂(r ,θ , φ) r2sin .

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

Lagrangian method of multipliers :

Determine maximum of f(x,y,z)= 8xyz subject to the condition

x2+y2+z2-1 by using Lagrangian method of multipliers


clc
syms lamda f(x,y,z) g(x,y,z)
f(x,y,z)=8*x*y*z;
g(x,y,z)=2*x^2+2*y^2+3*z^2-36;
h=f+lamda*g;
pdh_x=diff(h,x);
pdh_y=diff(h,y);
pdh_z=diff(h,z);
pdh_lamda=diff(h,lamda);
sol=solve(pdh_x,pdh_y,pdh_z,pdh_lamda);
[sol.x';sol.y';sol.z']'
k=0;
n=length(sol.x);
for i=1:n
if(isreal(sol.x(i))&&isreal(sol.y(i))&&isreal(sol.z(i)))
k=k+1;
sol.x1(k)=real(sol.x(i));
sol.y1(k)=real(sol.y(i));
sol.z1(k)=real(sol.z(i));
end
end
for i=1:k
value(i)=double(f(sol.x1(i),sol.y1(i),sol.z1(i)));

fprintf('value of the function at the point(%.2f,%.2f,%.2f) is


%0.04f\n',...

double(sol.x1(i)),double(sol.y1(i)),double(sol.z1(i)),double(value(i))
);
end

Exercise Problems

1. Determine maximum and minimum of f(x,y)= x2y2.


2. Determine maximum and minimum of f(x,y)= x3y2(1-x-y).
f  x, y   x 4  y 4  4 xy  2 y 2 ( x  0, y  0)
3. Compute the extreme values of the function , .

4. A rectangular box is resting on the


xy  plane with one vertex at the origin. The opposite vertex lies

in the plane 6 x  4 y  3 z  24 . Determine the maximum volume of such a box.


5. Determine the dimensions of the rectangular box, open at the top, of maximum capacity whose
surface is 432 sq.cm.
2
6. Obtain the shortest distance from the origin to the sphere xyz =2
Experiment-6
Evaluate double integrals and triple integrals and its applications

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

Example 3: Evaluate R where   R   is the region enclosed by the parabola x =


y2   and the line   x + y = 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

cylinder x  y  1 and the above the xy  plane.


2 2

Working:

2 1 2r 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

∫∫ ( x+1y)2 dydx ∫∫ ∫ xyz dzdydx


7. 3 1 8. 0 0 0

2 z yz a x y+ x

∫∫∫ xyz dxdydz ∫∫ ∫ e x+ y+ z dzdydx


9. 0 0 0 10. 0 0 0
Applications:

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

3. Evaluate the iterated integral by converting to polar coordinates


 0 y
x 2  y 2 dxdy
1 2 2
  
0 0 1
x 2 yz dx dy dz
4. Evaluate
1 z x z
  
1 0 xz
 x  z dydx dz
5. Evaluate
∭ dx dy dz
6. Evaluate V where V is the finte region of space formed by the planes x = 0, y =
0, z = 0 and 2x + 3y + 4z = 12.
2 2
7. Evaluate the volume bounded by the cylinders x + y =4 , y +z=4 and z=0

 4 xydV z  2 x 2  2 y 2  7  and  z  1 by


8. Evaluate   E  where E is the region bounded by 
applying cylindrical polar coordinates.

  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= int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_ lowerbound,y_upperbound)

Area=

----------

Syntax for evaluating Volume:


>>syms x y z;

>> f=1;

>> Volume= int(int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_


lowerbound,y_upperbound),z,’z’, ’x’, z_lowerbound,z_upperbound)

Volume=

----------

Syntax for evaluating Mass:


>>syms x y;

>> f= given density function;

>> Mass= int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_ lowerbound,y_upperbound)

Mass=

--------

Syntax for evaluating Moment of Inertia Ix, Iy, I0:


>>syms x y;

>> f= given density function*y^2;

>> Ix= int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_ lowerbound,y_upperbound)

Ix=

--------

>> f= given density function*x^2;

>> Iy= int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_ lowerbound,y_upperbound)

Iy=

--------

>>Io=Ix+Iy

Io=
---------

Syntax for evaluating Volume:


>>syms x y z;

>> f=1;

>> Volume=int( int(int(f, ’x’, x_lowerbound,x_upperbound),’y’,y_


lowerbound,y_upperbound),’z’, z_lowerbound,z_upperbound)

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

2. Sketch the region R in the xy-plane bounded by the curves


y  2 x and and y  x
2

find its area.


3. Find the volume of the region bounded below and above by the surfaces
z  x 2  y 2 and z  2  x 2  y 2
.

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 1: Determine gradient of x3+y3-z2 at (1,-1,2).


clc
syms x y z
f=x^3+y^3-z^2;
F=gradient(f,[x,y,z]);
F_val=subs(F,[x,y,z],[1,-1,2])

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

Syntax for divergence of a Vector point function :


syms x y z
f=[x_component y_component z_component];
F= divergence (f,[x,y,z]);
F_val=subs(F,[x,y,z],[a, b, c])

Example 1: Determine divergence of ḟ =x2 i+y2 j-z2 k at (1,-1,2).


clc
syms x y z
f=[x^2 y^2 x^2];
F= divergence (f,[x,y,z]);
F_val=subs(F,[x,y,z],[1,-1,2])

Example 2: If ⃗v =[ 3 xz , 2 xy ,− y z 2 ] =3 xzi+2 xyj± y z 2 k , evaluate div ⃗v

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

Example: Determine curl of ḟ =x2 i+y2 j-z2 k at (1,-1,2).

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

Example 2: Check whether f  (6 xy  z )i  (3 x


3 2
 z ) j  (3 xz 2  y )k is irrotational or not.
syms x y z
>> f = [ 6*x*y+z^3 3*x^2-z 3*x*z^2-y];
>> curl(f)

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 )

5. Given the velocity potential f =x 2 y −6 yz−z 2 x of a flow, Compute the velocity v


of the field and its value at P (−1,5,1 )
6. Determine a unit normal vector n of the cone of revolution z 2=4 ( x 2+ y2 ) at the point P
( 1,0,2 )
7. Find the directional derivative of f  xy  yz  zx in the direction of vector i  2 j  k at the
point (1, 2 , 0).

8. In what direction from the point (1,1,-1) is the directional derivative of f  x 2  2 y 2  4z 2


is maximum? Also find the value of this maximum directional derivative.

9. Determine div f where f  r n r .

10. Determine div f where f  grad ( x  y  z  3xyz )


3 3 3

11. If f =x y i+ 2 x yzj−3 y z k Determine Curl f at the point (1,-1.1)


2 2 2

12. Determine ∇ × ⃗v where ⃗v =xyz [ x , y , z ]


v =( y 2 −z2 +3 yz−2 x)i +(3 xz +2 xy) j+( 3 xy −2 xz +2 z)k is incompressible?
13. Is the vector ⃗
14. Check whether the vector function v⃗ =x 3 ( y 2 −z2 ) i+ y 3 ( z 2−x 2 ) j+ z 3 ( x 2− y 2) k is Solenoidal
15. Check whether the vector field f =[ 2 x −3 yz , 2 y−3 xz ,2 z−3 xy ] is irrotational.
Experiment-9
Evaluating Line Integrals, Surface Integrals, Volume Integrals

MATLAB code for Line integral:


Example: To compute ∫ c F . Td S where F ( x , y , z )=(x ¿¿ 2 , z , y )¿
C :r ( t )=3 cos ( t ) i +2 sin ( t ) j+sin ⁡(2 t) k for 0 ≤ t ≤ 2 π .
⃗ ⃗ ⃗
Syntax:
clc
syms x, y, z
F=[x^2, z, y];
r = [3*cos(t), 2*sin(t), sin(2*t)];
FF= subs(F, [x, y, z]);;
g = inline(vectorize (sum(FF.*diff(r,t)), ‘t’)
quad(g, 0, 2*pi)

MATLAB code for Surface integral:


2 2 2
Example:To compute ∫ E F . nd S where S is the surface of ellipse x + y + z =1.
4 9 1
Syntax:
clc
syms x y z
ellipsoid = [2*sin(p)*cos(t), 3*sin(p)*sin(t); cos(p)];
F=[x, y, z];
nds = [3*cos(t)*sin(p)^2, 2*sin(p)^2*sin(t), 6*cos(p)*sin(p)*cos(t)^2+6*cos(p)*sin(p)*sin(t)^2]
subs(nds, [p, t], [pi/2, 0])
F par = subs(F, [x, y, z], ellipsoid)
flux= symint(realdot(Fpar, nds), p, theta, pi, t, theta, 2*pi)

Output: 24*pi

MATLAB code for Volume integral:


Example:
2 2 2
To compute ∫ V ÷FdV where V is the volume bounded by the ellipse x + y + z =1.
4 9 1
Syntax:
clc
syms x y z r
F=[x, y, z];
divF=div(F,[x, y, z])
divF=3
solid=[2*r*cos(t)*sin(p), 3*r*sin(p)*sin(t), r*cos(p)]
scale= simple(det(jacobian(solid,[r, p, t])))
scale= 6*r^2*sin(p)
Ans= int(symint 2(3*scale, r, theta, 1, p, theta, pi), t, theta, 2*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=
-------

Green’s Theorem in the plane


If ϕ ( x , y ) , ψ ( x , y ) , ϕ y ∧ψ x be continuous∈a region E of the
xy− plane bounded by a closed corve C then
∂ψ ∂ ∅
(
∫ c ( ϕ dx +ψ dy )= ∫ ❑ ∫ E )−
∂x ∂ y
dxdy

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:

You might also like