Euler Method - Mathematica Implementation Part 1

Numerical Methods for Solving Differential Equations

Euler's Method

(continued fromÌýlast page...)

Using the Method withÌýMathematica

Up to this point in the laboratories we have been usingÌýMathematicaÌýpurely as a computer algebra system. We have been giving it commandsÌýone at a time, hitting [ENTER], and waiting for the result. However, the true power of theÌýMathematicaÌýsystem cannot be fully tapped until you learn that it is possible to put strings of commands together in a single cell. In other words, you can writeÌýprogramsÌý¾±²ÔÌýMathematica.

An Unrelated Example of a Program

Just to get the basic idea, let's look at a mini-program that has nothing whatsoever to do with Euler's Method. Consider the following sequence of instructions:

Print["Hello!"];
powerlist=Table[w^k, {k,3,8}];
Print["Look at my list of powers --> ",powerlist];
Do[Print["That was cool!"], {i,5}]

Notice that we have four lines of code here, and that each one is separated from the next by a semi-colon. (In actuality these instructions need not even be on separate lines provided that they have a semi-colon separating them.) Let's talk about the action of each line individually:

  1. Print["Hello!"];ÌýThe purpose of this command is fairly obvious. Note the use of quotes. This tellsÌýMathematicaÌýthatÌýHello!Ìýis to be interpreted as a literal string of characters, andÌýnotÌýas the factorial of aÌývariableÌýcalledÌýHello. (IfÌýHelloÌýwere a variable whose current value was 5, for example, theÌýPrintÌýcommand would have printed 120 if the quotes were absent.)

  2. powerlist=Table[w^k, {k,3,8}];ÌýThis command tellsÌýMathematicaÌýto generate a list of values of the formÌýwk, whereÌýkÌýranges from 3 up to 8. The resulting list is to be assigned to the variableÌýpowerlist.

  3. Print["Look at my list of powers --> ",powerlist];ÌýHere we tellÌýMathematicaÌýto print out the text enclosed in quotes as a literal string of characters, and to follow this up with the value ofÌýpowerlistÌýwhich was calculated in the previous step.

  4. Do[Print["That was cool!"], {i,5}]ÌýThis command is a loop in which theÌýPrintÌýstatement will print out the literal text, and theÌýDoÌýloop surrounding it will keep issuing the command until the variableÌýiÌýhas iterated from 1 up to 5 in steps of 1. (The starting value and step size default to 1 unless otherwise specified.)

Now that we know what the program isÌýsupposedÌýto do, let's go theÌýMathematicaÌýand type it in to see if it works. Remember, you can useÌýCopyÌý²¹²Ô»åÌýPasteÌýif you want to save yourself the effort of typing.

You can switch toÌýMathematicaÌýby clicking on the button at left. This will open up a fresh notebook for you. Don't forget to come back here when you're done! See you in a few minutes.


Now let'sÌýgo seeÌýwhat you should have gotten...


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.