Needing help with using newton raphson method to find roots
4 views (last 30 days)
Show older comments
I'm needing to make a function to determine the root of the function using Newton-Raphson method. with output being the root and and the inputs being an initial estimate and approximate error limit. I have one from false position method but I'm not sure where to start from there. Please let me know.
f=@(x)(x.^3+7*x.^2-33*x-135);
yl = input('type approximated lower boundary:');
yu = input('type approximated upper boundary:');
error = input('Type desired approximate error limit:');
while (f(yl)*f(yu)) > 0
disp(' the function at boundaries cannot have the same sign, try new ones.');
%if values do not produce a negative number give better boundaries
break
end
yold = yu;
Root = yl;
iterations = 0;
while (abs(yold-Root) > error)
yold = Root;
Root = yu -(f(yu)*(yl - yu))/(f(yl) - f(yu));
if (f(yl)*f(Root) < 0)
yu = Root;
else
yl = Root;
end
iterations = iterations+1;
plot(-10:.01:10,f(-10:.01:10))
end
iterations
Root
%f(ynew) was here % small, but difference from zero not the same as err criterion
% (they are related by df/dy at the root
0 Comments
Answers (1)
Geoff Hayes
on 18 Feb 2019
Anthony - your abovecode seems to be the Bisection method. For Newton's Method, your code should just take an initial guess and then find a better approximation as
y0 = ...; % initial guess
y1 = y0 - f(y0)/g(y0);
where f and g are your function and its derivative respectively. You then just repeat the above until your approximate error limit has been satisfied (or until you have reached a maximum number of iterations).
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!