- #1
renolovexoxo
- 25
- 0
I've been using this for a Newton Approximation in Matlab
function x = Newton(f, fp, x, nmax, e)
% f is an inline function which we apply Newton's method on
% fp is an inline function that is the derivative of function f
% x is the initial guess of the root
% nmax is the total number of iterations done
% e is the error used to control convergence
fprintf('x(0) = %10g \n', x)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < e
fprintf('Converged! \n')
return
end
end
with this to run it:
%declare our function f
f = inline('e^x+2^(-1*x)+2*cos(x)-6');
% declare the derivative of function f
fp = inline('e^x-ln(2)*2^(-1*x)-2*sin(x)');
% declare total number of iterations to be undertaken
nmax = 100;
% declare value of initial starting point
x = 1.0;
% declare amount of error allowed
e = 10.0e-5;
% carry out iteration using function above
x = Newton2(f,fp,x,nmax,e);
This isn't working or running for this function, but has run fine for every other function. I'm not sure what isn't working about it. I keep getting the error:
Error using inline/subsref (line 13)
Not enough inputs to inline function.
Error in Newton2 (line 11)
d = f(x)/fp(x);
But I am very new to Matlab, and have no idea what this means.
function x = Newton(f, fp, x, nmax, e)
% f is an inline function which we apply Newton's method on
% fp is an inline function that is the derivative of function f
% x is the initial guess of the root
% nmax is the total number of iterations done
% e is the error used to control convergence
fprintf('x(0) = %10g \n', x)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < e
fprintf('Converged! \n')
return
end
end
with this to run it:
%declare our function f
f = inline('e^x+2^(-1*x)+2*cos(x)-6');
% declare the derivative of function f
fp = inline('e^x-ln(2)*2^(-1*x)-2*sin(x)');
% declare total number of iterations to be undertaken
nmax = 100;
% declare value of initial starting point
x = 1.0;
% declare amount of error allowed
e = 10.0e-5;
% carry out iteration using function above
x = Newton2(f,fp,x,nmax,e);
This isn't working or running for this function, but has run fine for every other function. I'm not sure what isn't working about it. I keep getting the error:
Error using inline/subsref (line 13)
Not enough inputs to inline function.
Error in Newton2 (line 11)
d = f(x)/fp(x);
But I am very new to Matlab, and have no idea what this means.