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 in 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:
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.)
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.
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.
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 and 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...