Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate Matlab symbolic function

I have a problem with symbolic functions. I am creating function of my own whose first argument is a string. Then I am converting that string to symbolic function:

f =  syms(func)

Lets say my string is sin(x). So now I want to calculate it using subs.

a = subs(f, 1)

The result is sin(1) instead of number.

For 0 it works and calculates correctly. What should I do to get the actual result, not only sin(1) or sin(2), etc.?

like image 312
user2141889 Avatar asked Nov 17 '13 15:11

user2141889


People also ask

How do you convert symbolic to function handle MATLAB?

ht = matlabFunction( f ) converts the symbolic expression or function f to a MATLAB® function with handle ht . If there is an equivalent MATLAB function operating on the double data type for the symbolic expression or function, then the converted function can be used without Symbolic Math Toolbox™.

What is sym in MATLAB?

Assign Symbolic Variables to MATLAB VariablesThe sym function refers to a symbolic variable, which you can then assign to a MATLAB variable with a different name. For example, the command f1 = sym('x') refers to the symbolic variable x and assigns it to the MATLAB variable f1 . f1 = sym('x') f1 = x.


2 Answers

You can use also use eval() to evaluate the function that you get by subs() function

f=sin(x);
a=eval(subs(f,1));
disp(a);
a =

    0.8415
like image 148
xor Avatar answered Nov 02 '22 23:11

xor


syms x
f = sin(x) ;

then if you want to assign a value to x , e.g. pi/2 you can do the following:

subs(f,x,pi/2)
ans =

1
like image 43
Chris Avatar answered Nov 02 '22 22:11

Chris