Clear Filters
Clear Filters

What other way can I run a an array through MATLAB SIMSCAPE without error i.e. "Operand must be scalar"?

3 views (last 30 days)
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?

Answers (1)

Avadhoot
Avadhoot on 31 Oct 2023
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:
  1. Multiplication - MATLAB times .* - MathWorks India
  2. Matrix multiplication - MATLAB mtimes * - MathWorks India
I hope this helps

Categories

Find more on Thermal Liquid Library in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!