Creation of a simulink block from a Mupad expression containing subscripts.

조회 수: 1 (최근 30일)

Hello fellow Matlabers,

My goal is to generate a simulink block from a Mupad expression.

In Mupad I wrote:

S := sum(sin(`θ`[j])*cos(`θ`[j]), j=1..5)

and I got the symbolic expression:

   cos(`θ`[1])*sin(`θ`[1]) + cos(`θ`[2])*sin(`θ`[2]) + cos(`θ`[3])*sin(`θ`[3]) + cos(`θ`[4])*sin(`θ`[4]) + cos(`θ`[5])*sin(`θ`[5])

displayed in the Mupad Notebook.(see the attached image).

So my goal is to generate directly from the expression S := sum(sin(`θ`[j])*cos(`θ`[j]), j=1..5) the simulink block that you can see in the block_picture.PNG (attached).

I have created this block using the code below inside a MATLAB Function block:

function S = fcn(theta1, theta2, theta3, theta4, theta5)
%#codegen
S = cos(theta1)*sin(theta1) + cos(theta2)*sin(theta2) + cos(theta3)*sin(theta3) + cos(theta4)*sin(theta4) + cos(theta5)*sin(theta5) ;

But the problem is that I will be using long expression for S with many theta(j)'s (j=1...............20).

So I need to use an expression like:

S := sum(sin(`θ`[j])*cos(`θ`[j]), j=1..20)

inside the Matlab Function block because I will save a lot of time.

  댓글 수: 1
Karan Gill
Karan Gill 2017년 11월 15일
편집: Karan Gill 2017년 11월 15일
Why are you using MuPAD instead of Symbolic Math Toolbox? MuPAD notebooks are not recommended.

댓글을 달려면 로그인하십시오.

채택된 답변

Yogananda Jeppu
Yogananda Jeppu 2017년 11월 13일
Use Mux block to get theta 1 ... N. You need so many inputs. The Mux will create the array for you. Take this as a single input u into the model (Matlab function). in the function compute y = sum(sin(u).*cos(u)). note the '.*' for element multiplication.
  댓글 수: 1
kamal kiki
kamal kiki 2017년 11월 14일
Hi Mr. Yogananda,
Thank you so much for your answer.
I have used your approach before but in a more complicated format: I typed inside the "Fcn" block
y= sin(u(1))*cos(u(1))+sin(u(2))*cos(u(2))+sin(u(3))*cos(u(3)).......
But your idea about the use of the much shorter format with the '.*' is very helpful. It works with the "interpreted MATLAB function" block.
Thank you again, I will save time with that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by