1170:Lab12 November 20th, 2012

advertisement
1170:Lab12
November 20th, 2012
Goals For This Week
This week we’ll implement Euler’s Method, the simplest way to numerically solve differential equations.
1
Euler’s Method
Leonhard Euler was perhaps the greatest mathematician who ever lived. This is why we should all learn to
pronounce his name correctly (“Oiler” NOT “Yooler”). Among his many innovations was the appropriately
named “Euler’s Method” which allows us to find approximate solutions to differential equations.
Euler’s method works for many types of differential equations, but in this lab we will focus on those with
the following form.
dy
= f (t)
(1)
dt
The solution to the above is the given by the unknown function y(t), the derivative of which is f (t). Note
that the solution to this and all differential equations is a function rather than just a single number.
So far, all we know about y(t) is it’s derivative f (t), but this cannot tell us the whole story of the solution.
Imagine that y(t) represents the population of Salt Lake City, then f (t) must be the rate of change of the
population. No matter how smart you are, there’s no way that you could figure out y based on f unless
you know the population at some starting time (t = 0). A differential equation always has to have initial
conditions in order to have a solution. Hopefully, this will become apparent once we get familiar with Euler’s
method.
Euler’s method discretizes time. This means that we won’t be able to evaluate the function at every t, just
finitely many values. By convention, these values of t are at regular intervals of length h, i.e (0, h, 2h, 3h, ...).
We let yn = y(nh) and iteratively calculate each value like this.
y0
yn
=
=
Initial Value
yn−1 + h ∗ f (tn−1 )
(2)
(3)
=
yn−1 + h ∗ f (h ∗ (n − 1))
(4)
Note that this is a discrete dynamical system!
2
Your tasks for the week
Let f (t) = e10 sin(t) . Write this as a function in R. Plot this function from t=0 to t=10 and save the plot.
The main part of this assignment is writing a function to do Euler’s method on R. I want you to plot the
solution from t=0 to t=10. The function Euler should have two variables as inputs: an initial condition y0
and the number of steps N .
Your function will need to do the following.
1
1. Calculate the proper spacing h based off of the total time,10, and the number of steps, N .
2. Define the appropriate tvalues - numbers from 0 to 10 spaced by h. (Use the seq command)
3. Initialize the y values by using the seq command (seq(1,N))
4. Define the first y value as y0
5. Run a for loop to calculate the remaining y values
6. Plot the resulting y values with t on the horizontal axis. Leave the result as points.
Using your Euler function:
1. Set N = 1000, y0 = 0, 5000, 15000, 35000. Include all four plots. What is the difference between them?
How do their derivatives differ?
2. Can you explain why there are steep sections and flat section in your graph of y. Use the graph of f
in your explanation.
3. With y0 = 0, let N = 100 and save the plot. Then let N = 10 and save the plot. What difference does
this make. What can you conclude about Euler’s method?
2
Download