Tuesday, March 21, 2017

                                                              Chapter-10
    SOLUTION OF DIFFERENTIAL EQUATION USING MATLAB
MATLAB has large library of tools that can be used for solving differential equation.
dy/dt = f(t,y)  for t0<t<t with y = y0   at  t = t0
We can solve ordinary differential equation using MATLAB programming. We should note that using MATLAB we cannot find y(t) (or y as a function of t). We can find values of y at specified values of t.
There are following steps for solving differential equation:
  • ·         First step is to create a user-defined function file that calculates dy/dt for given values of t and y.

Syntax of function file:
function dydt = name_of_function_file( input_args )
dydt = f(t,y);
end
  • ·         Second step is to select method of solution.

Select the numerical method that you would like MATLAB to use in the solution. Several of the numerical methods are available as built-in functions in MATLAB. There are following MATLAB built-in functions that are used for solving differential equation:
1.       ode45 : It is one step solver and based on explicit Runge-kutta method.
2.       ode23 : It is also one step solver and based on explicit Runge-kutta method. It is often quicker but less accurate than ode45.
  • ·         Third step is to solve the differential equation using MATLAB.

Syntax for using command that is used to solve an initial value ordinary differential equation is:
[t,y] = built-in_function('name_of_function_file',tspan,y0)



For example:
We have differential equation:
dy/dt = (t3 – 2y)/t  for 1<t<3 with y = 4.2 at t = 1
Function file for following differential equation:
function dydt = ODE1(t,y)
%function for solving differential eqquation
%   Detailed explanation goes here
dydt = (t^3 - 2*y)/t;
end

Suppose We have selected ode45 method for solving differential equation. Script file for above differential equation:
%function for solving differential equation
[t y] = ode45('ODE1’,[1:0.01:3],4.2);%we have got values of y and t in workspace after the execution of this command.
plot(t,y)%for plotting graph of y with respect to t

Q) Solve the following differential equation using MATLAB:

dy/dx  =  (x2 - 2x + 3)/(y2)  for  0.5<x<3  with y(0.5)  =  2

function dydx = ODE3( x,y )
%fuunction file for solving differential equation
dydx = (x^2 - 2*x + 3)/(y^2);
end

clc
clear all
close all
xspan = [0.5:0.01:3];
y1 = 2;
[x y] = ode45('ODE3',xspan,y1);
plot(x,y)




No comments:

Post a Comment