The Picard Iteration
The goal is to obtain the solution of the first order
quasi-linear ODE by iteration using Matlab. The ODE is:
dy/dx=f(x,y)
where f(x,y) satisfies the Lipschitz Condition.
First, login to your unix account. Create
a directory named 'mae305' if one did not already exist.
Do a 'cd mae305' to enter the directory.
Then, edit the following m-files using your
favorite editor:
%initiations
echo on;
clear;
clear global;
tnew=linspace(0,5);
ynew=tnew*0;
%define global variables
global told yold;
%Nothing between told and yold.
%Start the Picard Iteration ...
RunPicard;
Save the above as 'StartPicard.m' in your mae305 directory.
echo off;
%Plot the current (old) iterant
plot(tnew,ynew,'r');
axis([0 5 0 3]);
grid on;
%Keep the old plot
hold on;
told=tnew;yold=ynew;
%Compute the new iterant
[tnew,ynew]=ode45('picard',0,5,0);
%Note ode45 generates both tnew and ynew.
%Plot the newly obtained iterant
plot(tnew,ynew);
title('Picard Iteration, red is old, yellow is new.');
hold off;
Save the above as 'RunPicard.m' in your mae305 directory.
function yp=picard(t,y);
%Computes dy/dt=picard(t,y)
%Here, yp, t and y are scalars.
%But Matlab can handle yp and y being arrays.
global told yold;
%Note: NOTHING between told and yold, no comma, no semi-colon.
yi=interp1(told,yold,t);
%Above interpolates yold(told) into yi(t)
yp=foft(t)-yi-0.1*(yi^2)/(1+yi^2);
%The right hand side should satisfy the Lipschitz Condition.
%foft(t) is a user-defined function
%to be defined by a separate m-file below.
Save the above as 'picard.m' in your mae305 directory.
function f=foft(t);
%Any function of t
f=3*cos(2*pi*t/20);
Save the above as 'foft.m' in your mae305 directory.
Now, enter matlab by typing 'matlab' at the unix prompt.
To run the code, issue 'StartPicard' at the matlab prompt
(>>). You will see a graph window showing up. There is
a red line (the old iterant), and a yellow line
(the newly computed iterant). To do one more iteration,
issue 'RunPicard'. You will see a new graph of the next iteration.
By pressing the up arrow on your keyboard (and you will see
your last command 'RunPicard' at the matlab prompt),
and then pressing the carriage return, you can do another
iteration.
Issue 'for i=0:5, RunPicard, end' and you will see the
result of doing five iterations.
If you want to edit your m-files while inside matlab,
issue '!jot' (or whatever is your favorite editor). Do
the editing in the jot window, and save your m-file
to mae305 directory.