As a newbie, i like to ask a simple question. I am trying to impliment a newton-rapson method for a simple equation as an example. I create a different matlab function from main function for the equation and call it inside the main function. However when I try to call the functions derivative it gives an error. I am aimin to not to take the derivative inside the main function for optimization concerns. I did try different methods but they give errors all the same.
function nr(x0,TC)
if nargin<2, x0=0; TC=10^-4;end
error=TC+1; i=0;
x(1)=x0;
while(error>TC)
x(i+2)=x(i+1)-f(x(i+1))/fd(x(i+1));
error=100*abs((x(i+2)-x(i+1))/x(i+2));
i=i+1;
end
fprintf('After %d iterations an approximate root is %f',i,x(i));
end
function [fx]=f(x)
fx=exp(-x)-x;
end
function fd=fd(x)
fd=-exp(-x)-1;
end