0

I am solving an equation symbolically:

% Newton's method
syms x;
F(x)=x-cos(x);
FPrime(x)=diff(F(x));
display(FPrime(x));
x0=input('please give first point[x0] = ');
Accuracy=input('Accuracy[xn-xn-1] = ');
for k=0:15;
    x=x0-(F(x0)/FPrime(x0));
    x0=x;
    if(abs(F(x))<=Accuracy);
        display(x);
        break
    end    
end

I need x in as a real number but the answer comes out as (cos(1) - 1)/(sin(1) + 1) + 1. What do I need to do with this if I want a number?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99

1 Answers1

2

Casting your output to double will produce to result you want:

x =

(cos(1) - 1)/(sin(1) + 1) + 1

>> double(x)

ans =

    0.7504

The above was tested on R2016b. If for some reason this doesn't work, there's the fallback of eval(), which produces the same result as double() (in this case).

Note that eval can have various side effects (see example) and should be used in extremely rare cases.

Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • 1
    @MohammadrezaKhoshbin - Thank you for this comment. You are right that `vpa(x)` turns the `sin+cos` expression into a number, however the number is of type `sym`. I understood from the question that the OP wants "a real number", which to me is a `double`. Your suggestion may be equally valid - it's all a question of what the OP wants. – Dev-iL Dec 24 '16 at 17:54
  • Thank you for the clarification! I didn't know about type of the resulting number. – Mohammadreza Khoshbin Dec 26 '16 at 19:06