Newton's Method Question

조회 수: 9 (최근 30일)
Zachary Grant
Zachary Grant 2015년 10월 5일
댓글: Zachary Grant 2015년 10월 7일
"Use your code to locate the root of x^3 + x = 2*x^2 + 3, starting with x0 = 3 and print a table of at least 5 iterates for epsilon = 1e − 7."
I'm having trouble getting my code right. I know that there's better methods out there, but this is along the lines of how it's supposed to be written for an assignment. I'm new to using Matlab, so it may not even make sense + it's my first time encountering the "while" function. Looking for some feedback on how to make this code more understandable.
%Newton's Method
function root = newtonr(f, fp, x, epsilon, nmax)
n=1;
x=3;
f=x^3-2*x^2+x-3;
fp=3*x^2-4*x+1;
nmax=10;
epsilon=1.0e-15;
while (abs(error)>epsilon && n <= nmax)
denom=fp;
if (denom==0)
disp('denom is zero');
return;
else
x=x-f/denom;
end
n=n+1;
error=x-x;
x=x;
end
root=x;

답변 (1개)

James Tursa
James Tursa 2015년 10월 5일
편집: James Tursa 2015년 10월 5일
Some hints (I leave it to you to insert them properly into your code):
I assume the function handle and function derivative handle are passed into your newtonr routine, so the f and fp lines in your code should evaluate these functions, not overwrite them. E.g., I would assume something like this would be in the calling routine:
f = @(x)x^3-2*x^2+x-3;
fp = @(x)3*x^2-4*x+1;
And then f and fp would be input arguments to the newtonr routine. So inside the newtonr routine these lines as written overwrite the f and fp inputs:
f=x^3-2*x^2+x-3;
fp=3*x^2-4*x+1;
To make your newtonr routine find roots of a function that is passed in via f and fp, don't overwrite them. E.g., do something like this instead to evaluate the function and its derivative at the value of x:
fx = f(x);
fpx = fp(x); % <-- but not really needed at the front of newtonr
Also, don't use error for a variable name since it is a built-in function name:
while (abs(error)>epsilon && n <= nmax)
The above line should be testing for change in x instead.
Your denominator evaluation line:
denom=fp;
Should look something like this instead (re-evaluate the function derivative at the current x):
denom = fp(x);
And this line:
x=x-f/denom;
Don't overwrite x immediately. First save the result in a new variable, e.g. xnew, so that you can calculate the change in x later. E.g.,
xnew = x - fx/denom;
Then the change in x (needed for your convergence test) can be:
xchange = xnew - x;
Then you can update x:
x = xnew;
fx = f(x);
  댓글 수: 5
Steven Lord
Steven Lord 2015년 10월 7일
How do you define f and fp? Do you define them as anonymous functions, function handles to named functions, or vectors of data? For simple functions, an anonymous function (like James created in the first code block in his Answer) is probably good enough to define the function to be evaluated. But if you have a more complicated function, writing it as a function file and passing a function handle to it into your newtonr function to be evaluated when newtonr needs a value from your function is fine as well.
fh = @why; % Define a function handle to the WHY function
fh(1) % This is the same as why(1), but without hard-coding WHY into the code
fh = @sin; % Change the function to be evaluated
fh(1) % Now returns sin(1)
As for the reason for the "Undefined function or variable xchange" error -- where is the first place you define that variable in your code? Where is the first place you use that variable? Which one comes first, definition or usage?
Zachary Grant
Zachary Grant 2015년 10월 7일
I guess I could just leave it as an anonymous function, like James suggested. I don't define "xchange" until the while function, and I think the usage comes before the definition.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by