Why the "sum" function is giving a wrong result in Simulink ???

조회 수: 6 (최근 30일)
kamal kiki
kamal kiki 2012년 10월 9일
My goal is to calculate the following sum using Simulink blocks
1.1*10^2*30^9+1.4*10^7*30^4+0.25*10^6*30^8
In the beginning I started with a test of the "sum" function
So I have entered in the command window the following:
c=[1.1 1.4 0.25 ]
p1=[2 7 6]
p2=[9 4 8]
a=10
b=30
And then I have entered in the command window the following command :
sum(c.*a.^p1.*b.^p2)
the result displayed in the command window was:
ans =
1.6620e+017
And this is a correct result.
So in order to do the same thing using Simulink blocks I have used the "MATLAB
Function" block in which I have written in the MATLAB function space the
following : sum(u(1).*u(2).^u(3).*u(4).^u(5)) and in the input of the "MATLAB
Function" block I have placed a "Mux" block containing 5 inputs.
To each input of the "Mux" block I have connected a "Constant" block.
In the Constant value space of the first "constant block" I wrote: [1.1 1.4 0.25]
In the Constant value space of the second "constant block" I wrote: 10
In the Constant value space of the third "constant block" I wrote: [2 7 6]
In the Constant value space of the fourth "constant block" I wrote: 30
In the Constant value space of the fifth "constant block" I wrote: [9 4 8]
Finally I have connected the output of the "MATLAB Function" block to a "Display" block.
After starting the simulation the number 119.7 was displayed in the "Display" block and this number is totally wrong.
Why this is happening ???
Why the "Sum" function is giving a wrong result while used in a Simulink block????
  댓글 수: 2
Ryan G
Ryan G 2012년 10월 9일
Are you using MATLAB Function block or Interpreted MATLAB Function block.
If you are using the MATLAB Function (formerly embedded MATLAB), could you paste the code exactly as it is inside the block?
kamal kiki
kamal kiki 2012년 10월 10일
I am using a " MATLAB Function" block.
It is a block under which is written "MATLAB Fcn".

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

채택된 답변

Ryan G
Ryan G 2012년 10월 9일
Try running this code instead:
sum(u(1:3).*u(4).^u(5:7).*u(8).^u(9:11))
This should become obvious if you spend a minute with it, but essentially when you mux an array with a single constant you are concatinating the vector. So you have
3 + 1 + 3 + 1 + 3 = 11 element array. Now you need to extract the proper elements from that array (see above).
  댓글 수: 1
kamal kiki
kamal kiki 2012년 10월 10일
Thank you very much Ryan G, I have understood your very clear explanation.
I have just replaced sum(u(1).*u(2).^u(3).*u(4).^u(5))
by sum(u(1:3).*u(4).^u(5:7).*u(8).^u(9:11))
and it is working now and the result displayed in the "display" block is
correct.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 9일
편집: Azzi Abdelmalek 2012년 10월 9일
I recommand matlab function(previously named embedded matlab function)
interpreted matlab function is not working for input (length>1). just try
u(1:3).*u(4).^u(5:7).*u(8).^u(9:11)
you will not find a vector with size 3
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 10일
Ryan's answer is fine, but why are using 5 constant block with a Mux. use one constant block with value: [ c a p1 b p2]
kamal kiki
kamal kiki 2012년 10월 11일
I think using 5 constant blocks is more convenient because, when the data is
separated, no confusion is made and it is less likely to make mistakes.
Also, in my Simulink model, I need to separate data into different blocks
because the values of a and b will come from calculations made in other
blocks.

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

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by