Manual For Experiential Learning Using Matlab: RV College of Engineering
Manual For Experiential Learning Using Matlab: RV College of Engineering
RV COLLEGE OF ENGINEERING®
(Autonomous Institution Affiliated to VTU, Belagavi)
DEPARTMENT OF MATHEMATICS
ENGINEERING MATHEMATICS - II
18MA21
MANUAL FOR
EXPERIENTIAL LEARNING
USING MATLAB
II - SEMESTER
Experiential Learning Department of Mathematics
Contents
Introduction 3
Modules 8
1 Multiple Integrals 8
2 Three-Dimensional Plots 11
5 Interpolation 29
6 Numerical Integration 32
Introduction
In Experiential Learning component of Engineering Mathematics - I, an introduction was given to MATLAB
software and modules were provided for basic concepts like arithmetic operations, math built-in functions,
plotting of curves, handling statistical data and operations of differential and integral calculus.
On successful completion of learning these modules, the process continues with Experiential Learning com-
ponent of Engineering Mathematics - II. A note on scripts and anonymous functions appear which are use-
ful in repeated usage of a set of MATLAB commands. Further a more rigorous approach is followed in
which the modules cover all the basic concepts of higher Engineering Mathematics like evaluating multi-
ple integrals, visualizing 3-D plots, solving higher order differential equations and implementing numerical
techniques on MATLAB.
It is ensured that after going through the MATLAB course as Experiential Learning of Engineering Mathe-
matics I and II, a student is well equipped to code any engineering problem successfully.
Scripts are the simplest type of program file. They are useful for automating a series of MATLAB com-
mands, such as computations that are performed repeatedly from the command line or series of commands
referenced.
This opens the MATLAB Editor window in which commands can be entered. After entering the commands,
save the script with a filename. The default MATLAB Script extension is ‘.m’.
After saving the script, the code can be run in the following ways:
1. Type the script name on the command line and press Enter. For example, to run a script titled
myMATLABProgram.m script, type myMATLABProgram.
2. Click the Run button on the Editor tab.
The following script calculates the area of a triangle with base ‘b’ and height ‘h’.
b = 5;
h = 3;
a = 0.5*(b*h)
This can be saved as triarea.m, run the script, by typing triarea on the MATLAB Command Prompt
or click the Run button on the Editor tab.
Writing the comment lines in MATLAB script will help the user to understand the code.
Add comments to MATLAB code using the percentage (%) symbol. Comment lines can appear anywhere in
a program file and comments can be appended at the end of a line of code.
%This script calculates the area of a triangle with base ‘b’ and height ‘h’
b = 5; % Base
h = 3; % Height
a = 0.5*(b*h) % Area
web(fullfile(docroot, ’matlab/scripts.html’))
Live Scripts
To combine code with embedded output, formatted text, equations and images in a single interactive envi-
ronment, create live scripts.
Live scripts are program files useful for interacting with a series of MATLAB commands and their output.
Live scripts contain output and graphics with the code that produced them, together in a single interactive
environment called the Live Editor. Add formatted text, images, hyperlinks and equations to live scripts
to produce an interactive narrative that can be shared with others. Live scripts are stored as files .mlx
extension.
To create a new live script, on the Home tab, in the New drop-down menu, select Live Script.
To obtain more information on Live Scripts, type the following in the Command Window:
Valid script names follow the same rules as variable names. It must start with a letter and can be followed
by letters, digits, or underscores.
MATLAB Functions
Both scripts and functions allow to reuse sequences of commands by storing them in program files. Scripts
are the simplest type of program, since they store commands exactly as it is typed at the command line.
Functions provide more flexibility, as primarily user can pass input values and return output values. For
example, the following function named fact computes the factorial of a number (n) and returns the result
(f).
function f = fact(n)
f = prod(1:n);
end
This type of function must be defined within a file, not at the command line. The best practice is to use the
same name for the function and the file (in this example, fact.m), since MATLAB associates the program
with the file name.
The function can be called from the command line, using the same syntax rules that apply to functions
installed with MATLAB. For instances, calculate the factorial of 5.
x = 5;
y = fact(x);
To create a MATLAB function, open the MATLAB Editor by typing edit in the Command Window.
The first line of every function is the definition statement, which includes the following elements.
The body of a function can include valid MATLAB expressions, control flow statements, comments, blank
lines and nested functions. Note that any variables can be created within a function and are stored within a
workspace specific to that function, which is separate from the base MATLAB workspace.
End Statements
Typically, functions end with an end statement at the end of the file. Although it is optional, use end for
better code readability.
Scripts vs Functions
For a comparison on scripts and functions and when to use them, go over the documentation in:
Anonymous Functions
What Are Anonymous Functions?
An anonymous function is a function that is not stored in a program file. It is associated with a variable
whose data type is function handle. Anonymous functions can accept inputs and return outputs, just
as standard functions do. However, they can contain only a single executable statement.
For example, create a function handle to an anonymous function that finds the square of a number:
Variable sqr is a function handle. The @ operator creates the handle and the parentheses () immediately
after the @ operator include the function input arguments. This anonymous function accepts a single input
x and implicitly returns a single output, an array the same size as x that contains the squared values.
Find the square of a particular value (5) by passing the value to the function handle, similar to passing an
input argument to a standard function.
a = sqr(5)
Many MATLAB functions accept function handles as inputs so that functions can be evaluated over a range
of values. Handles can be created either for anonymous functions or for functions in program files. The
benefit of using anonymous functions is that not to edit and maintain a file for a function that requires only
a brief definition.
where µ > 0 is a scalar parameter. Rewrite this equation as a system of first-order ODEs by making the
substitution y10 = y2 . The resulting system of first-order ODEs is
y10 = y2
y20 = µ(1 − y12 )y2 − y1 .
The function vdpeval represents the van der Pol equation using µ = 1. The variables y1 and y2 are the
entries y(1) and y(2) of a two-element vector, dydt.
Solve the ODE using the ode45 function on the time interval [0 20] with initial values [2 0]. The
resulting output is a column vector of time points t and a solution array y. Each row in y corresponds to a
time returned in the corresponding row of t. The first column of y corresponds to y1 and the second column
to y2 .
plot(t,y(:,1),’--’,t,y(:,2),’-.’)
title(’Solution of van der Pol Equation (\mu = 1) with ODE45’);
xlabel(’Time t’);
ylabel(’Solution y’);
legend(’y_1’,’y_2’)
1
Solution y
-1
-2
-3
0 5 10 15 20
Time t
Modules
1 Multiple Integrals
Topic learning outcomes:
Student will be able to:
1. Evaluate double and triple integrals.
2. Use them to determine area of plane figures, volumes of solids, etc.
syms x y a
int(int(exp(x/y),x,0,(yˆ2/a)),y,0,a)
ans =
aˆ2/2
x2 y2
x3 y dx dy where D is the region bounded by the ellipse
RR
Example 1.2: Evaluate D a2
+ b2
= 1 in the
first quadrant.
√
RaR b a2 −x2
3 x3 y dy dx .
RR a
Hint: D x y dx dy = 0 0
syms x y a b
I = int(int(xˆ3*y,y,0,(b/a)*sqrt(aˆ2-xˆ2)),x,0,a)
I =
(aˆ4*bˆ2)/24
syms x y
I = int(int(1,y,xˆ(3/2),xˆ(2/3)),x,0,1)
I =
1/5
Example 1.4: Find the volume bounded by the elliptic paraboloids z = x2 + 9y 2 and z = 18 − x2 − 9y 2 .
Hint: The two surfaces intersect on the elliptic cylinder z = x2 + 9y 2 and z = 18 − x2 − 9y 2 , i.e.,
x2 + 9y 2 = 9. The projection of this volume onto xy-plane is the plane region D enclosed by ellipse as
shown below:
syms x y z
int(int(int(1,z,xˆ2+9*yˆ2,18-xˆ2-9*yˆ2),y,-sqrt((9-xˆ2)/9),...
sqrt((9-xˆ2)/9)),x,-3,3)
ans =
27*pi
RR
Example 1.5: Evaluate R r sin θ dr dθ, where R is the region bounded by the cardioid r = a(1 − cos θ)
above the initial line.
RR R π R a(1−cos θ)
Hint: R r sin θ drd θ = 0 0 r sin θ dr dθ.
syms r theta a
int(int(r*sin(theta),r,0,a*(1-cos(theta))),theta,0,pi)
ans =
(4*aˆ2)/3
syms r phi z a
int(int(int(r,z,0,(aˆ2-rˆ2)/a),r,0,a*sin(phi)),phi,0,(pi/2))
ans =
(5*pi*aˆ3)/64
Exercise:
2. Find the mass, centroid and moments of inertia relative to x-axis, y-axis and origin of the plane region R
having mass density x + y and bounded by the parabola x = y − y 2 and the straight line x + y = 0.
2 Three-Dimensional Plots
Topic learning outcomes:
Student will be able to:
• fplot3(xt,yt,zt) - plots the parametric curve xt = x(t), yt = y(t) and zt = z(t) over the
default interval −5 < t < 5.
• fplot3(xt,yt,zt,[tmin tmax]) - plots xt = x(t), yt = y(t) and zt = z(t) over the interval
tmin < t < tmax.
• fplot3( ,LineSpec) - uses LineSpec to set the line style, marker symbol and line color.
• fplot3( ,Name,Value) - specifies line properties using one or more Name,Value pair ar-
guments. Use this option with any of the input argument combinations in the previous syntaxes.
Name,Value pair settings apply to all the lines plotted. To set options for individual lines, use the
objects returned by fplot3.
• plot3(X1,Y1,Z1,...), where X1, Y1 and Z1 are vectors or matrices - plots one or more lines
in three-dimensional space through the points whose coordinates are the elements of X1, Y1 and Z1.
• fsurf(f) - creates a surface plot of the symbolic expression f(x,y) over the default interval [-5 5]
for x and y.
• fsurf(f, [xmin xmax ymin ymax], LineSpec) - uses LineSpec to set the line style,
marker symbol and face color.
• fsurf( , Name,Value) - specifies line properties using one or more Name,Value pair argu-
ments. Use this option with any of the input argument combinations in the previous syntaxes.
• surf(Z) - creates a three-dimensional shaded surface from the z components in matrix Z, using
x = 1 : n and y = 1 : m, where [m, n] = size(Z). The height Z is a single-valued function defined
over a geometrically rectangular grid. Z specifies the color data, as well as surface height, so color is
proportional to surface height.
• surf(X,Y,Z) - uses Z for the color data and surface height. X and Y are vectors or matrices
defining the x and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m,
where [m, n] = size(Z). In this case, the vertices of the surface faces are (X(j), Y(i), Z(i,j))
triples. To create X and Y matrices for arbitrary domains, use the meshgrid function.
• surfc(Z) - creates a contour plot under the three-dimensional shaded surface from the z compo-
nents in matrix Z using x = 1 : n and y = 1 : m, where [m, n] = size(Z). The height Z is a single-valued
function defined over a geometrically rectangular grid. Z specifies the color data as well as surface
height. Therefore color is proportional to surface height.
• sphere - generates a sphere consisting of 20-by-20 faces.
• [X,Y,Z] = sphere(...) - returns the coordinates of the n-by-n sphere in three matrices that are
(n + 1)-by-(n + 1) in size. You draw the sphere with surf(X,Y,Z) or mesh(X,Y,Z).
• [X,Y,Z] = cylinder - returns the x-, y- and z- coordinates of a cylinder with a radius equal to
one. The cylinder has 20 equally spaced points around its circumference.
• [X,Y,Z] = cylinder(r) - returns the x-, y- and z- coordinates of a cylinder using r to define a
profile curve. cylinder treats each element in r as a radius at equally spaced heights along the unit
height of the cylinder. The cylinder has 20 equally spaced points around its circumference.
• [X,Y,Z] = cylinder(r,n) - returns the x-, y- and z- coordinates of a cylinder based on the
profile curve defined by vector r. The cylinder has n equally spaced points around its circumference.
• [x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr,n) - generates a surface mesh described by
three n + 1-by-n + 1 matrices, enabling surf(x,y,z) to plot an ellipsoid with center (xc, yc, zc)
and semi-axis lengths (xr, yr, zr).
• [x,y,z] = ellipsoid(xc,yc,zc,xr,yr,zr) - uses n = 20.
Note:
fmesh(f) also creates a mesh(surface) plot of the symbolic expression f (x, y) over the default interval
[-5 5] for x and y.
Example 2.1: Plot the 3-D parametric curve x = sin(t), y = cos(t), z = t over the default parameter range
[-5 5].
syms t
xt = sin(t);
yt = cos(t);
zt = t;
fplot3(xt,yt,t)
-5
1
0.5
0 0.5
0
-0.5 -0.5
Example 2.2: Define t as values between 0 and 10π. Define st and ct as vectors of sine and cosine values.
Plot a 3-D helix.
t = 0:pi/50:10*pi;
st = sin(t);
ct = cos(t);
figure
plot3(st,ct,t)
40
30
20
10
0
1
1
0 0.5
0
-0.5
-1 -1
Example 2.3: Plot the parametric curve x = e−t/10 sin(5t), y = e−t/10 cos(5t), z = t over the parameter
range [-10, 10].
syms t
xt = exp(-t/10).*sin(5*t);
yt = exp(-t/10).*cos(5*t);
zt = t;
figure
fplot3(xt,yt,zt,[-10 10])
10
-5
-10
2
2
0 1
0
-2 -1
-2
Example 2.4: Plot the 3-D parametric curve x = sin(t), y = cos(t), z = t three times over different
intervals of the parameter. For the first curve, use a Linewidth of 2. For the second, specify a dashed red
line style with circle markers. For the third, specify a cyan, dash-dot line style with asterisk markers.
syms t
figure
fplot3(sin(t), cos(t), t, [0 2*pi], ’LineWidth’, 2)
hold on
fplot3(sin(t), cos(t), t, [2*pi 4*pi], ’--or’)
fplot3(sin(t), cos(t), t, [4*pi 6*pi], ’-.*c’)
15
10
0
1
0 0.5
0
-0.5
-1
Example 2.5: Plot the surface x2 + y 2 + z 2 = 1, x, y, z > 0 using the colors in preset hot of colormap.
syms x y
figure
fsurf(sqrt(1-x.ˆ2-y.ˆ2))
colormap hot
0.5
0
1
1
0 0.5
0
-0.5
-1 -1
syms x y
figure
fsurf([x+y x-y])
10
-5
-10
5
5
0
0
-5 -5
figure
sphere
0.5
-0.5
-1
1
1
0 0.5
0
-0.5
-1 -1
Example 2.8: Define x, y and z as coordinates of a sphere. Plot a sphere centered at the origin. Plot two
more spheres centered at (3,-2,0) and (0,1,-3).
[x,y,z] = sphere;
figure
surf(x,y,z)
hold on
surf(x+3,y-2,z) % centered at (3,-2,0)
surf(x,y+1,z-3) % centered at (0,1,-3)
-1
-2
-3
-4
2
0 4
3
2
-2 1
0
-4 -1
figure
cylinder
0.8
0.6
0.4
0.2
0
1
1
0 0.5
0
-0.5
-1 -1
0.8
0.6
0.4
0.2
0
4
2 4
0 2
0
-2 -2
-4 -4
0.8
0.6
0.4
0.2
0
5
5
0
0
-5 -5
Example 2.12: Generate data for an ellipsoid with a center at (0,0,0) and semi-axis lengths (5.9,3.25,3.25).
Use surf to plot the ellipsoid.
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);
figure
surf(x, y, z)
axis equal
-2
2
0
0
-2
-5
2 2
Example 2.13: Plot the function z = xe−x −y on the domain −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2 . Use
meshgrid to define x and y. Then define z and create a surface plot.
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
Z = X.*exp(-X.ˆ2 - Y.ˆ2);
figure
surfc(X,Y,Z)
0.4
0.2
-0.2
-0.4
2
1 2
0 1
0
-1 -1
-2 -2
sin(r)
Example 2.14: Create a plot of the sinc function z = r .
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.ˆ2 + Y.ˆ2);
Z = sin(R)./R;
figure
surfc(Z)
0.5
-0.5
30
20 30
25
20
10 15
10
5
2
Example 2.15: Solution of heat conduction equation ∂u ∂ u
∂t = ∂x2 under the conditions u(0, t) = u(1, t) = 0
and u(x, 0) = sin πx, 0 ≤ x ≤ 1 is given in the following table using numerical method. Plot the solution
u(x,t).
x 0 0.2 0.4 0.6 0.8 1
t 0 1 2 3 4 5
0 0 0 0.5878 0.9511 0.9511 0.5878 0
0.02 1 0 0.4756 0.7695 0.7695 0.4756 0
0.04 2 0 0.3848 0.6225 0.6225 0.3848 0
0.06 3 0 0.3113 0.5036 0.5036 0.3113 0
0.08 4 0 0.2518 0.4074 0.4074 0.2518 0
0.1 5 0 0.2037 0.3296 0.3296 0.2037 0
x=0:0.2:1;
y=0:0.02:0.1;
z=[0,.5978,.9511,.9511,.5878,0;0,.4756,.7695,.7695,.4756,0;...
0,.3848,.6225,.6225,.3848,0;0,.3113,.5036,.5036,.3113,0;...
0,.2518,.4074,.4074,.2518,0;0,.2037,.3296,.3296,.2037,0];
figure
surf(x,y,z)
0.8
0.6
0.4
0.2
0
0.1
1
0.8
0.05 0.6
0.4
0.2
0 0
Exercise:
4. The transverse displacement u of a point at a distance x from one end and at any time t of a vibrating
2 2
string satisfies the equation ∂∂t2u = 4 ∂∂xu2 , with boundary conditions u = 0 at x = 0, t > 0 and initial
conditions u = x(4 − x) and ∂u ∂t = 0, 0 ≤ x ≤ 4. The transverse displacement u is given for one half period
of vibration, taking h = 1, k = 1/2 in the following table:
j\i 0 1 2 3 4
0 0 3 4 3 0
1 0 2 3 2 0
2 0 0 0 0 0
3 0 -2 -3 -2 0
4 0 -3 -4 -3 0
Matrices in MATLAB
A matrix is a two-dimensional array of real or complex numbers. Linear algebra defines many matrix oper-
ations that are directly supported by MATLAB. Linear algebra includes matrix arithmetic, linear equations,
eigenvalues, eigenvectors and etc.
• Square brackets - mark the beginning and the end of the matrix,
The following syntax can be used to define a matrix, where blank spaces are op-
tional (but make the line easier to read) and ” · · · ” denotes intermediate values: A =
[a11, a12, · · · , a1n; a21, a22, · · · , a2n; · · · ; an1, an2, · · · , ann]. A simpler syntax is available,
which does not require using the comma and semicolon characters. When creating a matrix, the
blank space separates the columns while the new line separates the rows, as in the following syntax:
A = [a11 a12 · · · a1n
a21 a22 · · · a2n
···
an1 an2 · · · ann].
This allows lightening considerably the management of matrices. Several MATLAB commands allow
creating matrices from a given size, i.e. from a given number of rows and columns.
A = [1, 2, 1; 4, −1, 1; 1, 6, 9]
B = [−8, 2, 1; 4, −1, 3; 4, 7, 9]
C =A+B
D =A−B
E =A∗B
F =B∗A
det(A)
inv(A)
rank(A)
eig(A)
[V, D] = eig(A)
size(A)
rref (A)
rref (B)
A=
1 2 1
4 −1 1
1 6 9
B=
−8 2 1
4 −1 3
4 7 9
C=
−7 4 2
8 −2 4
5 13 18
D=
9 0 0
0 0 −2
−3 −1 0
E=
4 7 16
−32 16 10
52 59 100
F =
1 −12 3
3 27 30
41 55 92
ans = −60
ans =
0.2500 0.2000 −0.0500
0.5833 −0.1333 −0.0500
−0.4167 0.0667 0.1500
ans = 3
ans =
−3.0000
2.0000
10.0000
V =
0.3277 −0.5774 0.1400
−0.8557 −0.5774 0.1400
0.4005 0.5774 0.9802
D=
−3.0000 0 0
0 2.0000 0
0 0 10.0000
ans =
3 3
ans =
1 0 0
0 1 0
0 0 1
ans =
1 0 0
0 1 0
0 0 1
Example 3.2: Check for consistency and obtain the solution if consistent for the following system of
equations x + y + z = 6, x + 2y + 3z = 10, x + 2y + 5z = 2.
A = [1, 1, 1; 1, 2, 3; 1, 2, 5]
B = [6; 10; 2]
[A, B]
rank(A)
rank([A, B])
linsolve(A, B)
A=
1 1 1
1 2 3
1 2 5
B=
6
10
2
ans =
1 1 1 6
1 2 3 10
1 2 5 2
ans = 3
ans = 3
ans =
−2
12
−4
Exercise:
1 2 −1 4
2 4 3 4
1. Find the rank of the matrix A =
1
2 3 4
−1 −2 6 −7
1 2 −1 4
2. Find the rank of the matrix M = 2
4 3 5
3 2 6 7
1 1 3
3. Find the inverse of the matrix S = 1 3 −3
−2 −4 −4
4. Verify the following system of equations for consistency and obtain the solution if consistent:
5. Verify the following system of equations for consistency and obtain the solution if consistent:
(a) x − z = 1, 3x + 2y + z = 1, 2x − y + z = 1
(b) x + y + z = 1, x + 3y − 2z = 1, 5x + 7y + 6z = 5
1 2
6. Find the eigenvalues and the eigenvectors of the matrix
3 2
6 −2 2
7. Find the eigenvalues and the corresponding eigenvectors of the matrix −2 3 −1
2 −1 3
syms y(x)
dsolve(2*xˆ2*diff(y, 2) + 3*x*diff(y) - y == 0)
ans =
C1/(3*x) + C2*xˆ(1/2)
Example 4.2: Solve the boundary value problem y 00 + 6y 0 + 9y = 2e−3x , y(0) = 0 and y(1) = 1.
syms y(x)
dsolve(diff(y, 2)+6*diff(y) +9*y == 2*exp(-3*x),y(0)==0,y(1)==1)
ans =
xˆ2*exp(-3*x) + x*exp(-3*x)*(exp(3) - 1)
Example 4.3: Obtain the solution of the differential equation (D4 − 1)y = cos 2x cosh x.
syms y(x)
simplify(dsolve(diff(y, 4)-y == cos(2*x)*cosh(x)))
ans =
Example 4.4: A 32 lb weight is suspended from a spring having constant 4 lb/ft. Find position of weight at
any time, if a force 16 sin 2t is applied and damping force is negligible. Assume that initially the weight is
at rest in the equilibrium position.
d2 x
dt2
+ 4x = 16 sin 2t; x(0) = 0, x0 (0) = 0.
syms x(t)
Dx=diff(x);
simplify(dsolve(diff(x, 2)+4*x == 16*sin(2*t),x(0)==0,Dx(0)==0))
ans =
2*sin(2*t) - 4*t*cos(2*t)
Example 4.5: A horizontal beam of length 2lf t is freely supported at both ends. Find the maximum
deflection if the load is w per unit length.
Hint:
Let P be any point (x, y) on the elastic curve. The total weight supported is 2lw. Hence the upward thrust
of the support at each end is lw. Consider the segment OP of the beam. The forces acting in this region are
upward thrust lw at zero and load wx acting at the midpoint of OP then the differential equation describing
this phenomena is
2
d y wx2
El dx 2 = 2 − wlx, under the boundary conditions y(0) = 0, y(2l) = 0.
syms y(x) E l w
dsolve(E*l*diff(y, 2) == (w*xˆ2/2)-w*l*x, y(0) == 0,y(2*l) == 0)
max_deflection=subs(ans,x,l)
ans =
(5*lˆ3*w)/(24*E)
Exercise:
1. Find the transient and steady state solutions of the mechanical system corresponding to the differential
2
equation ddt2x + 2 dx dx
dt + 2x = sin 2t − 2 cos 2t; x(0) = 0, dt (0) = 0.
2. In the case of a stretched elastic spring which has one end fixed and a particle of mass m attached at the
2
other end, the equation of motion is m ddt2x = − mg
e (x − l) where l is the natural length of the string and e is
the elongation due to weight mg. Find x under the condition that at t = 0, x = a and dx dt = 0.
3. The deflection of a strut of length l with one end (x = 0) built-in and the other supported subjected to
d2 y 2 a2 R dy
end thrust p satisfies the equation dx 2 + a y = p (l − x), y = 0, dx = 0 at x = 0. Find the deflection.
4. An electric circuit consists of an inductance of 0.05 Henrys, a resistance of 5 Ohms and a condenser
2
of capacitance 4 × 10−4 Farad and a constant emf of 110 Volts. Governing differential equation is ddtQ2 +
100 dQ
dt + 50000Q = 2200, under the conditions Q(0) = I(0) = 0. Determine charge Q(t).
5 Interpolation
Topic learning outcomes:
Student will be able to:
1. Interpolate from equally and unequally spaced domain points.
2. Extrapolate for evaluating points that lie outside the domain.
3. Use this concept to solve real life and engineering problems.
Method Description
‘linear’ Linear interpolation.
‘nearest’ Nearest neighbor interpolation.
‘next’ Next neighbor interpolation.
‘previous’ Previous neighbor interpolation.
‘pchip’ Shape-preserving piecewise cubic interpolation.
‘cubic’ Same as ‘pchip’.
‘spline’ Spline interpolation using not-a-knot end conditions.
Example 5.1: Using interpolation process find the value of sin 48o .
x = 0:5:90;
v = sind(x);
xq = 48;
vq1 = interp1(x,v,xq)
vq1 =
0.7425
Example 5.2: The following table gives the distances (in miles) of the visible horizon for the given heights
(in feet) above the earth’s surface.
x: 200 250 300 350 400
y = f (x) : 15.04 16.81 18.42 19.9 21.27
x = 200:50:400;
v = [15.04 16.81 18.42 19.9 21.27];
xq = [218 389];
vq1 = interp1(x,v,xq)
vq3 = interp1(x,v,xq,’spline’)
vq1 =
15.6772 20.9686
vq3 =
15.6977 20.9771
Example 5.3: The area A of a circle for different diameters d is given in the following table:
d: 80 85 90 95 100
A: 5026 5674 6362 7088 7854
x=[80 85 90 95 100];
y= [5026 5674 6362 7088 7854];
xq=105;
yq1=interp1(x,y,xq,’pchip’)
vq3 = interp1(x,y,xq,’spline’)
yq1 =
8.6589e+03
vq3 =
8663
Example 5.4: The following table gives the viscosity of oil as a function of temperature. Find the viscosity
of oil at a temperature of 140o .
yq1 =
7.0333
Exercise:
1. Estimate the population in 1895 and 1925 from the following statistics:
3. The pressure p of wind corresponding to velocity v is given in following table. Estimate p when v = 25,
35, 45.
Year v : 10 20 30 40
Population p : 1.1 2 4.4 7.9
4. A rod is rotating in a plane. The following table gives the angle θ (radians) through which the rod has
turned for various values of the time t second.
6 Numerical Integration
Topic learning outcomes:
Using MATLAB student will be able to:
R 0.6 2
Example 6.1: Evaluate 0 e−x dx.
q = integral(fun,0,0.6)
p = quadgk(fun,0,0.6)
q =
0.5352
p =
0.5352
Example 6.2: A solid of revolution is formed by rotating about the x−axis, the area between the x−axis,
the lines x = 0 and x = 1 and a curve through the point with the following coordinates.
x: 0.00 0.25 0.50 0.75 1.00
y: 1.0000 0.9896 0.9589 0.9089 0.8415
Estimate the volume of theRsolid formed.
1
Hint: Required volume = 0 πy 2 dx.
x = 0.00:0.25:1;
y = [1.0000 0.9896 0.9589 0.9089 0.8415];
Y = y.ˆ2;
V = pi*trapz(x,Y)
V =
2.8109
Example 6.3: A rocket is launched from the ground. Its acceleration is registered during the first 80 seconds
and is given in the table. Find the velocity of the rocket at t = 80s.
t: 0 10 20 30 40 50 60 70 80
a: 30 31.63 33.34 35.47 37.75 40.33 43.25 46.69 50.67
R 80
Hint: Velocity = 0 adt.
x = 0:10:80;
y = [30 31.63 33.34 35.47 37.75 40.33 43.25 46.69 50.67];
Q = trapz(x,y)
Q =
3.0879e+03
Numerical Integration: User defined functions for Trapezoidal rule, Simpson’s 1/3 rule and Simpson’s
3/8 rule.
Trapezoidal Rule
Rb
a ydx = h2 [y0 + 2(y1 + y2 + ... + yn−1 ) + yn ],
where
h = (b−a)n
i.e., the interval [a, b] is divided into n sub-intervals. Note that the indexing in MATLAB starts from 1 and
the last term in an array can be referred to using the index ’end’. We will need to modify the formula while
implementing to ensure that the indexing is correct, i.e., 0 is replaced by 1, 1 by 2 and so on and n is replaced
by ’end’.
I_trap = h/2*(y(1)+2*(sum(y(2:end-1)))+y(end))
I_simp13 = h/3*(y(1)+2*(sum(y(3:2:end-2)))+4*(sum(y(2:2:end-1)))+y(end))
I_simp38 = (3/8)*h*(y(1)+y(end)+2*(sum(y(4:3:end-3)))+3*(sum(y(2:end-1))...
-sum(y(4:3:end-3))))
x = [4.0,4.2,4.4,4.6,4.8,5.0,5.2];
y = [1.3863,1.4351,1.4816,1.5261,1.5686,1.6094,1.6487];
h = (5.2-4)/(length(x)-1);
I_trap = h/2*(y(1)+2*(sum(y(2:end-1)))+y(end))
I_trap =
1.8277
q = trapz(x,y)
q =
1.8277
I_simp13 = h/3*(y(1)+2*(sum(y(3:2:end-2)))+4*(sum(y(2:2:end-1)))+y(end))
I_simp13 =
1.8279
I_simp38 = (3/8)*h*(y(1)+y(end)+2*(sum(y(4:3:end-3)))+3*(sum(y(2:end-1))...
-sum(y(4:3:end-3))))
I_simp38 =
1.8278
Exercise:
R π/2 √
1. Evaluate 0 cos θdθ.
R 1.4
2. Evaluate 0.2 (sin x − log x + ex )dx.
3. A river is 80 feet wide. The depth d in feet at a distance x feet from one bank is given by:
x: 0 10 20 30 40 50 60 70 80
d: 0 4 7 9 12 15 14 8 3
4. A body of weight w tons is acted upon R by a variable force F tons weight. It acquires a speed of v mph
2 89.8
after travelling y yards where v = w F (y)dy. If w = 600 and F is given by the table below, estimate
the velocity at the end of 400 yards from the starting point.
1. Find the value of function (dependent variable) at different values of independent variable by numerically
solving the differential equation.
2. Write script and function files to solve application problems.
Note: Stiff differential equation - A differential equation of the form y 0 = f (t, y) is said to be stiff if its
exact solution y(t) includes a term that decays exponentially to zero as t increases, but whose derivatives
are much greater in magnitude than the term itself. An example of such a term is e−ct , where c is a large,
positive constant, because its k th derivative is ck e−ct . Because of the factor of ck , this derivative decays to
zero much more slowly than e−ct as t increases. Because the error includes a term of this form, evaluated
at a time less than t, the error can be quite large if h is not chosen sufficiently small to offset this large
derivative. Furthermore, the larger c is, the smaller h must be to maintain accuracy.
dy
Example 7.1: Solve the initial value problem dx = 2y + 3ex , y(0) = 0 for x = 0.1, 0.2, 0.3.
xspan = [0:0.1:0.3];
y0 = 0;
[x,y] = ode45(@(x,y) 2*y+3*exp(x), xspan, y0)
x =
0
0.1000
0.2000
0.3000
y =
0
0.3487
0.8113
1.4168
dy
Example 7.2: Solve the initial value problem dt = t2 y − 1, y(0) = 1 for t = 0.1 to 0.4.
tspan = [0:0.1:0.4];
y0 = 1;
[t,y] = ode23(@(t,y) tˆ2*y-1, tspan, y0)
plot(t,y,’-o’)
t =
0
0.1000
0.2000
0.3000
0.4000
y =
1.0000
0.9003
0.8023
0.7070
0.6151
0.9
0.8
y
0.7
0.6
0 0.1 0.2 0.3 0.4
t
Example 7.3: A 1500 gallon tank initially contains 600 gallons of water with 5 lbs of salt dissolved in it.
Water enters the tank at a rate of 9 gallon per hour and the water entering the tank has a salt concentration
of 15 (1 + cos t) lbs/gal. If a well mixed solution leaves the tank at a rate of 6 gal/hr. If amount of salt at any
time t is Q(t) then the IVP for this situation is dQ 2Q 9
dt + 200+t = 5 (1 + cos t), Q(0) = 5 and it overflows at t =
300 hrs. How much salt is in the tank when it overflows?
tspan = [0:150:300];
Q0 = 5;
[t,Q] = ode23(@(t,Q) (9*(1+cos(t))/5)-2*Q/(200+t), tspan, Q0)
t =
0
150
300
Q =
5.0000
171.1931
279.8302
This circuit can also be transformed into a parallel RLC circuit with a current source IS .
The voltage across the inductor and the capacitor are equal.
VL (t) = VC (t)
L dIdtL = VC (t)
dIL 1
dt = L VC (t)
IS = IR + IL + IC
Rearranging:
IC + IL + IR − IS = 0
C dVdtC + IL + VC
R − IS = 0
dVC IS IL VC
dt = C − C − RC
Assuming the initial conditions:
x1 (t) = IL (t), x2 (t) = VC (t)
dx1 1 1
dt = L VC (t) = L x2 (t)
dx2 dVC IS x1 x2
dt = dt = C − C − RC
Implementing this in a function RLC (found below) which is used in the following script:
t0 = 0; %Start Time
tf = 0.001; %Stop Time
x0 = [0 20]; %Initial Conditions for Capacitance Voltage and Inductor Curr
60
Capacitance Voltage
Inductor Current
40
20
-20
-40
0 0.2 0.4 0.6 0.8 1
Time -3
× 10
This function provides the xdot parameter for a second order RLC circuit
t is the time x is the initial condition (Capacitance voltage and inductor current) x is a 2 element vector -
x(1) is the capacitor voltage and x(2) is the inductor current
% Source Current
Is = 2;
%RLC values
R = 100;
L = 1e-3;
C = 1e-6;
%Calculating dx1/dt and dx2/dt and putting them into a column vector
xdot(1) = 1/L * x(2);
xdot(2) = 1/C * Is - 1/C*x(1) - 1/(R*C)*x(2);
xdot = xdot’;
end
Simple Pendulum
The equation of oscillation of a simple pendulum is given by:
d2 θ −g
dt2
= L sin θ
We will use the built-in function ode45 to solve the equation after setting up the differential equation in the
above form.
1.5
0.5
-0.5
-1
-1.5
-2
0 10 20 30 40 50
Exercise:
dy t3 −2y
1. Solve dt = t ,1 ≤ t ≤ 3 with y = 4.2 at t = 1.
2. An aeroplane uses a parachute and other means of braking as it slows down on the runway after landing.
Its acceleration is given by a = −0.0045v 2 − 3m/s2 . Since a = dv
dt , the rate of change of velocity is given
by:
dv
dt = −0.0035v 2 − 3.
Consider an aeroplane with a velocity of 300 km/h that opens its parachute and starts decelerating at t = 0s.
By solving the differential equation, determine and plot the velocity as a function of time from t = 0s until
the aeroplane stops.
dy y 2 −x2
3. Solve the initial value problem dx = y 2 +x2
with y(0) = 1 at x = 0.2, 0.4.
4. The number N of bacteria in a culture grew at a rate proportional to N. The value of N was initially 100
and increased to 332 in 1 hr. The governing equation is dN
dt = log(3.32)N, N (0) = 100. What is the value
of N at time t = 1.5 hr.
Example 8.1: Find all the roots of the equation x4 + 2x2 − 16x + 5 = 0.
syms x
vpasolve(xˆ4+2*xˆ2-16*x+5 == 0, x)
ans =
0.32653906234460422084857323401358
2.1111516462471155473107246160362
- 1.2188453542958598840796489250249 - 2.4015366984564279025134418606114i
- 1.2188453542958598840796489250249 + 2.4015366984564279025134418606114i
Example 8.2: Determine the root of the equation x2 − log x = 12 in (3, 4).
ans =
3.6460448399001736218717213181561
Example 8.3: The volume V of liquid in a spherical tank of radius r is related to the depth h of the liquid
2
by V = πh (3r−h)
3 . Obtain the roots of equation which give height h to which the dipstick immersed is wet
with liquid, given r = 1m and V = 0.5 m3 .
syms h
vpasolve(hˆ3-3*hˆ2+0.4775 == 0, h)
ans =
-0.37608006988252444956702991370319
0.43113799780627533182088418262644
2.9449420720762491177461457310768
Here radius of spherical tank is 1m, hence diameter = 2m. Therefore h = 0.4311m is the only possibility
for the given physical situation.
Example 8.4: The bacteria concentration c in a reservoir varies as t4 − t = c. Calculate the time t required
for the bacteria concentration to be c = 10, given that initial time is t = 2s.
syms t
vpasolve(tˆ4-t-10 == 0,t,[1,2.5])
ans =
1.855584528640937863760250564888
Exercise:
3. The current i in an electric circuit is given by i = 10e−t sin(2πt), where t is in seconds. Find the value
of t for i = 2 amp.
4. Water is falling in a trapezoidal channel at a rate of Q = 20m3 /s. The critical depth y for such a channel
2
must satisfy the equation 1 − QgAB3 = 0 where g = 9.81m/s2 , A is the cross sectional area in m2 and B is
the width of the channel at surface in m. For this case width and cross-sectional area can be related to depth
2
y by B = 3 + y and A = 3y + y2 . Solve it for critical depth.
R2016b
VISION
Disseminate Scientific and Engineering knowledge through quality teaching and
research partnership.
MISSION
Develop a spirit of competence to face challenges of the rapidly changing
teaching methodology.
Motivate faculty and scholars to acquire the latest skills for mathematical
modeling for modern technology application.
Develop positive attitude towards continuous learning.
Undertake inter-disciplinary research partnership.
Impart quality education through effective teaching process.
Provide extension programs for assisting individuals and organizations to find
solutions to engineering problems through consultation and research.
Impart skills on passing out-graduates to become excellent thereby enabling
them to make significant contribution in their chosen profession.
RV COLLEGE OF ENGINEERING ®
R. V. Vidyaniketan Post, Mysuru road
Tel: +91-80-67178021, 67178099 Fax: +91-80-67178011
www.rvce.edu.in