You are on page 1of 18

Advanced Engg Mathematics

DE ZG 535

BITS Pilani Lab session


Pilani Campus
𝒅𝒚 Linear First order- 𝟐
=𝟑General
∗ 𝒙 +𝟑
solution
𝒅𝒙

syms y(x)
eqn = diff(y) ==
3*x^2+3
ysol=dsolve(eqn)
Output:
ysol =

x^3 + 3*x + C

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 2


𝒅𝒚 𝟐
=𝟑 ∗ 𝒙 + 𝟑
Linear First order- IVP
𝒅𝒙
Family of solutions
syms y(x)
eqn = diff(y) == 3*x^2+3
for c=2:8
cond= y(0)==c;
ysol=dsolve(eqn,cond);
fplot(ysol,[-2 2])
hold on
end
grid on

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 3


( )
𝟐
𝒅𝒚
+𝒚 =𝟏 ; 𝒚 ( 𝟎 ) =𝟎 ;
𝒅𝒕
Non- Linear First
syms y(t) order, IVP
eqn = (diff(y)+y)^2==1;
cond=y(0)==0;
ysol =dsolve(eqn,cond)

Output:
ysol =
exp(-t) - 1
1 - exp(-t)

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 4


( )
𝟐𝒚 ❑
𝒅 𝒅𝒚 ′
𝟒 ∗ 𝟐 +𝟒 ∗ +𝟏𝟕∗ 𝒚=𝟎 ; 𝒚 ( 𝟎 )=−𝟏 ; 𝒚 ( 𝟎 )=𝟐
𝒅𝒕 𝒅𝒕

syms y(x) Linear Homogenous


Dy=diff(y); Second order DE
eqn = diff(y,x,2)==(-4*Dy-17*y)/4;
cond1= y(0)==-1;
cond2=Dy(0)==2;
sol=dsolve(eqn,cond1,cond2)
ezplot(sol)
grid on

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 5


( )
𝟑 𝟐 ❑
𝟑 𝒅 𝒚 𝟐 𝒅 𝒚 𝒅𝒚
𝒙 ∗ +𝟓 ∗ 𝒙 ∗ +𝟕 ∗ 𝒙 ∗ +𝟖∗ 𝒚 =𝟎
𝒅𝒙
𝟑
𝒅𝒙
𝟐
𝒅𝒙 Cauchy-Euler
Second order DE
syms y(x)
eqn= x^3*diff(y,x,3)+5*x^2*diff(y,x,2)+7*x*diff(y)
+8*y==0;
dsolve(eqn)

Or simply type

dsolve('x^3*D3y+5*x^2*D2y+7*x*Dy+8*y=0',"x")

Note: Default independent variable in this form is “t”,


if you need to change to x, you must declare it as seen
in the code (“x”)

Output:

ans = C56*cos(2*log(x)) - C55*sin(2*log(x)) + C57/x^2

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 6


( )

𝒅𝒙 −𝟑
=𝒌∗ 𝒙 ∗(𝟏𝟎𝟎𝟎− 𝒙) 𝒌=𝟎.𝟗𝟗∗𝟏𝟎 𝒂𝒏𝒅 𝒙 ( 𝟎 ) =𝟏;
𝒅𝒕
ODE45 Built
tspan = [0 10]; in solver
y0 = 1;
[t,y] = ode45(@(t,y) 0.9906*(10^-3)*y*(1000-y), tspan, y0);
plot(t,y,'-*')
[t,y]

Non – Linear,
First order

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 7


Systems of Linear First Order DE

syms x1(t) x2(t)


eqn1= diff(x1)==-2/25*x1 +(1/50)*x2;
eqn2= diff(x2)== 2/25*x1 -(2/25)*x2;
[x1,x2]= dsolve(eqn1,eqn2,'x1(0)=25','x2(0)=0')
t=0:1:50;
fplot(x1,[0 50])
hold on Systems of Linear First
fplot(x2,[0,50]) order Differential
equations

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 8


Systems of Linear First Order DE – Contd..

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 9


Direction field
[x,y]= meshgrid(-5:1:5,-5:1:5);
Direction Field
dy= 0.2.*x.*y;
dx= ones(size(dy));
dyu = 0.25*(dy./sqrt(dx.^2+dy.^2));
dxu = 0.25*(dx./sqrt(dx.^2+dy.^2));
quiver(x,y,dxu,dyu)
grid on
figure
% Exact solution =c*exp(0.1*x^2)
k= @(x) exp(0.1.*x.^2);
x= -5:0.1:5;
Solution curve
for c=-2:1:2
m= c*k(x);
plot(x,m) Glimpse of Direction
hold on Field
end

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 10


Euler’s method –Numerical ;
y(2)=4; h=0.1; find y(2.5)

h=0.1

h=0.05

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 11


Euler’s method –Numerical ;
y(2)=4; h=0.1; find y(2.5)

Function file
Basic Euler’s method on
function yout = euler1(F,t0,h,tfinal,y0) First order Non-linear
y=y0; DE
yout=y;
for t=t0:h:tfinal-h
s=F(t,y); File calling Euler function “euler1”
y=y+h*s;
yout=[yout;y]; F= @(t,y) 0.1*sqrt(y) +0.4*t^2;
end t0=2;
end h=0.1;
tfinal=2.5;
t y y0=4;
2.0000 4.0000 yout= euler1(F,t0,h,tfinal,y0)
2.1000 4.1800 [(t0:h:tfinal)',yout]
2.2000 4.3768
2.3000 4.5914
2.4000 4.8244
2.5000 5.0768
03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 12
RK4 method –Numerical

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 13


RK4 method –Numerical
𝑑𝑦 𝑝𝑖
cos ( 𝑥+𝑡 ) ; 𝑥 ( 0 )= ; h=0.5 ; find x( 9)
𝑑𝑡 4

Function file

function yout = rkfour(F,t0,h,tfinal,y0)


y=y0;
yout=y;
for t=t0:h:tfinal-h
k1= F(t,y);
k2= F(t+(0.5*h),y+(0.5*k1*h)); RK-4 Numerical on First
k3= F(t+0.5*h,y +0.5*k2*h); order Non-linear DE
k4= F(t+h, y+k3*h);
y= y+ ((h/6)*(k1+2*k2+2*k3+k4));
yout =[yout;y];
end
end

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 14


RK4 method –Numerical
𝑑𝑦 𝑝𝑖
cos ( 𝑥+𝑡 ) ; 𝑥 ( 0 )= ; h=0.5 ; find x( 9)
𝑑𝑡 4

File calling RK4 function “rkfour”


t y
F=@(t,y) cos(t+y)
t0=0; 0 0.7854 (pi/4)
0.5000 0.9807
h=0.5; 1.0000 0.9101
tfinal=9; 1.5000 0.6784
y0=pi/4; 2.0000 0.3559
rkfour(F,t0,h,tfinal,y0); 2.5000 -0.0197
3.0000 -0.4284
t=t0:h:tfinal 3.5000 -0.8588
x=[t',ans] 4.0000 -1.3041
4.5000 -1.7600
5.0000 -2.2238
5.5000 -2.6935
6.0000 -3.1678
6.5000 -3.6457
7.0000 -4.1266
7.5000 -4.6098
8.0000 -5.0950
8.5000 -5.5819
9.0000 -6.0701

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 15


Laplace Transforms (“t” to “s”)

>> syms a
>> syms k s t
>> laplace(exp(a*t)*sin(k*t))
>> laplace(t)
ans =
ans =
1/s^2
k/(k^2 + (a - s)^2)
>> laplace(sin(k*t))
ans =
>> pretty(ans)
k
k/(k^2 + s^2)
-------------
2 2
k + (a - s)

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 16


Inverse Laplace Transforms (“s” to “t”)

>> ilaplace(1/s^4)

ans =

t^3/6

>> ilaplace (1/(s*(s^2+1.4*s+1)))

ans =

1 - exp(-(7*t)/10)*(cos((51^(1/2)*t)/10) +(7*51^(1/2)*sin((51^(1/2)*t)/10))/51)

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 17


Thank you

03/11/2022 AEM Lab DE CA ZG535 BITS Pilani 18

You might also like