Jon
Jon

Reputation: 1209

Constructing Symbolic Functions in Matlab

I'm trying to build a symbolic function in Matlab, as follows:

syms theta
Rx(theta) = cos(theta) + sin(theta);

When I enter Rx(0.1), Matlab returns cos(1/10) + sin(1/10)

But what I'm trying to do is get Matlab to evaluate it numerically. I can accomplish that with double(Rx(0.1)), but when doing the same thing on much more complex functions in a loop, the conversion to double each time causes it to run very slowly. Is there a way to alter Rx itself to give numeric output?

Upvotes: 1

Views: 66

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

You could create a standard (non-symbolic), anonymous function from your symbolic function. For this you use matlabFunction

syms theta
Rx(theta) = cos(theta) + sin(theta);
Rxd = matlabFunction(Rx);

Then

>> Rxd(0.1)
ans =
   1.094837581924854

Note that you may lose precision, though, as the computations are done numerically from the beginning, as opposed to symbolically and converted to double only at the end.

Upvotes: 1

Related Questions