Euler Method - Mathematica - Analyzing the Program

Numerical Methods for Solving Differential Equations

Euler's Method

Analyzing the Euler Program


Instructions

(continued fromÌýlast page...)

To view this page under optimal conditions you should stretch the display window to fill your entire screen. This can be done by dragging on the Resize Handle located at the bottom right corner of this window, as shown in this picture.

You may also adjust the width of the two display panels by dragging the vertical separator bar.
On the left is a complete listing of the euler program that we'll be using for the remainder of this laboratory. You're probably a little foggy on the purpose of many of the commands used within the program, and we're going to try and clear things up a little for you here.

Analyzing the Euler Program

In order to find out the purpose of each of the commands used within the program, simply click on the command that you wish to find out about within the program listing.

euler[f_,{x_,x0_,xn_},{y_,y0_},steps_]:=

Block[
ÌýÌýÌý{xold=x0,
ÌýÌýÌýyold=y0,
ÌýÌýÌýsollist={{x0,y0}},
ÌýÌýÌýx,y,h
},

h=N[(xn-x0)/steps];

Do[
ÌýÌýÌýxnew=xold+h;
ÌýÌýÌýynew=yold+h*(f/.{x->xold,y->yold});
ÌýÌýÌýsollist=Append[sollist,{xnew,ynew}];
ÌýÌýÌýxold=xnew;
ÌýÌýÌýyold=ynew,
ÌýÌýÌýsteps
];

Return[sollist]
]


1. The Function Definition

The first line:

euler[f_,{x_,x0_,xn_},{y_,y0_},steps_]:=

is pretty much what we described earlier, when we designed the form of theÌýinput, but there are a few twists. What about all of thoseÌýunderlineÌýcharacters after each of the variable names? Well, this isÌýMathematica's way of designating the variables as "dummy" or "wild-card" variables within the function's definition. It tellsÌýMathematicaÌýthat theÌýnamesÌýof the variables aren't really important—they're just place-holders in the pattern of use that we are defining.


2. TheÌýBlockÌýCommand

TheÌýBlockÌýcommand here is enormous. If you inspect its brackets, you'll see that it spans 15 lines of code. To find out more about it, we could jump back toÌýMathematicaÌýand ask it for the syntax of theÌýBlockÌý³¦´Ç³¾³¾²¹²Ô»å. I've done this for you in the command displayed below, and I've done a little color-coding to help us discuss things a bit more clearly.

?Block

Block[{x, y, ...°¨,Ìýexpr] specifies thatÌýexprÌýis to be evaluated with local values for the symbolsÌýx, y,Ìý...Ìý.
Block[{x = xo, ...},Ìýexpr]Ìýdefines initial local values forÌýx, ...Ìý.ÌýÌý >>

Now let me display a color-coded version of theÌýBlockÌýcommand from the program:

Block[
ÌýÌýÌý
{xold=x0,
ÌýÌý yold=y0,
ÌýÌý sollist={{x0,y0}},
ÌýÌý x,y,h
ÌýÌý }
,

ÌýÌýÌýh=N[(xn-x0)/steps];

ÌýÌý Do[ xnew=xold+h;
ÌýÌýÌýÌý ynew=yold+h*(f/.{x->xold,y->yold});
ÌýÌýÌýÌý sollist=Append[sollist,{xnew,ynew}];
ÌýÌýÌýÌý xold=xnew;
ÌýÌýÌýÌý yold=ynew,
ÌýÌýÌýÌý {steps}
ÌýÌý ];

ÌýÌý Return[sollist]

]

Comparing the color-coded portions of this display with the syntax help we displayed at the top of this frame, you should be able to see that theÌýBlockÌýcommand that we are using in our program best fits theÌýsecondÌýform of our syntax request:

Block[{x = xo, ...},Ìýexpr]defines initial local values forÌýx, ...Ìý.

Instead ofÌýµ÷x = xo, ...}, we have:

ÌýÌýÌý{xold=x0,
ÌýÌý yold=y0,
ÌýÌý sollist={{x0,y0}},
ÌýÌý x,y,h
ÌýÌý }

The fact that it covers multiple lines is unimportant! I typed it this way just to make it easier to read. This list is still taken to be a list ofÌýlocalÌývariables,Ìýxold,Ìýyold,Ìýsollist,Ìýx,Ìýy, andÌýh. The first three of these also define initial values for the variables, as mentioned in the syntax description. Notice that the initial value ofÌýsollist, which is the variable in which I'm going to store a list of the solution points, is actually the initial condition point.

So what is meant by aÌýlocal variable? Essentially this means that that any values assigned to these variablesÌýwithinÌýour program won't be carried over to any use of these variablesÌýoutsideÌýour program. It is especially important to be careful about this issue if the program is being used by a third party (not you) who is unaware of what variable names should be avoided. With the use of local variables, it doesn't matter. Even though we're usingÌýxÌýwithin the program, it can also be used later in our session without any values we assign to it within the program affecting our later calculations. (By the way,Ìýnon-local variables are calledÌýglobal variables.)

The other part of the syntax request:

Block[{x = xo, ...},Ìýexpr]Ìýdefines initial local values forÌýx, ...Ìý.

refers toÌýexpr. InÌýourÌýprogram we have several lines of code in place ofÌýexpr:

ÌýÌýh=N[(xn-x0)/steps];

ÌýÌý Do[ xnew=xold+h;
ÌýÌýÌýÌý ynew=yold+h*(f/.{x->xold,y->yold});
ÌýÌýÌýÌý sollist=Append[sollist,{xnew,ynew}];
ÌýÌýÌýÌý xold=xnew;
ÌýÌýÌýÌý yold=ynew,
ÌýÌýÌýÌý {steps}
ÌýÌý ];

ÌýÌý Return[sollist]


3. The Local Variable Declarations

The variables listed in braces in the form:

{xold=x0,
ÌýÌý yold=y0,
ÌýÌý sollist={{x0,y0}},
ÌýÌý x,y,h
}

are simply being declared asÌýlocalÌýto the program, as discussed under theÌýBlockÌý³¦´Ç³¾³¾²¹²Ô»å.

  • xold=x0Ìýsets the initial value ofÌýxoldÌýtoÌýx0.

  • yold=y0Ìýsets the initial value ofÌýyoldÌýtoÌýy0.

  • sollist={{x0,y0}}Ìýsets the initial value of the list representing the numerical solution,Ìýsollist, to be the initial pointÌýµ÷{x0,y0}}.

  • x,y,hÌýare not given any initial values, but are still declared as local to the program.

Once you're done, we can go and see the program in action ...


4. The Step-size Calculation

The command:

h=N[(xn-x0)/steps];

is a simple calculation command whose purpose should be quite obvious to you. The step-size,Ìýh, is found by taking the length of theÌýx-interval,Ìýxn-x0, and dividing this length by the number of steps that the user requested,Ìýsteps. TheÌýNÌýcommand wrapped around the calculation assures that the answer will be made into a decimal.


5. The Do Loop

´¡ÌýMathematicaÌýsyntax request on this command would reveal the following:

?Do

Do[expr,Ìýn]Ìý±ð±¹²¹±ô³Ü²¹³Ù±ð²õÌýexprÌýnÌýtimes.
Do[expr,{i,Ìýimax}] evaluatesÌýexprÌýwith the variableÌýiÌýsuccessively taking on the values 1 throughÌýÌýimaxÌý(in steps of 1).
Do[expr,{i,Ìýimin,Ìýimax}] starts withÌýi=imin.
Do[expr,Ìý{i,Ìýimin,Ìýimax,Ìýdi}] uses stepsÌýdi.
Do[expr,Ìý{i,Ìýi1,Ìýi2,…}°¨] uses the successive valuesÌýi1,ÌýÌýi2, …Ìý.
Do[expr,{i,Ìýimin,Ìýimax°¨,Ìý{j,Ìýjmin,Ìýjmax°¨,…] evaluatesÌýexprÌýlooping over different values ofÌýj, etc. for eachÌýi.ÌýÌý>>

Now, looking at a color-coded version of theÌýDoÌýloop in ourÌýeulerÌýprogram:

Do[Ìýxnew=xold+h;
ÌýÌý ynew=yold+h*(f/.{x->xold,y->yold});
ÌýÌý sollist=Append[sollist,{xnew,ynew}];
ÌýÌý xold=xnew;
ÌýÌý yold=ynew
,
ÌýÌýÌýsteps
]

you should be able to see that the version ofÌýDoÌýthat we are using is actually the simplest one of all—the first one described in the syntax request:

Do[expr,Ìýn]Ìý±ð±¹²¹±ô³Ü²¹³Ù±ð²õÌýexprÌýnÌýtimes.

Once again, what the syntax description refers to asÌýexprÌýactually consists of several lines of code, which you can investigate by clicking on them individually in the program listing in the left-hand panel. TheÌýnÌýthat it refers to is what we have calledÌýsteps. In other words, ourÌýDoÌýloop will repeat itselfÌýstepsÌýtimes.


6.ÌýThe Euler Iteration Formulas

The two lines of code:

xnew=xold+h;
ynew=yold+h*(f/.{x->xold,y->yold});

are clearly meant to be using the Euler iteration formulas that we described back in theÌýIntroduction. TheÌýnewÌývalues ofÌýxÌýandÌýyÌýare found from the old values by using Euler's formulas. The code-segment:

f/.{x->xold,y->yold}

simply says to substituteÌýxoldÌýforÌýxÌýandÌýyoldÌýforÌýyÌýin the functionÌýf. In other words, we are evaluating the function at the oldÌýx- andÌýy-values.


7. Building the Solution Point List

The command:

sollist=Append[sollist,{xnew,ynew}]

tellsÌýMathematicaÌýto append (or add) to the end ofÌýsollistÌýthe newest point that's been calculated (the one found in the previous two lines of code.)


8. Resetting the Point Variables

The commands:

xold=xnew;
yold=ynew

makeÌýMathematicaÌýput the values for the coordinates of our newly calculated point into the variables representing theÌýoldÌýpoint's coordinates. The reason for this is to prepareÌýxoldÌýandÌýyoldÌýfor theÌýnextÌýrun through the loop, when the point we've just found will actually be thought of as theÌýoldÌýpoint.


9. The Return Statement

The command:

Return[sollist]

tellsÌýMathematicaÌýto send the user of the program the list of solution points,Ìýsollist, as their result. Notice that this command is only executed once because the loop statement has been closed on the previous line of code.


Once you're done, we can go andÌýsee the program in actionÌý...

Ìý


Compass If you're lost, impatient, want an overview of this laboratory assignment, or maybe even all three, you can click on the compass button on the left to go to the table of contents for this laboratory assignment.