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

Manual For Experiential Learning Using Matlab: RV College of Engineering

Uploaded by

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

Manual For Experiential Learning Using Matlab: RV College of Engineering

Uploaded by

Advaith Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Rashtreeya Sikshana Samithi Trust

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

3 Elementary Linear Algebra 21

4 Higher Order Ordinary Differential Equations 26

5 Interpolation 29

6 Numerical Integration 32

7 Numerical Solution to Ordinary Differential Equations 36

8 Solution to algebraic and transcendental Equations 42

RV College of Engineering 2 Enjoy Learning...


Experiential Learning Department of Mathematics

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


MATLAB Scripts
To perform a sequence of commands repeatedly or to save for future reference, they are stored in a program
file. The simplest type of MATLAB program is a script, which contains a set of commands exactly as it is
typed at the command line.

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.

The new scripts are created in the following ways:

1. Click the New Script button on the Home tab.


2. Use the edit function by typing it in the MATLAB Command Prompt.

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.

Example for a MATLAB Script

The following script calculates the area of a triangle with base ‘b’ and height ‘h’.

b = 5;
h = 3;
a = 0.5*(b*h)

RV College of Engineering 3 Enjoy Learning...


Experiential Learning Department of Mathematics

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.

Entering Comments in a MATLAB Script

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

To obtain more information on Scripts, type the following in Command Window:

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:

web(fullfile(docroot, ’matlab/matlab prog/create-live-scripts.html’))

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

RV College of Engineering 4 Enjoy Learning...


Experiential Learning Department of Mathematics

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

Creating MATLAB Functions


Defining Functions

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.

1. The function keyword (required)


2. Output arguments (optional)
3. Function Name (required)
4. Input arguments (optional)
Valid function names follow the same rules as variable names. It must start with a letter and can be followed
by letters, digits, or underscores.

function [x,y] = myFunction(one,two,three)


This above function has the following elements:

1. Function Name - myFunction


2. Input Arguments - one, two, three
3. Output Argument - x, y
Contents of Functions and Files

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.

RV College of Engineering 5 Enjoy Learning...


Experiential Learning Department of Mathematics

Scripts vs Functions
For a comparison on scripts and functions and when to use them, go over the documentation in:

web(fullfile(docroot, ’matlab/matlab prog/scripts-and-functions.html’))

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:

sqr = @(x) x.ˆ2;

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.

Example of Anonymous Function with ODE45 solver


The van der Pol equation is a second order ODE

y100 − µ 1 − y12 y10 + y1 = 0,




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 .

RV College of Engineering 6 Enjoy Learning...


Experiential Learning Department of Mathematics

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 .

[t,y] = ode45(@vdpeval,[0 20],[2; 0]);

Plot the solutions for y1 and y2 against t.

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

Solution of van der Pol Equation ( µ = 1) with ODE45


3
y1
y2
2

1
Solution y

-1

-2

-3
0 5 10 15 20
Time t

function dydt = vdpeval(t,y)


%VDP1 Evaluate the van der Pol ODEs for mu = 1
% Jacek Kierzenka and Lawrence F. Shampine
dydt = [y(2); (1-y(1)ˆ2)*y(2)-y(1)];
end

Copyright 1984-2014 The MathWorks, Inc.

RV College of Engineering 7 Enjoy Learning...


Experiential Learning Department of Mathematics

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.

Syntax and description:


• int(int(expr, var1,a,b),var2,c,d) - computes double integral, i.e., integral of expr
with respect to var1 from a to b and then integral of resulting expression with respect to var2 from
c to d.
• int(int(int(expr, var1,a,b),var2,c,d),var3,e,f) - computes triple integral, i.e.,
integrates the expr with respect to var1 from a to b, var2 from c to d and then var3 from e to
f.
R a R y2 /a x/y
Example 1.1: Evaluate 0 0 e dx dy.

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)

RV College of Engineering 8 Enjoy Learning...


Experiential Learning Department of Mathematics

I =

(aˆ4*bˆ2)/24

Example 1.3: Find the area bounded by the curves y 2 = x3 and x2 = y 3 .


R 1 R x2/3
Hint: Area= 0 x3/2 dy dx.

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:

Volume bounded by elliptic paraboloids is,


q
R3 R 9−x2 R 18−x2 −9y2
V = q 9 dz dy dx.
−3 2 x2 +9y 2
− 9−x9

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

RV College of Engineering 9 Enjoy Learning...


Experiential Learning Department of Mathematics

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

R π/2 R a sin φ R (a2 −r 2 )


a
Example 1.6: Evaluate I = 0 0 0 r dz dr dφ.

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:

1. Determine the area bounded by the curves xy = 2, 4y = x2 and y = 4.

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.

3. Compute the volume of the ellipsoid of semi axes a, b, c.

z dx dy dz over the hemisphere x2 + y 2 + z 2 ≤ a2 , z ≥ 0 by changing to


RRR
4. Find the value of
spherical coordinate system.

RV College of Engineering 10 Enjoy Learning...


Experiential Learning Department of Mathematics

2 Three-Dimensional Plots
Topic learning outcomes:
Student will be able to:

1. Plot 3-D parametric curves.


2. Plot 3-D contours, surfaces, multiple surfaces.
3. Specify the properties: line properties, color data, surface height, axes label, title and marker symbol.
4. Use plots to visualize numerical data.

Syntax and description:

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

RV College of Engineering 11 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 12 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 13 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 14 Enjoy Learning...


Experiential Learning Department of Mathematics

Example 2.6: Plot the planes z = x + y and z = x − y in the same graph.

syms x y
figure
fsurf([x+y x-y])

10

-5

-10
5
5
0
0
-5 -5

Note: This can also be plotted using hold on.

Example 2.7: Plot a sphere.

figure
sphere

0.5

-0.5

-1
1
1
0 0.5
0
-0.5
-1 -1

RV College of Engineering 15 Enjoy Learning...


Experiential Learning Department of Mathematics

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

Example 2.9: Display unit cylinder.

figure
cylinder

0.8

0.6

0.4

0.2

0
1
1
0 0.5
0
-0.5
-1 -1

RV College of Engineering 16 Enjoy Learning...


Experiential Learning Department of Mathematics

Example 2.10: Plot a square box inside cylinder of radius 3.


figure
cylinder(3,100)
hold on
[x,y,z] = cylinder(3,4);
surf(x,y,z,’FaceColor’,’Green’)

0.8

0.6

0.4

0.2

0
4
2 4
0 2
0
-2 -2
-4 -4

Example 2.11: Generate a cylinder defined by the profile function 2 + cos(t).


t = 0:pi/10:2*pi;
figure
[X,Y,Z] = cylinder(2+cos(t));
surf(X,Y,Z)
axis square

0.8

0.6

0.4

0.2

0
5
5
0
0

-5 -5

RV College of Engineering 17 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 18 Enjoy Learning...


Experiential Learning Department of Mathematics

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)

RV College of Engineering 19 Enjoy Learning...


Experiential Learning Department of Mathematics

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:

1. The position of a moving particle as a function of time is given by x = (2 + 4 cos(t)) cos(t), y =


(2 + 4 cos(t)) sin(t), z = t2 . Plot the position of the particle for 0 ≤ t ≤ 20.

2. Draw the surface z = sin2 x + sin2 y; |x| ≤ π/2, |y| ≤ π/2.


−5
3. Plot of the function z = 1+x2 +y 2
, |x| ≤ 3, |y| ≤ 3.

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

Plot u(x, t).

RV College of Engineering 20 Enjoy Learning...


Experiential Learning Department of Mathematics

3 Elementary Linear Algebra


Topic learning outcomes:
Student will be able to:
1. Create and work with matrices.
2. Find the eigenvalues and eigenvectors of a square matrix.
3. Examine the consistency of system of linear equations.
4. Solve the system of linear equations.

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.

Introduction to matrices and commands connected to the matrices


Informally, the terms matrix and array are often used interchangeably. More precisely, a matrix is a two-
dimensional rectangular array of real or complex numbers that represents a linear transformation. The linear
algebraic operations defined on matrices have found applications in a wide variety of technical fields. (The
optional Symbolic Math Toolbox extends the capabilities of MATLAB to operations on various types of
nonnumeric matrices.) In MATLAB, every variable is treated as a matrix, i.e., if a variable a is stored the
value 10, then it is treated as 1 × 1 matrix.
The following is the list of symbols used to define a matrix:

• Square brackets - mark the beginning and the end of the matrix,

• Commas - separate the values in different columns,

• Semicolons - separate the values of different rows.

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.

RV College of Engineering 21 Enjoy Learning...


Experiential Learning Department of Mathematics

Matrix operators and element wise operators

+ Addition ·∗ Element wise multiplication


− Subtraction ./ Element wise right division
∗ Multiplication .\ Element wise left division
/ Right division .∧ Element wise power
\ Left division ., Transpose (but not conjugate)
∧ or ∗∗ power, i.e. xy , Transpose and conjugate

Syntax and description

size(A) returns an order of matrix


det(A) returns the determinant of square matrix A
inv(A) computes the inverse of square matrix A
rank(A) returns the rank of matrix A
eig(A) returns a column vector containing the eigenvalues of square matrix A
[V, D] = eig(A) returns diagonal matrix D of eigenvalues and matrix V whose columns
are the corresponding right eigenvectors
rref (A) produces the reduced row echelon form of A
linsolve(A, B) solves the linear system AX = B

Example 3.1: Create two matrices


 
1 2 1
A = 4 −1 1
1 6 9
 
−8 2 1
B= 4 −1 3
4 7 9
and addition of A and B, difference of A and B, product of A and B, determinant of A, inverse of A, rank
of A, eigenvalues and eigenvectors of A, row reduced echelon form of A and B.

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)

RV College of Engineering 22 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 23 Enjoy Learning...


Experiential Learning Department of Mathematics

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:

RV College of Engineering 24 Enjoy Learning...


Experiential Learning Department of Mathematics

 
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:

(a) x + y − 2z = 5, x − 2y + z = −2, −2x + y + z = 4


(b) x − z = 1, 2x + y + z = 2, y − z = 3, x + y + z = 4, 2y − z = 0

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

RV College of Engineering 25 Enjoy Learning...


Experiential Learning Department of Mathematics

4 Higher Order Ordinary Differential Equations


Topic learning outcomes:
Student will be able to:
1. Solve linear differential equations of second and higher order with constant coefficients.
2. Solve linear differential equations of second and higher order with variable coefficients.
3. Obtain solution of initial and boundary value problems.

Syntax and description:


• S = dsolve(eqn) - solves the ordinary differential equation eqn. Here eqn is a symbolic equa-
tion containing diff to indicate derivatives. Alternatively, you can use a string with the letter D
indicating derivatives.
• S = dsolve(eqn,cond) - solves the ordinary differential equation eqn with the initial or bound-
ary condition cond.
d y2 dy
Example 4.1: Solve 2x2 dx 2 + 3x dx − y = 0.

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 =

C5*cos(x) - (cos(2*x)*exp(x))/160 + C8*exp(x) - (3*sin(2*x)*exp(x))/160 ...


+ C6*sin(x) - (cos(2*x)*exp(-x))/160 + C7*exp(-x) + (3*sin(2*x)*exp(-x))/160

RV College of Engineering 26 Enjoy Learning...


Experiential Learning Department of Mathematics

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.

Hint: The differential equation describing this phenomenon is

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)

RV College of Engineering 27 Enjoy Learning...


Experiential Learning Department of Mathematics

ans =

(x*(8*w*lˆ3 - 4*w*l*xˆ2 + w*xˆ3))/(24*E*l)


max_deflection =

(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).

RV College of Engineering 28 Enjoy Learning...


Experiential Learning Department of Mathematics

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.

Syntax and description:


• yq = interp1(x,y,xq) - returns interpolated values of a 1-D function at specific query points
using linear interpolation. x contains the sample points and y contains the corresponding values,
y(x). xq contains the coordinates of the query points.
• yq = interp1(x,y,xq,method) - specifies an alternative interpolation method:
’nearest’, ’next’, ’previous’, ’linear’,’spline’,’pchip’, or ’cubic’.
The default method is ’linear’.
• yq = interp1(x,y,xq,method,extrapolation) - specifies a strategy for evaluating
points that lie outside the domain of x. Set extrapolation to ’extrap’ when you want to
use the method algorithm for extrapolation. Alternatively, you can specify a scalar value, in which
case, interp1 returns that value for all points outside the domain of x.

Interpolation method, specified as a value from the table below.

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 .

Hint: Define the sample points x and corresponding sample values v.

x = 0:5:90;
v = sind(x);
xq = 48;
vq1 = interp1(x,v,xq)

vq1 =

0.7425

RV College of Engineering 29 Enjoy Learning...


Experiential Learning Department of Mathematics

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

Find the value of y at x = 218 and 389 ft.

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

Calculate the area of a circle of diameter 105 units.

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

RV College of Engineering 30 Enjoy Learning...


Experiential Learning Department of Mathematics

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 .

Temp(degree): 110 130 160 190


Viscosity: 10.8 8.1 5.5 4.8

x=[110 130 160 190];


y=[10.8 8.1 5.5 4.8];
xq= 140;
yq1 = interp1(x,y,xq,’spline’)

yq1 =

7.0333

Exercise:

1. Estimate the population in 1895 and 1925 from the following statistics:

Year x : 1891 1901 1911 1921 1931


Population y : 46 66 81 93 101

2. Using interpolation find the value of e1.85 .

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.

t: 0 0.2 0.6 0.8 1.2


θ: 0 0.12 1.12 2.02 4.67

Calculate the angle θ at t = 0.4, 1, 1.3.

RV College of Engineering 31 Enjoy Learning...


Experiential Learning Department of Mathematics

6 Numerical Integration
Topic learning outcomes:
Using MATLAB student will be able to:

Evaluate definite integrals when


1. analytic method fails.
2. numerical data is known.

Syntax and description:


• q = integral(fun,xmin,xmax) - numerically integrates function fun from xmin to xmax
using global adaptive quadrature and default error tolerances.
• q = quadgk(fun,a,b) - attempts to approximate the integral of a scalar-valued function fun
from a to b using high-order global adaptive quadrature and default error tolerances.
• [q,errbnd] = quadgk(fun,a,b) - returns an approximate upper bound on the absolute error,
|q - I|, where I denotes the exact value of the integral.
• Q = trapz(Y) - returns the approximate integral of Y via the trapezoidal method with unit spacing.
• Q = trapz(X,Y) - integrates Y with spacing increment X. By default, trapz operates on the first
dimension of Y whose size does not equal 1. length(X) must be equal to the size of this dimension.
If X is a scalar, then trapz(X,Y) is equivalent to X*trapz(Y).

R 0.6 2
Example 6.1: Evaluate 0 e−x dx.

fun = @(x) exp(-x.ˆ2);

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.

RV College of Engineering 32 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 33 Enjoy Learning...


Experiential Learning Department of Mathematics

Simpson’s 1/3 Rule


Z b
h
ydx = [y0 + 2(y2 + y4 + ... + yn−2 ) + 4(y1 + y3 + ... + yn−1 ) + yn ]
a 3

I_simp13 = h/3*(y(1)+2*(sum(y(3:2:end-2)))+4*(sum(y(2:2:end-1)))+y(end))

Simpson’s 3/8 Rule


Z b
3
ydx = h[y0 + 2(y3 + y6 + ... + yn−3 ) + 3(y1 + y2 + y4 + y5 ... + yn−2 + yn−1 ) + yn ]
a 8

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

Example 6.4: Given that y = log(x)

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
R 5.2
Evaluate 4 log(x)dx using Trapezoidal rule, Simpson’s 1/3 rule and Simpson’s 3/8 rule and verify the
result for Trapezoidal rule using inbuilt function.

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

RV College of Engineering 34 Enjoy Learning...


Experiential Learning Department of Mathematics

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

Find approximately the area of cross section of the river.

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.

y: 0 50 100 150 200 300 400


f (y) : 90 62 45 34 26 15 8
R 400 R 200 R 400
Hint: 0 F dy = 0 F dy + 200 F dy with h1 = 50, h2 = 100.

RV College of Engineering 35 Enjoy Learning...


Experiential Learning Department of Mathematics

7 Numerical Solution to Ordinary Differential Equations


Topic learning outcomes:
Using MATLAB student will be able to:

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.

Syntax and description:


• [t,y] = ode45(odefun, tspan,y0), where tspan = [t0 tf] - integrates the system
of differential equations y 0 = f (t, y) from t0 to tf with initial conditions y0. Each row in the solution
array y corresponds to a value returned in column vector t. (Solves nonstiff differential equations
medium order method).
• [t,y] = ode23(odefun, tspan,y0), where tspan = [t0 tf] - integrates the system
of differential equations y 0 = f (t, y) from t0 to tf with initial conditions y0. Each row in the
solution array y corresponds to a value returned in column vector t. (Solves nonstiff differential
equations low order method).
• [t,y] = ode113(odefun, tspan,y0), where tspan = [t0 tf] - integrates the system
of differential equations y 0 = f (t, y) from t0 to tf with initial conditions y0. Each row in the
solution array y corresponds to a value returned in column vector t. (Solve nonstiff differential
equations variable order method).
• [t,y] = ode15s(odefun, tspan, y0), where tspan = [t0 tf] - integrates the sys-
tem of differential equations y 0 = f (t, y) from t0 to tf with initial conditions y0. Each row in the
solution array y corresponds to a value returned in column vector t. (Solve stiff differential equations
and differential-algebraic equations (DAEs) variable order method).

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

RV College of Engineering 36 Enjoy Learning...


Experiential Learning Department of Mathematics

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

RV College of Engineering 37 Enjoy Learning...


Experiential Learning Department of Mathematics

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

Higher Order Differential Equations - Application Examples


RLC Circuit
Consider the series RLC circuit below:

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.

RV College of Engineering 38 Enjoy Learning...


Experiential Learning Department of Mathematics

VL (t) = VC (t)
L dIdtL = VC (t)
dIL 1
dt = L VC (t)

By Kirchoff’s current law:

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

%Using ODE23 to solve the set of equations


[t,x] = ode23(@RLC,[t0,tf],x0);
plot(t,x);
legend(’Capacitance Voltage’,’Inductor Current’);
xlabel(’Time’)
ylabel(’Capacitance voltage and Inductor Current’);
function xdot = RLC(t,x)
Capacitance voltage and Inductor Current

60
Capacitance Voltage
Inductor Current
40

20

-20

-40
0 0.2 0.4 0.6 0.8 1
Time -3
× 10

RV College of Engineering 39 Enjoy Learning...


Experiential Learning Department of Mathematics

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 θ

where g is the gravitation constant and L is the length of the pendulum.

We will use the built-in function ode45 to solve the equation after setting up the differential equation in the
above form.

%========= Sets initial parameters for pendulum ===========================


g = 9.81; % Gravity (ms-2)
L = 4; % pendulum length (m)
initialangle1 = pi/2; % Initial angle 1
initialangle2 = 0; % Initial angle 2
runtime = 50; % Runs simulations for this time

RV College of Engineering 40 Enjoy Learning...


Experiential Learning Department of Mathematics

%============== Solves simple pendulum differential equations =============


deq1=@(t,x) [x(2); -g/L * sin(x(1))]; % Pendulum equations uncoupled
[t,sol] = ode45(deq1,[0 runtime],[initialangle1 initialangle2]);
% uses a numerical ode solver
plot(t,sol(:,1));

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.

RV College of Engineering 41 Enjoy Learning...


Experiential Learning Department of Mathematics

8 Solution to algebraic and transcendental Equations


Topic learning outcomes:
Student will be able to:

1. Find the roots of algebraic and transcendental equations numerically.

2. Solve wide variety of application problems.

Syntax and description:


• S = vpasolve(eqn) - numerically solves the equation eqn for the variable determined by
symvar.
• S = vpasolve(eqn,var) - numerically solves the equation eqn for the variable specified by
var.
• S = vpasolve(eqn,var,init guess) - numerically solves the equation eqn for the variable
specified by var using the starting point in init guess. If you do not specify var, vpasolve
solves for variables determined by symvar.
• S = vpasolve(eqn,var,[a,b]) - numerically solves the equation eqn for the variable spec-
ified by var using the search range specified in [a, b], where a and b are real numbers. If you do not
specify var, vpasolve solves for variables determined by symvar.

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

vpasolve(xˆ2-log(x) == 12, x,[3,4])

ans =

3.6460448399001736218717213181561

RV College of Engineering 42 Enjoy Learning...


Experiential Learning Department of Mathematics

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:

1. Obtain all the roots of the equation x6 − x4 − x3 + 1 = 0.

2. Determine the root of the equation tan x + tanh x = 0 in (2, 3).

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.

Published with MATLAB


R

R2016b

RV College of Engineering 43 Enjoy Learning...


DEPARTMENT OF MATHEMATICS

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.

Rashtreeya Sikshana Samithi Trust

RV COLLEGE OF ENGINEERING ®
R. V. Vidyaniketan Post, Mysuru road
Tel: +91-80-67178021, 67178099 Fax: +91-80-67178011
www.rvce.edu.in

You might also like