Code covered by the BSD License

# Newton-Rapshon method

### hüseyin çilsalar (view profile)

16 Nov 2012 (Updated )

Calculation of a function's root using Newton-Rapshon method

Newton_Rapshon_Method.m
```disp('Enter the function(example f(x)=x^2-4x+4)')
fx=input('f(x)= ','s');
f=inline(fx);
disp(   ' ----   Function   ---'   )
fx
fd=inline(diff(fx));
disp(   '---   Derivative   ---   ')
fd

tolerance=input('tolerance=  ');
x0=input('initial value of x = ');
x(1)=x0;

format long e
disp(' teration No:              x:              Relative difference:')
for i=1:inf;
x(i+1)=x(i)-(f(x(i))/fd(x(i)));
a(i)=abs((x(i+1)-x(i))/(x(i)));

disp('-------------/--------------------------------/------------------')
fprintf('%d       %3.17e                 %3.17e\n',i,x(i),a(i));

if abs(a(i))<tolerance
break
end
end

fid =fopen('Newton-Rapshon_Method.txt','w');

fprintf(fid,'\nteration no:            x:                       Relative difference\n');

for j=1:i;

fprintf(fid,'\n%4d             %3.17e       %3.17e\n',j,x(j),a(j));
end
fclose(fid)

ezplot(f,-90,90)
grid on
ylabel('f(x)')
text(x(i),0,'----->f(x)=0 point')

```