MATLAB: The “sum” function is giving a wrong result in Simulink ??

MATLABsimulinksum function

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????

Best Answer

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).