Sunteți pe pagina 1din 7

EX.

No: 7
Date : Solving Initial value problem

Objective: To write a MATLAB script for solving initial value problem.

Software used: MATLAB

Commands used: ode23, ode45.

Problem Description:
1. Solve the following initial value problem using MATLAB inbuilt functions.

 where  0  1 for a time interval of 0 to 1. compare with exact solution


 2 

.

2. 
     4 

      

where  0  1,  0  0 for 0 to 1
Flowchart:

Start

Solve the given problems using ode23 and ode45


commands

Display output

End
Program:
dydx
function dydx=eqn1(t,y);
dydx=-2*t*(y^2);

dydx2
function dydx2=eqn(t,y);
dydx2=[1 -4;-1 1]*y

clc
fprintf('Reg.No : PR10ME1011 \n');
fprintf('Ex 7 \n');
format long;
tspan=0:0.25:1;
y0=1;
[t1 y1]=ode23('dydx',tspan,y0);
[t2 y2]=ode45('dydx',tspan,y0);
[t1 y1 y2];
subplot(2,2,1)
plot(t1,y1,t1,y2),xlabel('t'),ylabel('y'),legend('y1','y2'),title('Plot b/w
y1 & y2');
subplot(2,2,2)
plot(t1,y1-y2),xlabel('t'),ylabel('Diff between y1&y2'),title('Comparison
b/w y1 & y2');
t=[0:0.25:1];
y3=dsolve('Dy+2*t*(y^2)','y(0)=1','t');
y3=1./(1+t.^2);
subplot(2,2,3);
y3=y3';
plot(t,y1-y3,t,y2-y3),xlabel('t'),ylabel('y'),legend('y1 error','y2
error'),title('Comparison with Exact solution');
y0=[1 0];
[t1 y1]=ode23('dydx2',tspan,y0);
[t2 y2]=ode45('dydx2',tspan,y0);
figure
plot(t1,y1,t1,y2),xlabel('t'),ylabel('y'),legend('ode23','ode45'),title('Co
mparison b/w ode23 & ode 45');
Output:
Result: Thus a MATLAB script was programmed for the given initial value problems and
verified with exact solution.
Program:
dydx
function dydx=eqn1(t,y);
dydx=-2*t*(y^2);

dydx2
function dydx2=eqn(t,y);
dydx2=[1 -4;-1 1]*y

clc
fprintf('Reg.No : PR10ME1011 \n');
fprintf('Ex 7 \n');
format long;
tspan=0:0.25:1;
y0=1;
[t1 y1]=ode23('dydx',tspan,y0);
[t2 y2]=ode45('dydx',tspan,y0);
[t1 y1 y2];
subplot(2,2,1)
plot(t1,y1,t1,y2),xlabel('t'),ylabel('y'),legend('y1','y2'),title('Plot b/w
y1 & y2');
subplot(2,2,2)
plot(t1,y1-y2),xlabel('t'),ylabel('Diff between y1&y2'),title('Comparison
b/w y1 & y2');
t=[0:0.25:1];
y3=dsolve('Dy+2*t*(y^2)','y(0)=1','t');
y3=1./(1+t.^2);
subplot(2,2,3);
y3=y3';
plot(t,y1-y3,t,y2-y3),xlabel('t'),ylabel('y'),legend('y1 error','y2
error'),title('Comparison with Exact solution');
y0=[1 0];
[t1 y1]=ode23('dydx2',tspan,y0);
[t2 y2]=ode45('dydx2',tspan,y0);
figure
plot(t1,y1,t1,y2),xlabel('t'),ylabel('y'),legend('ode23','ode45'),title('Co
mparison b/w ode23 & ode 45');
Output:

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