The following is a Matlab program (second version)
to solve differential equations
numerically using
Euler's Method .
This variation will give the graph/solution on two sides of
the innitial-time.
I will explain how to use it at the end:
The Program:
function y=y(n,t0,t1,y0)
|
h=(t1-t0)/n;
|
t(1)=t0;
|
y(1)=y0;
|
for i=1:n
|
t(i+1)=t(i)+h;
|
y(i+1)=y(i)+h*ex(t(i),y(i));
|
end;
|
t(n+1)=t0;
|
y(n+1)=y0;
|
for i=n+1:2*n
|
t(i+1)=t(i)-h;
|
y(i+1)=y(i)-h*ex(t(i),y(i));
|
end;
|
for i=1:n
|
T(i)=t(2*n-i+1);
|
Y(i)=y(2*n-i+1);
|
end;
|
for i=n+1:2*n
|
T(i)=t(i-n);
|
Y(i)=y(i-n);
|
end;
|
V=[T',Y']
|
plot(T,Y)
|
title('satya')
|
How to Use the Program?
- You have to have the above program in a
filename.m-file. I would call it
eulertwo.m.
- You also have to have a file ex.m
and you type in your equation in the file.
I typed in a problem as follows:
%x is a function of t and y is the first derivative x'(t)
|
function y=y(t,x)
|
y=(t^2-x^2)*sin (x);
|
- Now, on matlab prompt, you write
eulertwo(n,t0,t1,y0) and return
, where n is the number of t-values, t0 and t1 are the left
and right end points and
y(t0)=y0
is the innitial condition.
- Matlab will return your answer. You should also get the graph, if
your computer is set up properly. I do not get the graph in my office
but I get it in the lab.