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.?
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™.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With