1170:Lab8 October 23rd, 2012

advertisement
1170:Lab8
October 23rd, 2012
Goals For This Week
In this lab we’ll use R to perform Newton’s method. You will shortly be learning about Newton’s
method as a way to find the solutions to complicated equations.
We’ll compute Newton’s method, as well as
1
Intro to Newton’s Method
Suppose that we have a an equation of the following form xe−x = .5x − cos(x). No matter how clever we
are, there is simply no way to solve this equation by hand. However, we can make a series of ever better
approximations.
First, we rewrite the equation so that one side is zero.
xe−x − .5x + cos(x) = 0
(1)
Then let’s replace the left hand side by f (x). Now, we are just trying to solve f (x) = 0.
Your first task is to write the the function f . Then plot f , with x going from 0 to 2, to see approximately
where it is zero. When plotting call your x values list ”xvals”. I’m going to guess that the root is at about
x = 1.
I’ll plug in to check.
>f(1)
[1] 0.4081817
Newton’s method says that I can get a better solution with the following formula.
xnew = xold − f (xold )/f ′ (xold )
(2)
This requires us to calculate the derivative of f . Now we’re going to need a second function called ’fprime’
that represents the derivative of f . You’ll need to first calculate the derivative (by hand) and then put that
into a function.
After you have that setup then we can use R to update the initial guess of 1.
> x<-1
> x<-x-f(x)/fprime(x)
> x
[1] 1.281771
> f(x)
[1] -0.03482677
With the new value of x, f (x) is much closer to zero!
Rerun the command twice more and record the value of x you get, as well as the value of
f (x).
1
2
Visualizing the method
Next we’ll do a little plotting exercise to better visualize what’s happening when we perform Newton’s
method.
Even though it’s clear wrong, we’ll start with x = .5.
> x<-.5
Then we’ll mark the point on our plot.
> points(x,f(x),col=’red’)
Then plot the tangent line to the function at that point at x = .5.
> lines(xvals,(xvals-x)*fprime(x)+f(x),col=’red’)
Update your guess for x. Plot the corresponding point and line in blue.
Update for a third time and plot the final point in purple. You should now have a plot with the
function f , one blue line, one red line and three plotted points showing your guesses. Save
this plot.
3
Making a function for Newton’s Method
Here I’ve written part of a little function that runs Newton’s method
newton<-function(x0,N){
x<-x0
for.....
print(x)
}
Fill in the missing for loop line to make it work.
4
Tasks for this week
1. Please include the value of x you found in the first section.
2. Include the graph we made in the second section
3. Use the function from the third section to compute the solution of the equation. First, let N=5 and
x0=.5 to make sure you get the same answer.
4. Now try x0=2, is it the same?
5. Try x0=-2, is it the same? What if you increase N?
6. Now try x0=3, is it the same? What if you increase N?
7. Now try x0=7. Again, try increasing N.
8. Now try x0=10.
9. Finally, set x0=3. Create another plot like we did in the second section. Can you explain why Newton’s
method is producing different answers?
2
Download