Function for symbolic expression with only some variable among all variables

3 views (last 30 days)
Hi,
I have this symbolic expression:
J =
[ - a2*sin(t1 + t2) - a1*sin(t1), -a2*sin(t1 + t2)]
[ a2*cos(t1 + t2) + a1*cos(t1), a2*cos(t1 + t2)]
[ 0, 0]
[ 0, 0]
[ 0, 0]
[ 1, 1]
where symvar are:
[ a1, a2, t1, t2]
I saw that I can convert this symbolic expression by matlabFunction command but variables/function-input must be [ a1, a2, t1, t2].
Because I want to put resultant function into a Interpreted MATLAB Function block, how can I assign only t2 as "input" and the others like mask-parameters ? There is a way to convert J to a matlab function with only t2 as input?

Answers (1)

Shashank Prasanna
Shashank Prasanna on 23 Jan 2013
You can use anonymous function to do that. However make sure that the other 3 variables a1 a2 t2 are also defined in the function other wise it will error out.
>> Jfun = matlabFunction(J)
Jfun =
@(a1,a2,t1,t2)reshape([-a2.*sin(t1+t2)-a1.*sin(t1),a2.*cos(t1+t2)+a1.*cos(t1),0.0,0.0,0.0,1.0,-a2.*sin(t1+t2),a2.*cos(t1+t2),0.0,0.0,0.0,1.0],[6,2])
>> Jfunt2 = @(t2)Jfun
Jfunt2 =
@(t2)Jfun
This was you can call Jfunt2 with only t2 as input arguments, but make sure the other 3 variables are defined someone above.
  2 Comments
Walter Roberson
Walter Roberson on 23 Jan 2013
You would need
Jfunt2 = @(t2)Jfun(a1, a2, t1, t2)
where a1, a2, t1 were defined in scope when the anonymous function was defined.
Walter Roberson
Walter Roberson on 23 Jan 2013
I am concerned here about whether Simulink MATLAB functions are permitted to use anonymous functions (or function handles). I do not recall that as being one of the permitted datatypes in such functions. I might be getting confused on terminology, though, about which Simulink MATLAB functions are executed in MATLAB as a whole and which ones have to conform to the embedded function requirements.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!