You are on page 1of 1

Honors Differential Equations

Using Professor White's MATLAB script for Euler's Method




1. You will need Professor White's m-file euler.m. To download this file, start Netscape
or Internet Explorer and go to the UML home page: http://www.uml.edu. Click on
Academic Departments, then Mathematical Sciences Department, then the Courses
circle, then 92.236 Honors Differential Equations - Prof. Pennell (under Course Web
Pages), then Resource Links, then Professor White's Web Page, then List of
MATLAB Demos, then euler.m under the Numerical Methods heading. You will see
the contents of the file on your screen. Click on the File menu, then click on Save As.
In the File Name box, type d:\euler.m (or a:\euler.m if you have a floppy disk, or
e:\euler.m if you have a Zip disk), then click Save.
2. Start MATLAB, then type cd d:\ (or cd a:\ if you saved the files on a floppy disk, or
cd e:\ if you saved the files on a Zip disk).
3. Before you use euler.m, you will need to create a file defining the right-hand side of
the d.e. you want to solve. Click on the File menu, click on New, then click on M-
file. An Editor/Debugger box will open on the screen. To solve the d.e. y' = 3 + t - y,
type in the following three lines (including the last line with the word return):
function z = f(t, y)
z = 3 + t - y; %Dont forget the ; at the end of the line
return
Click on the File menu in the Editor/Debugger box, click Save As, then in the File
Name box type d:\f.m (or a:\f.m if you have a floppy disk, or e:\f.m if you have a
Zip disk). Click on the X in the upper right of the Editor/Debugger box, putting you
back in MATLAB command mode.
4. To use Euler's Method on the problem y' = 3 + t - y, y(0) = 1, for 0 t 0.4, with a
step size h=0.1, type
a = 0;
b = 0.4;
y0 = 1;
h = 0.1;
n = (b - a)./h; % Dont forget the . before the /
[t, y] = euler('f', a, b, y0, n);
y' % The vector y contains the computed y values.
% The ' causes the numbers to be printed in rows rather than in a single column
plot(t, y) % This plots the computed solution

To solve the same IVP with a step size of h=0.05, type the commands
h=0.05;
n=(b - a)./h; % Dont forget the . before the /
[t, y] = euler('f', a, b, y0, n); % Dont forget the ; at the end of the line
y(1 : (n ./ 4) : n+1)' % Dont forget the . before the /
% This command shows the computed y values at 4 equally spaced t values

5. Follow the same four steps with h=0.0025. Print the results of all three runs.

You might also like