Mathematics & Science
Learning Center |
|||
Numerical Methods for Solving Differential EquationsEuler's MethodUsing the Method with Mathematica(continued from last page...) Let's build at a very basic program that could be used to generate a numerical solution to a first order initial value problem of the form:
y′ = f(x, y) using Euler's Method. The solution that it produces will be returned to the user in the form of a list of points. Designing the Program's OutputThe form of the output of our program is, perhaps, the most important part of our program design. After all, it's for this that the user of our program is doing everything. (Even if that user happens to be us.) You'll notice that in the program description given above, we are told that the "solution that it produces will be returned to the user in the form of a list of points." Well, that's easy enough to handle! Based on what we already know about Mathematica's handling of lists and points, we would expect an typical solution to look something like this: {{0,0},{0.25,0},{0.5,0.0625},{0.75,0.21875},{1,0.515625}} The user can then do whatever he likes with this output, such as create a graph, or utilize the point estimates for other purposes. Designing the Program's InputSo now that we have a goal, in the form of the program's output form, we must decide upon the information that must be passed to the program for it to be able to achieve this. In programming jargon, these values are often referred to as parameters. So what parameters would an Euler program need to know in order to start solving the problem numerically? Quite a few should come to mind:
I think that's about it! To code these parameters into the program we need to decide on actual variable names for each of them. Let's choose variable names as follows:
Now let's think about the form of command we'd like to be able to give Mathematica whenever we want the Euler Method numerical solution to a problem. It would be nice if the new program that we are building behaved in a similar fashion to the way Mathematica's built in functions behave. Think about the way the Plot command that we've used so many times is structured. An example of it's use is: Plot[x^2+3x, {x,-2,5}] The parameters that we pass to the Plot command here are:
Maybe we could design our program, let's call it euler, to receive its parameters in a similar way. Say we were told to solve the preliminary example, back in the introduction: y′ = x + 2y numerically, finding a value for the solution at x = 1, and using 4 steps. Doesn't the following command look like the kind of structure Mathematica would use: euler[x+2y,{x,0,1},{y,0},4] if it had a built-in routine for using Euler's Method? Note that we don't capitalize our function's name, euler, since capitalized names are reserved, by convention, for Mathematica's built in commands. Let's look over the parameters:
Is this making sense? I hope so, since I'm trying to take it very slowly for you beginners out there. (I know, you expert programmers are getting bored. Patience!) Using what we just designed as an input form for solving this particular problem, let's now generalize. Earlier we gave names to the parameters our program would need. I'll repeat them here:
Remember? If we put these into a kind of "input template", based on the last example, the general command for executing our program might look something like this: euler[f,{x,x0,xn},{y,y0},steps] Do you agree? Of course, we still have no code written to make the program do anything, but at least we know what the user of our program is going to type in as input. Getting from Input to OutputSo now we come to the hard part! How do we take the program user's input, and produce the expected output? What should be the "guts" of our program? Perhaps we could describe these "guts" in English before we try doing any actual coding. Recall that we summarized Euler's Method back in the Introduction as follows:
Breaking this summary down into small steps we might describe it like this:
The Program ItselfNow we have the job of translating this list of steps from English into Mathematica commands. The result of the translation looks something like this:
euler[f_,{x_,x0_,xn_},{y_,y0_},steps_]:= Ooh! What a lot of new commands! It looks like we need to carefully sift through them all in order to fully understand how the program works. Let's go and analyze the program now... |
|||
ODE Laboratories: A Sabbatical Project by Christopher A. Barker ©2017 San Joaquin Delta College, 5151 Pacific Ave., Stockton, CA 95207, USA e-mail: cbarker@deltacollege.edu |