|
I have been trying to code Newton Raphson method and while making the GUI which I ran into a problem which is unique for me.
I want to input a function in the form of a polynomial including the x etc so that I can us the following code which I wrote as a very crude and rough example.
syms x
g=x^3-7*x^2+14*x-6;
dg=diff(g,x);
f=sym2poly(g);
df=sym2poly(dg);
a=0;
b=1;
tol=0.01;
itr=2;
itrr=50;
for i=1:itr
p=(a+b)/2;
if(polyval(f,a)*polyval(f,p)<0)
b=p;
else
a=p;
end
end
s=p;
for j=1:itrr
s1=s-polyval(f,s)/polyval(df,s)
if(polyval(f,s)==0|[abs(s-s1)/s]<tol)
break
else
s=s1
end
end
Which functions can I use?
If that is not possible how can I input in the form [1,-7,14,-6] ? My first priority would be to input including x etc if the procedure involved is not too hectic.
Thanks.
|