I am trying to write a modified Newton formula in MATLAB, but MATLAB shows results in symbolic (x) form, not numeric. Here is the code:
clc
format long g
syms x;
fun = input ('Enter the function f(x)= : ','s');
f=inline(fun);
z=diff(f(x));
f1=inline(z);
z1=diff(f1(x));
f2=inline(z1);
u1=f(x)/f1(x);
u2=1-[(f(x)*f2(x))/(f1(x)^2)];
x0=input('enter the first guess x0=: ');
for i=0:6
xn=x
x=xn-[u1/u2];
if x==xn
break
end
end
And here are the results:
Enter the function f(x)= : x^2-2
enter the first guess x0=: 1
xn =
x
xn =
x + (x^2 - 2)/(2*x*((2*x^2 - 4)/(4*x^2) - 1))
xn =
x + (x^2 - 2)/(x*((2*x^2 - 4)/(4*x^2) - 1))
xn =
x + (3*(x^2 - 2))/(2*x*((2*x^2 - 4)/(4*x^2) - 1))
xn =
x + (2*(x^2 - 2))/(x*((2*x^2 - 4)/(4*x^2) - 1))
xn =
x + (5*(x^2 - 2))/(2*x*((2*x^2 - 4)/(4*x^2) - 1))
xn =
x + (3*(x^2 - 2))/(x*((2*x^2 - 4)/(4*x^2) - 1))
how can i fix it? Thanks.