hw6_soln - Due: October 16, 2008 CS 257 (Luke Olson):...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Due: October 16, 2008 CS 257 (Luke Olson): Homework #6 Problem 1 Problem 1 You are writing an API for a finance company which is in need of a solver for finding the roots of nonlinear equations (i.e. finding an x such that f ( x ) = 0). You decide to implement Newton’s method in MATLAB. (Newton’s method is given in Lecture 13.) The function takes as arguments f,f ,x ,maxiter where f is the function we want to find the zeros of, f its derivative, x the initial guess and maxiter is the maximum number of iterations. For each iteration, output the following table. Iteration number x i f ( x i ) f ( x i ) /f ( x i- 1 ) 2 f ( x i ) /f ( x i- 1 ) Run your program on the following test cases a. f ( x ) = e- x- cos( x ), x = π/ 2, maxiter = 10 b. f ( x ) = 2 x 3- 9 x 2 + 12 x + 15, x = [2 . 99 , 3 , 3 . 01], maxiter = [20 , 4 , 20] c. f ( x ) = p | x | ,x = 0 . 5 ,maxiter = 10 hint: f ( x ) = sign ( x ) 2 √ | x | and for each case, determine if the method converges. If so, is the convergence quadratic or linear? If is not quadratic explain why. You can use the following template to start your code. Submit a print out of your code, a table of results for each test case and an explanation of convergence and convergence rate. Solution An implementation of Newton’s method Listing 1: Newton’s Method 1 function [x,err] = newton(f,df,x0,maxiter) 2 3 fprintf ( ’-- Running Newtons method at x0 = %g on ’ , x0) 4 disp (f) 5 6 fprintf ( ’iter x f(x) f(x)/f(x)ˆ2 f(x) /f(x)\n’ ) 7 x_old = x0; 8 err = zeros (maxiter, 1); 9 for i=1:maxiter 10 x = x_old - f(x_old)/df(x_old); 11 12 err(i) = f(x)/f(x_old)ˆ2; 13 fprintf ( ’%4d %15g %15g %15g %15g\n’ , i,x,f(x),f(x)/f(x_old)ˆ2,f(x)/f (x_old)); 14 if ( abs (err(i)) < 1e-12) 15 err = err(1:i); 16 return 17 end 18 19 x_old = x; 20 end A script to run test functions Listing 2: Test Script 1 newton_soln(inline( ’exp(-x)-cos(x)’ ),inline( ’-1 * exp(-x)+sin(x)’ ), pi /2,10) Problem 1 [Solution] continued on next page... Page 1 of 10 Due: October 16, 2008 CS 257 (Luke Olson): Homework #6 Problem 1 [Solution] (continued) 2 newton_soln(inline( ’2 * xˆ3-9 * xˆ2+12 * x+15’ ),inline( ’6 * xˆ2-18 * x+12’ ) ,2.99,20) 3 newton_soln(inline( ’2 * xˆ3-9 * xˆ2+12 * x+15’ ),inline( ’6 * xˆ2-18 * x+12’ ),3,4) 4 newton_soln(inline( ’2 * xˆ3-9 * xˆ2+12 * x+15’ ),inline( ’6 * xˆ2-18 * x+12’ ) ,3.01,20) 5 newton_soln(inline( ’sqrt(abs(x))’ ), inline( ’sign(x)/(2 * sqrt(abs(x)))’ ) ,.75,10) Output of test runs Listing 3: Output of Test Functions 1 test_newton 2-- Running Newtons method at x0 = 1.5708 on 3 Inline function : 4 f(x) = exp (-x)- cos (x) 5 6 iter x f(x) f(x)/f(x)ˆ2 f(x)/f(x) 7 1 1.30836 0.0108301 0.250616 0.0520979 8 2 1.29279 6.49974e-05 0.554155 0.00600156 9 3 1.2927 2.45629e-09 0.581416 3.77905e-05 10 4 1.2927 11 12 ans = 13 14 1.2927 15 16-- Running Newtons method at x0 = 2.99 on 17 Inline function : 18 f(x) = 2 * xˆ3-9 * xˆ2+12 * x+15 19 20 iter x f(x) f(x)/f(x)ˆ2...
View Full Document

This note was uploaded on 07/10/2011 for the course CS 257 taught by Professor Olson during the Spring '08 term at University of Illinois, Urbana Champaign.

Page1 / 10

hw6_soln - Due: October 16, 2008 CS 257 (Luke Olson):...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online