- Multiplication - MATLAB times .* - MathWorks India
- Matrix multiplication - MATLAB mtimes * - MathWorks India
What other way can I run a an array through MATLAB SIMSCAPE without error i.e. "Operand must be scalar"?
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to take a set of values e.g. 5 to 40 in increments in 5. Then, use this to calculate the area of a circle and then, insert each calculated value into Simscape individually. My aim is to calculate the pressure through a pipe for a range of diameters but I have other parameters I would like to iterate the process to - within the same code.
I have the following code:
Dia =5
Dia4 =Dia *(1:10)
Len1 = 10* (5:10)
numsim=length (Dia)%Number of simulations.
in(numsim) = Simulink.SimulationInput;
for idx = numsim:-1:1 % This index the number of dia values from array into SIMSCAPE.
in(idx).ModelName = 'x';
in(idx) = in(idx).setVariable('Dia', Dia1(idx))
in(idx) = in(idx).setVariable('Len', len1(idx))
end
Ar1 = Dia.^2*pi/4 % This returns row array.
I want to run each increment of Dia obtained into Ar1 one at a time to run multiple simulations and record each result separately. However, the following error in SIMSCAPE occurs.
Error compiling Simscape network for model x.
Caused by:
['x/Pipe (TL)']: Failed to compute value for parameter, surface_area. In foundation.thermal_liquid.elements.pipe (line 65)
Invalid use of *. Both operands must be matrices. At least one of the operands must be scalar or the inner dimensions must agree. In foundation.thermal_liquid.elements.pipe (line 65)
Argument 1 = {[1x10 double], 'm'}
Argument 2 = {[50 60 70 80 90 100], 'm'}
area = {[1x10 double], 'm^2'}
Dh = {5, 'm'}
length = {[50 60 70 80 90 100], 'm'}
Line 65:
parameters (Access = private)
surface_area = (4*area/Dh)*length; % Pipe surface area
volume = area * length; % Pipe volume
end
I am assuming it is because of the way the surface area is calculated.
How do I get passed this? Is there something I can do to improve the current code?
0 个评论
回答(1 个)
Avadhoot
2023-10-31
Hi Lihini,
I understand that you want to perform element-wise multiplication operation on your area vector. The error you are facing is due to the use of matrix multiplication operator instead of element-wise multiplication operator.
In this example, “area” is a 1x10 double vector, “Dh” is a scalar and “length” is a 1x6 double vector. The line:
surface_area = (4*area/Dh)*length
attempts to perform matrix multiplication between the scalar 4 and the vector “area”. Hence the error “Both operands must be matrices. At least one of the operands must be scalar or the inner dimensions must agree” is encountered.
To resolve this error, use the element-wise multiplication operator “.*” instead of the matrix multiplication operator “*” so your line of code would look as follows:
surface_area = (4.*area./Dh)*length
This will solve the error but the matrix multiplication with “length” will still throw an error as the matrix dimensions are incompatible. To resolve that I suggest you use compatible matrices if you want to perform matrix multiplication or use matrices with same dimension if you want to perform element-wise multiplication.
For more information on the element-wise and matrix multiplication refer to the documentation links mentioned below:
I hope this helps
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Thermal Liquid Library 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!