Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 19th, 2009, 07:49 PM
Newbie
 
Join Date: Sep 2009
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
xpack is on a distinguished road
Default Does this look right?

Use Newton's method to find all roots of the equation correct to the six decimal place.
2cos(x) = 2 - x

This is the newton code our teacher gave us, its all explained here
Code:
function r = newton(f, fp, xg, mx, tol, vu)
% NEWTON: Finds zero of a function via Newton's method
% Syntax: newton(f, fp, xg, mx, tol, vu)
%   f   - function for which a zero [f(x)=0] is sought;
%           use function handles: @f, @fp, etc.
%   fp  - function representing derivative f'(x)
%   xg  - initial guess for zero
%   mx  - maximum number of iterates
%   tol - tolerance (how close last two iterates
%            must be to declare convergence)
%   vu  - 1=view iterates, 0=suppress viewing
fprintf('Newton''s Method\n\n')
xn = xg; ae = 2*tol;
fprintf('initial guess: x = %+13.9f\n', xn)
for k = 1:mx
    xo = xn;
    fo = feval(f, xo);
    fpo = feval(fp, xo);
    xn = xo - fo/fpo;
    ae = abs(xn - xo);
    if vu
        fprintf('x = %+13.9f\n', xn)
    end
    if ae < tol
        break
    end
end
if ae < tol
    disp('Converged!')
    fprintf('solution: %+13.9f, #iterations: %i\n\n', xn, k)
else
    disp('Failed to converge!')
    fprintf('last iterate: %+13.9f, #iterations: %i\n\n', xn, mx)
end
r = xn;
This is what I got
Code:
delete s232x17.txt; diary s232x17.txt
clear; clc; close all; echo on
%
% Stewart 232/17
%
f=@(x) 2*cos(x)+x-2
fp=@(x) 1-2*sin(x)
x=[0:.01:2];
y=f(x);
plot(x,y)
hold on
plot(x,0)
format long
newton(f,fp,1.5,10,1e-6,1)

%answer 1.109144

%
echo off; diary off
Can someone tell me if this is correct and if not, what am I doing wrong. Thank you
Reply With Quote
Advertisement
 
  #2  
Old October 20th, 2009, 01:49 AM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 11,379
Country:
Thanks: 667
Thanked 3,619 Times in 2,916 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

Quote:
Originally Posted by xpack View Post
Use Newton's method to find all roots of the equation correct to the six decimal place.
2cos(x) = 2 - x

This is the newton code our teacher gave us, its all explained here
Code:
function r = newton(f, fp, xg, mx, tol, vu)
% NEWTON: Finds zero of a function via Newton's method
% Syntax: newton(f, fp, xg, mx, tol, vu)
%   f   - function for which a zero [f(x)=0] is sought;
%           use function handles: @f, @fp, etc.
%   fp  - function representing derivative f'(x)
%   xg  - initial guess for zero
%   mx  - maximum number of iterates
%   tol - tolerance (how close last two iterates
%            must be to declare convergence)
%   vu  - 1=view iterates, 0=suppress viewing
fprintf('Newton''s Method\n\n')
xn = xg; ae = 2*tol;
fprintf('initial guess: x = %+13.9f\n', xn)
for k = 1:mx
    xo = xn;
    fo = feval(f, xo);
    fpo = feval(fp, xo);
    xn = xo - fo/fpo;
    ae = abs(xn - xo);
    if vu
        fprintf('x = %+13.9f\n', xn)
    end
    if ae < tol
        break
    end
end
if ae < tol
    disp('Converged!')
    fprintf('solution: %+13.9f, #iterations: %i\n\n', xn, k)
else
    disp('Failed to converge!')
    fprintf('last iterate: %+13.9f, #iterations: %i\n\n', xn, mx)
end
r = xn;
This is what I got
Code:
delete s232x17.txt; diary s232x17.txt
clear; clc; close all; echo on
%
% Stewart 232/17
%
f=@(x) 2*cos(x)+x-2
fp=@(x) 1-2*sin(x)
x=[0:.01:2];
y=f(x);
plot(x,y)
hold on
plot(x,0)
format long
newton(f,fp,1.5,10,1e-6,1)
 
%answer 1.109144
 
%
echo off; diary off
Can someone tell me if this is correct and if not, what am I doing wrong. Thank you
You can check this by substituting the final solution back into f(x). In this case:

f(1.109144)=2*\cos(1.109144)+1.109144-2\approx 1.4\times 10^{-7}

which looks pretty good.

(Also the code looks good as well)

The only problem is that the question asks for all of the roots, there appear to be three near x=0,\ 1.1,\ 3.7 (you can find these approximatly by plotting f(x)

CB
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
  #3  
Old November 1st, 2009, 03:18 PM
Newbie
 
Join Date: Sep 2009
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
xpack is on a distinguished road
Default

Oh okay, I got it, thank you very much!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 02:56 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.