% Newton's method example

clear

f=inline('exp(x)+x.^2-5*x'); % define function and its derivative
fp=inline('exp(x)+2*x-5');

x=-3:.01:3;
figure;plot(x,f(x),'r',x,zeros(size(x)),'k') % graph to see where zeros are

x0=0; % starting guess
x1=x0-f(x0)/fp(x0); % first iteration
count=1;
while abs(x0-x1)>1e-8 && count<100 % iterate until values are very close
    x0=x1;
    x1=x0-f(x0)/fp(x0);
    count=count+1;
end
[count x1] % prints out number of iterations and final answer