Docsity

Euler Method for Solving First Order Ordinary Differential Equations, Assignments of Differential Equations

Toggle sidebar
bg1
Euler Method for First Order ODE
Date: Jan 21, 2002
Last Revision: Jan 21, 2002
Maple 6
Bent E. Petersen
Course: Mth 256
Term: Winter 2002
File name: 256w2002-euler-method.mws
Maple has a number of numeric solvers for differential equations built-in. Normally Maple chooses a
numeric method when we request a numeric answer, but we can specify the method. In particular we
can specify the simple Euler method. Our goal here though is to study the Euler method and therefore
we r oll our own E uler metho d.
> restart;
Here is a simple Euler procedure for solving dy/dx=f(x,y), y(x0)=y0 on a specified interval with a
specified number of steps. We do no error checking - be careful. We return the approximate solution as
a list of lists - the first is the list of abscissae, the second is the list of ordinates. This form is convenient
for plotting.
First here is our Euler routine, eul(). Note we force Maple to use floating point arithmetic. Otherwise
Maple will a ttempt exact evaluat ion, which will wast e immense amount s o f time and RAM.
> eul:=proc(f,x0,y0,x1,N)
> local h,XX,YY,k,x,y;
> x:=evalf(x0): XX:=[x];
> y:=evalf(y0): YY:=[y];
> h:=(x1-x0)/N;
> for k from 1 to N do
> y:=y+h*f(x,y);
> x:=x+h;
> XX:=[op(XX),x];
> YY:=[op(YY),y];
> od;
> [XX,YY];
> end:
pf3
pf4
pf5

Partial preview of the text

Download Euler Method for Solving First Order Ordinary Differential Equations and more Assignments Differential Equations in PDF only on Docsity!

Euler Method for First Order ODE

Date: Jan 21, 2002

Last Revision: Jan 21, 2002

Maple 6

Bent E. Petersen

[email protected]

[email protected]

Course: Mth 256

Term: Winter 2002

File name: 256w2002-euler-method.mws

Maple has a number of numeric solvers for differential equations built-in. Normally Maple chooses a

numeric method when we request a numeric answer, but we can specify the method. In particular we

can specify the simple Euler method. Our goal here though is to study the Euler method and therefore

we roll our own Euler method.

> restart;

Here is a simple Euler procedure for solving dy/dx=f(x,y), y(x0)=y0 on a specified interval with a

specified number of steps. We do no error checking - be careful. We return the approximate solution as

a list of lists - the first is the list of abscissae, the second is the list of ordinates. This form is convenient

for plotting.

First here is our Euler routine, eul(). Note we force Maple to use floating point arithmetic. Otherwise

Maple will attempt exact evaluation, which will waste immense amounts of time and RAM.

> eul:=proc(f,x0,y0,x1,N)

> local h,XX,YY,k,x,y;

> x:=evalf(x0): XX:=[x];

> y:=evalf(y0): YY:=[y];

> h:=(x1-x0)/N;

> for k from 1 to N do

> y:=y+hf(x,y);*

> x:=x+h;

> XX:=[op(XX),x];

> YY:=[op(YY),y];

> od;

> [XX,YY];

> end:

Here is a routine to convert the data returned by eul() into a piecewise linear (or polygonal) expression:

> eulpoly:=proc(EUL,x);

> spline(EUL[1],EUL[2],x,1);

> end:

Let’s try an example. Let

> f01:=(x,y)->x^2+y^2;

f01 :=( x y , )→ x +

2 y

2

> init01:=y(0)=0;

init01 :=y 0( )= 0

> ode01:=diff(y(x),x)=f01(x,y(x));

ode01 := = ∂

x

y( x ) x +

2 y( x )

2

Maple can solve this initial value problem.

> soln01:=dsolve({ode01,init01},y(x));

soln01 :=y( x )=−

x

BesselJ , 

x

2

BesselY , 

x

2

BesselJ , 

x

BesselY , 

x

2

Let’s plot this solution on [0,1].

> plot(rhs(soln01),x=0..1,title="Actual solution",thickness=3);

Let’s put both plots on one diagram.

> plot([rhs(soln01),esoln01],x=0..1,thickness=3,color=[red,blue]);

Here’s another example:

> f02:=(x,y)->x^2-x^3cos(y);*

f02 :=( x y , )→ x −

2 x

3 cos( y )

> ode02:=diff(y(x),x)=f02(x,y(x));

ode02 := = ∂

x

y( x ) x −

2 x

3 cos( y( x ))

> init02:=y(0)=1;

init02 :=y 0( )= 1

> soln02:=dsolve({ode02,init02},y(x));

soln02 :=

Maple returns an empty solution (after a long time) to indicate that it can not find an analytic solution

expression. We can still, however, ask Maple for a numeric (approximate) solution:

> soln02:=dsolve({ode02,init02},y(x),numeric, output=listprocedure);

soln02 :=[ x =( proc ( ) ... x end proc ),y( x )=( proc ( ) ... x end proc )]

Maple actually returns a procedure to compute the numeric estimate. We can use subs() to separate

out the function we are looking for:

> yy:=subs(soln02,y(x));

yy := proc ( ) ... x end proc

> plot(yy,0..2.5,thickness=3,title="Maple numeric solution");

A better way to plot Maple’s numeric solutions of ODEs is to use the odeplot() function. Check

Maple’s help.

Let’s see how our Euler routine fares here

> esoln02:=eulpoly(eul(f02,0,1,2.5,30),x);

> plot([yy,

unapply(esoln02,x)],0..2.5,thickness=3,color=[red,blue]);

Not bad, considering how rough the Euler method is.