0

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.

am304
  • 13,758
  • 2
  • 22
  • 40
  • It would have been better to do an edit in your former post and being active answering peoples questions that publishing a new copy hoping for the best. The reason that you got no response was that the inline solution is deprecated and difficult. Also, inline functions are meant to be evaluated numerically. This means that for pure symbolic functions you get problems. I wanted to to try to remove the inlines and test to go symbolic, but since you did not ask how or react in any way it is hard to teach you this. SO is not a free doing homework for you service. However, I will commit an answer. – patrik Mar 09 '15 at 13:21
  • Dear patrik, i edited my former post but i didn't see your comment yesterday. i just saw it. i try to do what you say. but i can't get solution by that way. i get close the solution by this code. but this is either not the correvt one. i know it. but it is best i can do. – Omer Faruk Mar 09 '15 at 13:59

2 Answers2

1

The answer is symbolic because x (and by extension xn) is defined as a symbolic variable. To have numerical results instead use subs:

for i=0:6
    xn=x;
    subs(xn) % display numerical value in the command window
    x=xn-[u1/u2];
    if abs(subs(x)-subs(xn))<=1e-6 % replace equality test by comparing difference to a small threshold value
        break
    end
end

Note: you shouldn't do equality test on floating-point numbers, instead you should compare the difference to a small threshold value (see my modified code).

am304
  • 13,758
  • 2
  • 22
  • 40
  • Have you successfully tried this? I may have done it wrong, but I did not manage to get the expected numerical result. Also I got a crash in the if test. – patrik Mar 09 '15 at 13:08
  • @patrik No, I haven't. However, the symbolic variables appear to be displayed correctly, so there's no reason why using `subs` shouldn't work. i don't know what release the OP is using, but it's quite possible that in an older release, using `inline` is perfectly fine. – am304 Mar 09 '15 at 14:02
  • I see, it did not work on 2014b however. Maybe since`x` is overwritten. It seems kind of weird and `x` seems to be both guess and variable. I think the problem is that x becomes an expression instead of a "constant" in `x=xn-[u1/u2];`. This causes problems since `x` is also the dependent variable. However, I wrote an answer not using `inline` since matlab claims it to be deprecated and later removed. – patrik Mar 09 '15 at 14:44
1

It is the inlines that causes trouble. If you are going to use something similar you should use anonymous functions, since inline functions are deprecated and about to be removed. However, when going symbolic this is completely redundant. It will only cause more trouble for you, so I strongly suggest that you get rid of the inlines. Normally eval is not recommended, but for this application it ought to be ok. I guess that there is other ways to do this, but I feel that eval feels intuitive when evaluation expressions.

Further you should consider an approach when you use a function call instead of input. function xout = newton(symbolicExpression, initialGuess). This is more dynamic.

The following code should do what you ask:

clc
format long g
syms x;
fun = input ('Enter the function f(x)= : ','s');
f = eval(fun);
f1 = diff(f);
f2 = diff(f1);
u1 = f/f1;
u2 = 1-( (f*f2)/(f1^2) );
x=input('enter the first guess x0=: ');
for i=0:6
    xn=x
    x=xn-eval(u1/u2);
    if (x-xn)<0.01
        break
    end
end
patrik
  • 4,506
  • 6
  • 24
  • 48
  • this is working. thank you. my english is not so good. so i can't understand what you say at all. but i reallı try to do what you say. – Omer Faruk Mar 09 '15 at 14:10