| 
October 19th, 2009, 07:49 PM
| | Newbie | | Join Date: Sep 2009
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
| | 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 | 
October 20th, 2009, 01:49 AM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 11,379
Country: Thanks: 667
Thanked 3,619 Times in 2,916 Posts
| | Quote:
Originally Posted by xpack 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  . In this case:
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  (you can find these approximatly by plotting
CB
__________________ Truth does not change because it is, or is not, believed by a majority of the people.
Giordano Bruno | 
November 1st, 2009, 03:18 PM
| | Newbie | | Join Date: Sep 2009
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
| | Oh okay, I got it, thank you very much! | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -7. The time now is 02:56 PM. | | |