Why the "sum" function is giving a wrong result in Simulink ???
4 次查看(过去 30 天)
显示 更早的评论
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
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?
采纳的回答
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 个)
Azzi Abdelmalek
2012-10-9
编辑:Azzi Abdelmalek
2012-10-9
I recommand matlab function(previously named embedded matlab function)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172088/image.jpeg)
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
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]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!