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

MATLAB code_Solving ODEs (1) - converted

The document provides instructions for solving ordinary differential equations (ODEs) and systems of ODEs using MATLAB. It includes specific examples, such as finding explicit solutions and numerical approximations, along with corresponding MATLAB code. Additionally, it covers the implementation of function files and plotting results for visual representation.

Uploaded by

Md Rony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MATLAB code_Solving ODEs (1) - converted

The document provides instructions for solving ordinary differential equations (ODEs) and systems of ODEs using MATLAB. It includes specific examples, such as finding explicit solutions and numerical approximations, along with corresponding MATLAB code. Additionally, it covers the implementation of function files and plotting results for visual representation.

Uploaded by

Md Rony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

 

1.2  Second  and  Higher  Order  Equations   1.3  System  of  Ordinary  Differential  Equations  (ODEs)  
1.  Finding  Explicit  Solutions  of  ODE  or  ODE  systems:     Write  matlab  code  to  solve  the  following  ODE  system:  
  Solve.    𝑦 !! 𝑥 + 8𝑦 ! 𝑥 + 2𝑦 𝑥 = cos 𝑥 ;              𝑦 0 = 0, 𝑦 ! 0 = 1.  
!"
1.1  Solve  IVP.    !" = 𝑥𝑦    for  𝑦 1 = 1.      
  Matlab  code:                                                                                    𝑥 ! 𝑡 = 𝑥 𝑡 + 2𝑦 𝑡 − 𝑧(𝑡)  
Matlab  code:    
clear all
𝑦 ! 𝑡 = 𝑥 𝑡 + 𝑧(𝑡)  
 
clear all
clc                                                                                𝑧 ! 𝑡 = 4𝑥 𝑡 − 4𝑦 𝑡 + 5𝑧(𝑡)  
clc
x = linspace(0,1,20); Matlab  code:  
y = dsolve('D2y + 8*Dy + 2*y = cos(x)','y(0)=0, Dy(0)=1','x');
x = linspace(0,1,20); clear all
z = eval(vectorize(y));
y = dsolve('Dy = y*x','y(1)=1','x'); plot(x,z) clc
z = eval(vectorize(y)); xlabel('x', 'FontSize',16); t = linspace(0,0.5,25);
plot(x,z) ylabel('y','FontSize',16); inits='x(0)=1, y(0)=2, z(0)=3';
  [x,y,z] = dsolve('Dx=x+2*y-z', 'Dy=x+z', 'Dz=4*x-4*y+5*z',
  inits);
0.2
xx = eval(vectorize(x));
yy = eval(vectorize(y));
1
zz = eval(vectorize(z));
0.18
plot(t,xx,'k--','LineWidth',1.5)
0.95 hold on
0.16
plot(t,yy,'k-.','LineWidth',1.5)
hold on
0.9 0.14
plot(t,zz,'k','LineWidth',1.5)
hold on
0.12 %plot(t,xx,t,yy,t,zz)
0.85
xlabel('t', 'FontSize',16);
ylabel('x,y,z','FontSize',16);

y
0.1
0.8 legend('x','y','z')
0.08  
0.75
0.06
25
x
0.7 0.04 y
z

0.02
0.65 20

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0.6
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 x
15
   

x,y,z
   
    10

   
   
   
5

   
    0
    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
t
     
 

2  Finding  Numerical  Solutions    


45
  2.2  System  of  ODEs    
2.1  First  order  ODE  with  M-­‐files     40
  Solve  the  following  system  of  Lorentz  equations  
Write  matlab  code  to  numerically  approximate  the  solution     35

of  the  first  order  differential  equation     𝑑𝑥


!" = −𝜎𝑥 + 𝜎𝑦   30
= 𝑥𝑦 ! + 𝑦;      𝑦 0 = 1,  on  the  interval  𝑥 ∈ [0, 0.5].   𝑑𝑡
!" 𝑑𝑦
  = 𝜌𝑥 − 𝑦 − 𝑥𝑧  

z
25

To  solve  the  above  ODE  problem  we  need  to  create  two  m-­‐ 𝑑𝑡
𝑑𝑧 20
files  (function  file  and  matlab  file)       = −𝛽𝑧 + 𝑥𝑦  
𝑑𝑡
  To  solve  the  above  ODE  system  we  need  to  create  two  m-­‐ 15
Function  file  is  as  follows:   files  (function  file  and  matlab  file)      
function yprime = firstode(x,y); 10
yprime=x*y^2+y;  
end
Function  file  is  as  follows:   5
    -20 -15 -10 -5 0 5 10 15 20
x
matlab  file  is  as  follows:   function xprime = lorentz(t,x);
clear all sig=10;
 
clc beta=8/3
y0=1; rho= 28  
xspan=[0,0.7]; xprime=[-sig*x(1)+sig*x(2);rho*x(1)-x(2)-x(1)*x(3);-
[x,y]=ode23(@firstode,xspan,y0);
 
beta*x(3)+x(1)*x(2)];
plot(x,y, 'LineWidth',2) end  
axis([0 0.7 1 2]);
   
   
 
   
matlab  file  is  as  follows:  
2
   
1.9 To  draw  first  figure:    
1.8    
1.7
clear all  
clc
 
1.6
x0=[-8 8 27];  
1.5 tspan=[0,20];  
[t,x]=ode45(@lorentz, tspan, x0);
1.4
plot(x(:,1),x(:,3))  
1.3 xlabel('x', 'FontSize',16);  
ylabel('z','FontSize',16);
1.2  
1.1
 
1
 
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
 
 
To  draw  second  figure:  
 
clear all
clc

x0=[-8 8 27];
tspan=[0,20];
[t,x]=ode45(@lorentz, tspan, x0);
%plot(x(:,1),x(:,3))
subplot(3,1,1)
plot(t,x(:,1))
subplot(3,1,2)
plot(t,x(:,2))
subplot(3,1,3)
plot(t,x(:,3))
 
20

10

-10

-20
0 2 4 6 8 10 12 14 16 18 20

40

20

-20

-40
0 2 4 6 8 10 12 14 16 18 20

50
40
30
20
10
0
0 2 4 6 8 10 12 14 16 18 20

 
 

You might also like