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

7 views (last 30 days)
kamal kiki on 9 Oct 2012
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????

Ryan G on 9 Oct 2012
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 on 10 Oct 2012
I am using a " MATLAB Function" block.
It is a block under which is written "MATLAB Fcn".

Ryan G on 9 Oct 2012
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 Comment

kamal kiki on 10 Oct 2012
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.

Azzi Abdelmalek on 9 Oct 2012
Edited: Azzi Abdelmalek on 9 Oct 2012
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

kamal kiki on 10 Oct 2012
Thank you Azzi Abdelmalek for your continuous help.
Thank you also for the time that you have spent in the design of your
I have just used the "MATLAB Function" block and I wrote in the MATLAB
function space the expression sum(u(1:3).*u(4).^u(5:7).*u(8).^u(9:11))
and the result displayed in the "display" block is correct now.
Azzi Abdelmalek on 10 Oct 2012
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 on 11 Oct 2012
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.