How to show the result of a symbolic computation as a number?

42 views Asked by At

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?

1

There are 1 answers

2
Dev-iL On BEST ANSWER

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.