% % Written by: % -- % John L. Weatherwax 2009-04-21 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % % Problem on EPage 63; Solutions on EPage 161 % %----- close all; clc; clear all; addpath('../../BookCode'); % The problem is equivalent to solving: % % x/5 - cos(x) = 2 % % for x. Then y = exp(x/10) % fn = @(x) ( x/5 - cos(x) - 2 ); fn_prime = @(x) ( 1/5 + sin(x) ); % Lets get a good guess at the location of the root: % if( false ) xs = linspace( 0, 10, 100 ); ys = fn( xs ); plot( xs, ys, '-b' ); grid('on'); end %[x_guess,y_guess] = ginput(1); % % gives % % x_guess ~ 8.2604 % x_guess = 8.2604; % we are told to try x_guess = 1 x_guess = 1; % Use the scalar version: % [ x_newton, n_newton ] = fnewton( fn, fn_prime, x_guess, 1.e-4 ) fn( x_newton ) y_newton = exp(x_newton/10.) % Use the multivariate version: % fn_mv = @(x) ( [ exp(x(1)/10) - x(2); 2*log(x(2)) - cos(x(1)) - 2 ] ); fn_prime_mv = @(x) ( [ [ exp(x(1)/10)/10, -1 ]; [ sin(x(1)), 2/x(2) ] ] ); [ x_newton_mv, n_newton_mv ] = newtonmv( [1,1]', fn_mv, fn_prime_mv, 2, 1.e-4 ) fn_mv( x_newton_mv )