Unit-5(Solution of ODE)
Unit-5(Solution of ODE)
Unit-5(Solution of ODE)
differential Equations
(ODEs)
3-1
2 nd
Order Range-kutta
Method
3-2
3-3
3-4
3-5
MATLAB Program
f=input('Enter the function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t0+i*h;
k1=h*f(t(i),y(i));
k2=h*f(t(i+1),y(i)+k1);
y(i+1)=y(i)+(1/2)*(k1+k2);
Coupled Equations
3-7
3-8
MATLAB Program-Coupled
Equations
f=input('Enter the first function:');
g=input('Enter the second function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the 1st dependent
variable:');
z0=input('Enter the initial value of the 1st dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate
your solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
z(1)=z0;
for i=1:n
m1=h*g(t(i),y(i),z(i));
k2=h*f(t(i+1),y(i)+k1,z(i)+m1);
m2=h*g(t(i+1),y(i)+k1,z(i)+m1);
y(i+1)=y(i)+(1/2)*(k1+k2);
z(i+1)=z(i)+(1/2)*(m1+m2);
fprintf('y(%.2f)=%0.4f\n',t(i+1),y(i+1));
fprintf('z(%.2f)=%0.4f\n',t(i+1),z(i+1));
end
4 th
Order Range-kutta
Method
3-11
MATLAB Program
f=input('Enter the function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t0+i*h;
k1=h*f(t(i),y(i));
k2=h*f(t(i)+(h/2),y(i)+(k1/2));
k3=h*f(t(i)+(h/2),y(i)+(k2/2));
k4=h*f(t(i)+(h),y(i)+(k3));
MATLAB Program-Coupled
Equations
f=input('Enter the first function:');
g=input('Enter the second function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the 1st dependent
variable:');
z0=input('Enter the initial value of the 1st dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
z(1)=z0;
for i=1:n
t(i+1)=t0+i*h;
m1=h*g(t(i),y(i),z(i));
k2=h*f(t(i)+(h/2),y(i)+(k1/2),z(i)+(m1/2));
m2=h*g(t(i)+(h/2),y(i)+(k1/2),z(i)+(m1/2));
k3=h*f(t(i)+(h/2),y(i)+(k2/2),z(i)+(m2/2));
m3=h*g(t(i)+(h/2),y(i)+(k2/2),z(i)+(m2/2));
k4=h*f(t(i)+(h),y(i)+(k3),z(i)+(m3));
m4=h*g(t(i)+(h),y(i)+(k3),z(i)+(m3));
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4);
z(i+1)=z(i)+(1/6)*(m1+2*m2+2*m3+m4);
fprintf('y(%.2f)=%0.4f\n',t(i+1),y(i+1));
fprintf('z(%.2f)=%0.4f\n',t(i+1),z(i+1));
end
ODE Solving using
dsolve
3-18
MATLAB Program
syms y(x) a
eqn = diff(y,x) == y;
S = dsolve(eqn)
syms y(t) a
eqn = diff(y,t,2) == a*y;
ySol(t) = dsolve(eqn)
syms y(t) a
eqn = diff(y,t) == a*y;
cond = y(0) == 5;
ySol(t) = dsolve(eqn,cond)
syms y(t) a b
eqn = diff(y,t,2) == a^2*y;
Dy = diff(y,t);
cond = [y(0)==b, Dy(0)==1];
ySol(t) = dsolve(eqn,cond)
syms y(t) z(t)
eqns = [diff(y,t) == z, diff(z,t) == -y];
S = dsolve(eqns)
Solving First –order
equations using ODE23
and ODE45
3-25
%dy/dx=(x^2-2*x+2)/(y^2); domain [0.5,5] ;y=2 @ x=0.5
syms y(t);
answer=dsolve('Dy=(x^2-2*x+2)/(y^2)','y(0.5)=2','x');
%simplify(answer);
domain=[0.5 5];
fplot(answer,domain);
ODE Solving using
ODE23
3-27
Solving using ode23 and comparing the same with solving
dsolve command
%Solving first order differential equations using ODE23
and ODE45
domain=[0.5 5];
y_at_xi=2;
%ode23('Which is the function that contains the
DE',domain,y_at_xi)
[IV,DV]=ode23('MYODE1',domain,y_at_xi);
disp('IV');
disp('________');
fprintf('\n');
disp(IV);
disp('DV');
disp('________');
fprintf('\n');
disp(DV);
figure;
%dy/dx=(x^2-2*x+2)/(y^2); domain [0.5,5] ;y=2 @
x=0.5
syms y(t);
answer=dsolve('Dy=(x^2-2*x+2)/(y^2)','y(0.5)=2','x');
fplot(answer,domain);
hold on;
plot(IV,DV,'r.');
clc;
clear all;
close all;
%Solve first order De using ODE23
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode23(F,[0,3],1);
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE23');
ODE Solving using
ODE45
3-31
clc;
clear all;
close all;
%Solve first order De using ODE45
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode45(F,[0,3],1);
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE45');
clc;
clear all;
close all;
%Solve first order De using ODE23
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode23(F,[0,3],1);
figure;
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE23');
hold on;
clc;
clear;
%Solve first order De using ODE45
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode45(F,[0,3],1);
plot(x,y,'k.');
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE45');
Graphical User
Interface
3-35
MATLAB Graphical User Interface
Components