1.6 Numerical Computation
Total Page:16
File Type:pdf, Size:1020Kb

§1.6 Numerical Computation
For most of initial value problem
dx f (t, x) , x(t ) x (6.1) dt 0 0 we cannot find a closed form for the solution x(t) . Hence it is important to find the numerical solution of I.V.P. (6.1) . In the matlab there are several ODE solver for the
I.V.P.(6.1) in vector form, i.e., a system of ordinary differentiatial equations,
x1(t) f1 (t, x1 , x2 ,, xn )
x2 (t) f 2 (t, x1 , x2 ,, xn )
⋮ (6.2)
xn (t) f n (t, x1 , x2 ,, xn )
xi(t0 ) xi0 , i 1,2,,n Let
f1 (t, x) x1 (t) f 2 (t, x) x(t) ⋮ , f (t, x) ⋮ xn (t) f n (t, x)
Then (6.2) can be written in vector form dx f (t, x) , dt x(t0 ) x0 . We recommend two most popular ODE solvers in the Matlab, ode45 and ode23s. ode45 is an explicit one-step Runge-Kutter (4th to 5th order) solver. Suitable for nonstiff problems that require moderate accuracy. This is typically the first solver to try on a new problem. Ode23s is suitable for stiff problem where lower accuracy is acceptable.
Example 6.1: Write the well-known van der pol equation x (x 2 1)x x 0 into a system of ODE.
Let x1 x , x2 x . Then
x1 x2
2 x2 (1 x1 )x2 x1 .
In the following we show how to use the Matlab ODE solvers and sketch the graph.
Example 6.2: Use ode45 solver to solve
y w asin y , y(0) 2 / w , 0 t 100
clear all
format long
global w a
w input(‘Please input w ’);
a w 0.001;
t0 0 ; tf 100 ;
y0 2* pi / w ;
[t y]=ode45(‘test1’, [t0 tf],y0);
plot(t, y)
x label ('t') ;
y label (' y') ; Xmin=0;
Xmax=85;
Ymin=0;
Ymax=16;
axis([Xmin,Xmax,Ymin,Ymax]);
disp(‘finish’)
function dy=test1(t,y)
global w a
dy w a *sin( y) ;
Example 6.3: Solve the Van der Pol equation
x (x 2 1)x x 0 , x(0) 2 , x(0) 0 , 0 t 20 .
function ydot =vdpol(t , y ) %VDPOL van der Pol equation.
% ydot =VDPOL(t , y )
% ydot(1) y(2)
% ydot(2) mu(1 y(1)^2) * y(2) y(1)
% mu 2
mu 2 ;
ydot [y(2);mu *(1 y(1)^2) * y(2) y(1)];
Note that the input arguments are t and y but that the function does not use t . Note also that the output ydot must be a column vector.
Given the above ODE file, this set of ODEs is solved using the following commands.
>> t span =[0 20]; % time span to integrate over
>> y 0=[2; 0]; % initial conditions (must be a column)
>> [t,y]= ode45(@vdpol, t span, y 0);
>> size(t ) % number of time points
ans =
333 1
>>size( y ) % (i)th column is y (i) at t (i)
ans =
333 2
>> plot(t , y (:,1),t , y (:,2), ‘- -’)
>> x label(‘time’)
>> title(‘Figure 24.1: van der Pol Solution’) Figure 24.1: van der Pol Solution