Solving non- linear equations using newton Rapshson
Show older comments
function [k,p0,inc,error,y] = newton(f,dfv,p0,delta,max1)
error = 10^-5 ;
fprintf('k\t\t Seq Of P0\t\t Increment\t\t Value of P0\n');
for k = 1:max1
[f,dfv] = feval(f,p0);
inc = f/dfv;
y = f;
fprintf('%d \t %15.8f\t%15.8f\t%15.8f\n',k,p0,inc,y);
if abs(inc) < delta,break, end
p0 = p0 -inc;
error = abs(inc);
end
k
p0
inc
y
This is what I have coded for newton raphson but I'm constantly getting this error
>> newton(@ (x) x^3 -3*x - 2,@(x) 3*x^2 - 3 , 2.1, 0.001, 15)
k Seq Of P0 Increment Value of P0
Error using -
Too many output arguments.
Error in @(x)x^3-3*x-2
Error in newton (line 6)
[f,dfv] = feval(f,p0);
Answers (1)
Sulaymon Eshkabilov
on 20 May 2021
There are a few points that need to be corrected. Here are there.
function [k,p0,inc,error,y] = newton(f,dfv,p0,delta,max1)
error = 10^-5 ;
fprintf('k\t\t Seq Of P0\t\t Increment\t\t Value of P0\n');
for k = 1:max1
F = f(p0);
DF = dfv(p0);
inc = F/DF;
y = F;
fprintf('%d \t %15.8f\t%15.8f\t%15.8f\n',k,p0,inc,y);
if abs(inc) < delta,break, end
p0 = p0 -inc;
error = abs(inc);
end
end
Note that feval() not recommended to use.
Now you can test it:
[k,p0,inc,error,y]=newton(@ (x) x^3 -3*x - 2,@(x) 3*x^2 - 3 , 2.1, 0.001, 15)
Categories
Find more on Numerical Integration and Differential Equations 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!