Sunteți pe pagina 1din 15

Lab Manual-MAT1002 with MATLAB

Applications of Differential Equations and


Difference
Department of Mathematics
Vellore Institute of Technology Andhra Pradesh
Amaravati
February 2, 2019
Contents
0.1 MATLAB Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
0.2 MATLAB Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
0.3 MATLAB Syntax and Commands . . . . . . . . . . . . . . . . . . . . 3

1 Solution of difference equations using Z-transform 4


1.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Mathematical Form: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 MATLAB Syntax Used: . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Examples: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Steps to solve difference equation: Using Symbolic Workflows . . . . . 5
1.6 Rabbit Population . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 MATLAB Code for Solving Rabbit Growth problem . . . . . . . . . . 5
1.8 Exercise: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Applications of Second Order Differential Equations 7


2.1 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Mathematical Form: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 MATLAB Syntax Used: . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 MATLAB Code for Solving Second Order Ordinary Differential Equa-
tions: Method of Variation of Parameter . . . . . . . . . . . . . . . . 7
2.5 MATLAB Code Using dsolve Command . . . . . . . . . . . . . . . . . 8
2.6 Examples: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 Modeling of Physical Problem: Electrical Series Circuit . . . . . . . . 9

3 Fourier Series 9
3.1 MATLAB Syntax Used: . . . . . . . . . . . . . . . . . . . . . . . . . . 10

i
Preface
This lab manual is intended for the undergraduate students for better understanding
of the analytical solutions with graphical visualization. There are several softwares
available for solving these problems numerically, and a few are, MATLAB, MATH-
EMATICA, MAPLE, PYTHON, SCILAB, FORTRAN, C, C++. Here, we restrict
ourselves to MATALB as it has many essential tools and packages. Many of the re-
searchers, academic institutes, companies that deals with computer vision, industries,
financial companies and many other use MATLAB either for simulations or modeling
and solving real world problem. The manual is restricted to the MAT1002 theory
course and solve problems based on the content provided in MAT1002.

In the first chapter, we present introduction MATLAB in a systematic way. The


authors assumed that the reader has no prior knowledge on MATLAB and framed
the content accordingly. The basic structure of MATLAB, syntax, tools are provided
in the introduction. In later chapters, we use these tools to solve a few problems
of MAT1002 course, however, the students are not restricted to solve the problems
in this particular course. The enthusiastic scholar can broaden the programming
techniques in solving research and industry problems with MATLAB. The commands
which are developed in this manual are best suits to solve problems in the MAT1002
course using MATLAB. The manual is prepared followed by the theory classes and
the chapters are in each module, the necessary commands and syntax is provided.
However, the motivated students may learn other tools and techniques for better
programming writing skills and encourage to develop their own tools and program.

Note to Students
This is meant for an introduction course to the undergraduate students on the theory
point of view. Those who are much excited to learn other MATLAB tools are sug-
gested to refer the following books. Finally, If you spot any errors, typos, please drop
me an email (manoj.m@vitap.ac.in„ ramakrishnareddy.reddy@vitap.ac.in, v.marka@vitap.ac.in)
and I will do my best in correcting the manual and make those changes in subsequent
printings and editions.Millions of thanks to you for your help in this matter.

Dr. VenkatRajam Marka


Dr. Manoj Kumar Mishra
Dr. V R K Reddy

ii
0.1 MATLAB Functions
MATLAB provides many matrix functions for various matrix/vector manipulations;
see Table 3.3 for some of these functions. Use the online help of MATLAB to and
how to use these functions.

Command Explanation
inv inv(A) returns inverse of matrix A.
round Y = round(X,N) rounds X to N digits
\ X = A\B solves the system of linear equations AX =
B. The matrices A and B must have the same number
of rows.
det det(A), returns the determinant of matrix A
diag diag(A), returns an equivalent diagonal matrix of A
eig [V,D] = eig(A) returns matrices V and D. The columns
of V present eigenvectors of A. The diagonal matrix D
contains eigenvalues.
null Z = null(A) returns an orthonormal basis for the null
space of A.

0.2 MATLAB Code

% Program for solving system of second order differential equation


clc
clear all
close all
syms x1(t) x2(t)
A=[-10 4;4 -4]
Dx1=diff(x1);
Dx2=diff(x2);
cond10=x1(0)==0;
cond11=Dx1(0)==1;
cond20=x2(0)==0;
cond21=Dx2(0)==-1;
cond=[cond10 cond11;cond20 cond21];
[P lambda] = eig(A);
P=round(P,5)
fprintf(’Eigen values of A are %f, %f, %f \n’,lambda);
disp(’The Modal Matrix is: ’);
disp(P);
if (rank(P)˜=length(P))
fprintf(’The matrix is not diagonlizable, thus solution is not possible
using this method \n’)
return
end

1
D = inv(P)*A*P;
D=round(D,5)
X = [x1(t);x2(t)];
for i=1:length(A)
eqs=diff(X(i),t,2) - D(i,i)*X(i) == 0
bc=cond(i,:)
Sol(i)= dsolve(eqs,bc)
end
disp(’The solution of the system diff(X,2)+DX=0 is: ’);
disp(Sol);
disp(’The Solution of the given system is: ’);
Y = P*Sol’ %[Sol1; Sol2]

fplot(Y,[0 15])

2
0.3 MATLAB Syntax and Commands

Command Explanation
syms var1 · · · varn Creates symbolic variables var1 , · · · , varn . Separate variables by
spaces.
subs(f,old, new) returns a copy of f , replacing all occurrences of old with new, and
then evaluates f.
simplify(A) It performs algebraic simplification of A. If A is a symbolic vector
or matrix, this function simplifies each element of A.
solve(eqn,var) Solves the equation eqn for the variable var. If you do not specify
var, the symvar function determines the variable to solve for. For
example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for
x.
diff(y,var,n) Computes the nth derivative of y with respect to the variable var.
dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equa-
tion. Use diff and == to represent differential equations. For ex-
ample, diff(y,x) == y represents the equation dy/dx=y. Solve
a system of differential equations by specifying eqn as a vector of
those equations
dsolve(eqn,cond) solves eqn with the initial or boundary condition cond.
besselj(n, x) Computes the Bessel function of the first kind, Jn (x), for each
element of the array x.
besselj(n, x, 1) Computes besselj(n, x). ∗ exp(−abs(imag(x))).

bessely(n, x) Computes the Bessel function of the second kind, Yn (x), for each
element of the array x.

besseli(n, x) Computes the modified Bessel function of the first kind, In (x), for
each element of the array x.

besselk(n, x) Computes the modified Bessel function of the second kind, Kn (x),
for each element of the array x.

We now demonstrate the plots of Bessel functions of first kind for different orders,
namely, n = 0, 1, 2, 3, 4, that is plots of J0 (X), J1 (X), J2 (X), J3 (X), J4 (X).
We can also plot for Bessel functions of second kind (Yn (X)), modified Bessel func-
tions of first kind (In (X)), and the modified Bessel function of second kind (Kn (X)).
https://in.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html

3
1 Solution of difference equations using Z-transform
1.1 Objective
• To solve and visualize difference equation using Z- transform in MATLAB.

1.2 Mathematical Form:


The Z− transform of an expression f = f (n) is defined as follows:

X f (n)
F (z) = (1)
n=0
zn

and Inverse z-transform Z−transform of the function is defined is given by the formula
I
1 F (z)
f (n) = Z −1 (F (z)) = dz (2)
2πj C z n−1)

1.3 MATLAB Syntax Used:

Command Explanation
ztrans(f) finds the Z−Transform of f . By default, the independent variable
is n and the transformation variable is z. If f does not contain n,
ztrans uses symvar.
ztrans(f,var) uses the transformation variable var instead of z.
ztrans(f,var1,var2) uses the independent variable var1 and transformation variable
var2 instead of n and z, respectively.
siztrans(f) returns the Inverse Z− Transform of f . By default, the indepen-
dent variable is z and the transformation variable is n. If f does
not contain z, iztrans uses the function symvar.
iztrans(f,var) uses the transformation variable var instead of n.
iztrans(f,var1,var2) uses the independent variable var1 and transformation variable
var2 instead of z and n respectively.
stem(y) plots the data sequence, y, as stems that extend from a base-
line along the x−axis. The data values are indicated by circles
terminating each stem.

1.4 Examples:
Example 1 Compute the Z−transform of sin(n). By default, the transform is in
terms of z.

syms n
f = sin(n);
ztrans(f)

>> ans = (z*sin(1))/(zˆ2 - 2*cos(1)*z + 1)

4
Example 2 Compute the Z−transform of Compute the Z-transform of the Heaviside
function.

syms n z
ztrans(heaviside(n-3),n,z)

>> ans = (1/(z - 1) + 1/2)/zˆ3

Example 3 Compute the inverse Z−transform of 2z/(z − 2)2 .

syms z
F = 2*z/(z-2)ˆ2;
iztrans(F)

>> ans = 2ˆn + 2ˆn*(n - 1)

1.5 Steps to solve difference equation: Using Symbolic Work-


flows
In order to solve a difference equation using Z− transform we use symbolic compu-
tation platform of MATLAB. Typically, the steps are:
1. Define difference equations.
2. Find the Z− transform of difference equation.
3. Solve for F(Z).
4. Find inverse Z− transform.
5. plot the function.
6. Analyze results.

1.6 Rabbit Population


1.7 MATLAB Code for Solving Rabbit Growth problem

%Program for finding rabbit population in n months


%using Z-transform
syms f(n) z F
assume(n>=0 & in(n,’integer’))
eq = f(n+2) - f(n+1) - f(n) % Define difference equation
Zt = ztrans(eq,n,z) % Z-transform of difference equation
Zt = subs(Zt,ztrans(f(n),n,z),F)
F = solve(Zt,F) % Solve for transformed dependent variable F(z)
pSol = iztrans(F,z,n); % Inverse Z-transform
pSol = simplify(pSol)
pSol = subs(pSol,[f(0) f(1)],[1 1]) % Initial conditions
nvalues = 1:10; % Range values of n

5
pSolValues = subs(pSol,n,nvalues);
pSolValues = double(pSolValues);
pSolValues = real(pSolValues);
stem(nvalues,pSolValues)
title(’Rabbit Population’)
xlabel(’Years (n)’)
ylabel(’Population f(n)’)
grid on

1.8 Exercise:
1. A bank account gives an interest rate of 5% compounded monthly. If you invest
invest initially Rs. 1000, and add Rs. 10 every month. How much money do
you have after 5 years?
2. Determine the probability to flip a coin n times and have no successive heads

6
2 Applications of Second Order Differential Equa-
tions
2.1 Objective
• To acquire skills in solving some specific second order differential equations.
• To apply relevant skills of forming and solving second order differential equa-
tions in some given physical situations using MATLAB.

2.2 Mathematical Form:


Many different physical systems can be described by a linear second-order differential
equation:
d2 x dx
P (t) 2 + Q(t) + R(t)x = f (t) (3)
dt dt
where, P (t), Q(t), R(t) are constants and f (t) is known as force term.

2.3 MATLAB Syntax Used:

Command Explanation
syms var1 · · · varn Creates symbolic variables var1 , · · · , varn . Separate variables by
spaces.
subs(f,old, new) returns a copy of f , replacing all occurrences of old with new, and
then evaluates f.
simplify(A) It performs algebraic simplification of A. If A is a symbolic vector
or matrix, this function simplifies each element of A.
solve(eqn,var) Solves the equation eqn for the variable var. If you do not specify
var, the symvar function determines the variable to solve for. For
example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for
x.
diff(y,var,n) Computes the nth derivative of y with respect to the variable var.
dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equa-
tion. Use diff and == to represent differential equations. For
example, diff(y,x) == y represents the equation dy/dx=y. Solve
a system of differential equations by specifying eqn as a vector of
those equations
dsolve(eqn,cond) solves eqn with the initial or boundary condition cond.

2.4 MATLAB Code for Solving Second Order Ordinary Dif-


ferential Equations: Method of Variation of Parameter

% Program for solving differential equation of the form


% ay"+by’+cy=f(t), for a, b and c as constants.

7
% Using Variation of Parameter
clear all
close all
clc
syms A B t m
p=input(’Enter the coefficients a,b,c inside a big bracket ’);
f=input(’Enter the RHS function f(x)’);
a=p(1);b=p(2);c=p(3);
disc=bˆ2-4*a*c;
m=subs(solve(a*mˆ2+b*m+c) );
if(disc>0)
CF= A*exp(m(1)*t)+B*exp(m(2)*t);
u=exp(m(1)*t); v=exp(m(2)*t);
elseif (disc==0)
CF=(A+B*t)*exp(m(1)*t);
u=exp(m(1)*t); v=t*exp(m(1)*t);
else
alfa=real(m(1)); beta=imag(m(1));
CF=exp(alfa*t)*(A*cos(beta*t)+B*sin(beta*t));
u=exp(alfa*t)*cos(beta*t); v=exp(alfa*t)*sin(beta*t);
end
% Method of variation of parameters.
f1=f/a;
Wronsikian=u*diff(v,t)-diff(u,t)*v; %Jacobian of u and v
P=int(-v*f1/Wronsikian,t);
Q=int(u*f1/Wronsikian,t);
PI=P*u+Q*v; y_gen=CF+PI;
dy_gen=diff(y_gen);
cond=input(’Enter the initial conditions t_0, y(t_0) and Dy(t_0)’);
eq1=(subs(y_gen,t,cond(1))-cond(2));
eq2=(subs(dy_gen,t,cond(1))-cond(3));
[A B]=solve(eq1,eq2);
y=subs(CF+PI)
fplot(y)

2.5 MATLAB Code Using dsolve Command

clear all
clc
eqn = input(’Enter the Differential Equation as a string: ’);
%Enter the input within quotes like ’D2y+5*Dy+6*y=cos(x)’
inits = input(’Enter the initial condition as a string: ’);
%Enter the initial condition within quotes like ’y(0)=0,Dy(0)=1’
y=dsolve(eqn,inits,’x’)
fplot(y)

8
2.6 Examples:
Example 1: Solve the differential equation

d2 y dy
−5 + 6y = sin(3t)
dt2 dt

Inputs:
Enter the coefficients a,b,c inside a big bracket: [1 -5 6]
Enter the RHS function f(x): sin(3*t)
Enter the initial conditions t_0, y(t_0) and Dy(t_0): [0 0 1]

Output:
y = 5*cos(3*t))/78 - (16*exp(2*t))/13 + (7*exp(3*t))/6 -sin(3*t)/78

Example 2: Solve the following initial value problem

d2 y
+ 9y = sin(2t), y(0) = 0, y 0 (0) = 1
dt2

Inputs:
Enter the coefficients a,b,c inside a big bracket: [1 0 9]
Enter the RHS function f(x): sin(2*t)
Enter the initial conditions t_0, y(t_0) and Dy(t_0): [0 0 1]
Output:
y =sin(3*t)/15 + (4*sin(3*t)*(10*tan(t/2)ˆ2 - 20*tan(t/2)ˆ4
+ 30*tan(t/2)ˆ6 - 5*tan(t/2)ˆ8 + 1))/(15*(tan(t/2)ˆ2 + 1)ˆ5)
+ (2*cos(3*t)*sin(t)ˆ3*(4*sin(t)ˆ2 - 5))/15

clc
Clear all
Syms x p n
N = input( Enter the number of terms up to which you want evaluate the
series )
N = n-1;
Disp( Note : Input the coefficients of the Diff Equation as constant function
x)
P(1) = input( The coefficient of D2y: );
P(2) = input( The coefficient of Dy: );
P(3) = input( The coefficient of y: );
For I = 0:n
Tmp = strcat( a , num2str(i) );
A(i+1) = sym(tmp);
End

Y = sum(a.*x.ˆ(0:n));
Ps = collect(p(1)*diff(y,2) + p(2)*diff(y,1) + p(3)*y*x);
Psc = coeffs(ps,x);
I1 = input( Enter the first initial values:);
I1 = input( Enter the first initial values:);

9
Ics1 = strcat( a0 = ,num2str(i1) );
Ics2 = strcat( a1 = ,num2str(i2) );
Sol = vpa(subs(y,solve(ics1,ics2,psc(1:n-1)))),3);
Disp( The solution of the given differential equation is:);
Disp(sol);
Ezplot(sol,[-4,4]);

2.7 Modeling of Physical Problem: Electrical Series Circuit


If i(t) denotes current in the LRC-series electrical circuit shown in figure,
q
C.
By Kirchhoff’s second law, the sum of these voltages equals the voltage E(t)
impressed on the circuit; that is,
di 1
L + R i + q = E(t) (4)
dt C
dq
But the charge q(t) on the capacitor is related to the current i(t) by i = dt, and so
(2) becomes the linear second-order differential equation

d2 q dq 1
L + R + q = E(t) (5)
dt2 dt C

3 Fourier Series
Aim: To generate Fourier Series of a given function and its visualization by the
plotting.
MATHEMTAICAL FORMULATION:
The Fourier series of a 2l periodic function f (x) defined on the integral [L,U=L+2l]
is given by
∞  
1 X k πx k πx
f (x) = a0 + ak cos( ) + bk sin( ) (6)
2 l l
k=1

and the constant coefficients ak , bk are given by


Z U
1 k πx
ak = f (x) cos( ) dx, k = 0, 1, 2, 3.. (7)
l −L l
Z U
1 k πx
bk = f (x) sin( ) k = 1, 2, 3..dx. (8)
l −L l

The nth partial sum of the Fourier Series is:

n  
a0 X k πx k πx
Sn (x) = + ak cos( ) + bk sin( ) (9)
2 l l
k=1

10
3.1 MATLAB Syntax Used:

Command Explanation
syms var1 · · · varn Creates symbolic variables var1 , · · · , varn . Separate variables by
spaces.
disp(X) Displays the content of X without printing variable name.
simplify(A) It performs algebraic simplification of A. If A is a symbolic vector
or matrix, this function simplifies each element of A.
solve(eqn,var) Solves the equation eqn for the variable var. If you do not specify
var, the symvar function determines the variable to solve for. For
example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for
x.
diff(y,var,n) Computes the nth derivative of y with respect to the variable var.
dsolve(eqn) solves the differential equation eqn, where eqn is a symbolic equa-
tion. Use diff and == to represent differential equations. For
example, diff(y,x) == y represents the equation dy/dx=y. Solve
a system of differential equations by specifying eqn as a vector of
those equations
dsolve(eqn,cond) solves eqn with the initial or boundary condition cond.

clc
clear all
syms x k L U n
f = input(’Enter the function:’);
L = input(’Enter the lower limit:’);
U = input(’Enter the upper limit:’);
l = (U-L)/2;
n = input(’Enter the number of the terms required:’);

ak = @(f,x,k) int(f*cos(k*pi*x/l)/l,x,L,U);
bk = @(f,x,k) int(f*sin(k*pi*x/l)/l,x,L,U);
fs = @(f,x,n) ak(f,x,0)/2 + ...
symsum(ak(f,x,k)*cos(k*pi*x/l) + bk(f,x,k)*sin(k*pi*x/l),k,l,n );
pretty(fs(f,x,n))

fst = ak(f,x,0)/2;
for i = 1:n
fst = fst + ak(f,x,i)*cos(i*pi*x/l) + bk(f,x,i)*sin(i*pi*x/l);
disp([’Harmonics upto:’,num2str(i)]);
disp(fst)
h = ezplot(f,[L,U])
set(h,’LineWidth’,1.5)
hold on
h = ezplot(fst,[L,U]);
set(h,’LineStyle’,’-’,’Color’,[i/n,1/n, 1/n],’LineWidth’,1.5);
title([’Partial sums up to n=’, sum2str(i)])

11
end

12

S-ar putea să vă placă și