Why the "sum" function is giving a wrong result in Simulink ???
3 views (last 30 days)
Show older comments
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 Comments
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?
Accepted Answer
Ryan G
on 9 Oct 2012
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).
More Answers (1)
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
3 Comments
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]
See Also
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!