I am writing the following code to solve for Newtons method and I keep getting t
ID: 2249031 • Letter: I
Question
I am writing the following code to solve for Newtons method and I keep getting the error not enough input arguments for the line x = x0;
function [x, f, conv] = newtfun(fh, dfh, x0)
% newtfun solves f(x)=0 using Newton's Method
% Input arguments
% fh handle to f(x) % dfh handle to f'(x) % x0 initial guess
% Output arguments
% x root % f f(x) % conv convergence=1, 0 otherwise
format long e
steps = 0;
x = x0;
re = 1e-8;
myrel = 1;
while (myrel > re) && (steps < 20)
xold = x;
x = x - feval(fh, x)/feval (dfh, x);
steps = steps + 1;
disp([x feval(fh,x)])
myrel = abs((x-xold));
end
if myrel <= re
conv = 1;
else
conv = 0;
end
f = feval(fh, x);
Explanation / Answer
Your code seems correct to me, but You may be executing it in the worng way.
Here is the explaination :-
By default, pressing the "Run" button (green "Play" button) or F5 will enter the function or script name of the active Editor file in the Command Window and execute that line. In this case, it will enter
>> newtfun
This is incorrect syntax for this function, as you have defined it to require three input arguments; thus, you will get the error message stating that you have not provided enough input arguments. The function should be called as follows (assuming "fh" and "dfh" and "x0" are defined)
>> newtfun(fh,dfh,x0)
I hope you are satisfied with the answer. Please give Positive feedback if you like and do comment :)
Thank You
Happy Learning !!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.