Finding the zero of a function using Newton Raphson's method in MatLab
Show older comments
I want to find using Newton's method the zero of the function, f(x) to 4d.p. the first positive root, x(first) by writing my code in matlab.
My function is: f(x) = x^4 -6.4x^3 + 6.45x^2 + 20.538x - 31.752 = 0
This is the code that I have so far:
function [root] = newton_simple(func,dfunc,x,tol)
if nargin < 5; tol = 1.0e6*eps; end for i = 1:30 dx = -feval(func, x)/feval(dfunc,x); x = x + dx; if abs(dx) < tol root = x; return end end root = NaN
func = @(x) (x^4 - 64*x^3 + 6.45*x^2 + 20.538*x - 31.752); dfunc = @(x) (4*x^3 - 19.2x^3 + 12.9*x + 20.538); xStart = 2; [root] = newton_simple(func,dfunc,xStart)
[root] = newton_simple(@fex4_7,@dfex4_7,2.0) root = 2.1000
2 Comments
What is your question?
John D'Errico
on 13 Feb 2017
So what is wrong with that code? If nothing, then why are you posting a question?
Answers (0)
Categories
Find more on Newton-Raphson Method in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!