Make A Script File of Newton's Method That Calculates The Derivative by Itself.
Make A Script File of Newton's Method That Calculates The Derivative by Itself.
Assignment # 02
Program:
function [x]=newton1(fun,f,df,dff,a,b,acc,N,z,z1,x0,i)
syms x;
fun=input('Enter the function as variable of x : ');
f=inline(fun);
z=diff(f(x));
df=inline(z);
z1=diff(df(x));
dff=inline(z1);
char x;
a=input('Enter Initial root Guess : ');
b=input('Enter another Initial Root Guess : ');
if (f(a)*f(b)>0)
error('Root does not lie in this region');
else if (f(a)*dff(a)>0)
x0=a;
else
x0=b;
end
end
N=input('Enter the number of itterations : ');
i=1;
x=x0;
x(x0-1)=x0-f(x0)/df(x0);
while (i<=N)
x(i+1)=x(i)-f(x(i))/df(x(i));
i=i+1;
end
end
Output :
>> newton1
ans =