Filter löschen
Filter löschen

How do I return multiple array values?

51 Ansichten (letzte 30 Tage)
Said Bolluk
Said Bolluk am 31 Aug. 2019
Kommentiert: Star Strider am 1 Sep. 2019
This is a code that calculates the curvature and moment capacity of the given pre-stressed concrete beam section. I try returning two values from the
function: curvature and the total_moment. However, there is only one output which is the first element of the returning array. The code is:
function [curvature, moment] = axis_matlab(ec_top)
%curv = 0;
%moment = 0;
%ec_top = 0.001;
h = 910;
d = 795;
ht = 180;
b = 450;
bw = 140;
As = 1700;
%fp = 1100;
fcu = 50;
Ec = 12680 + 460 * fcu;
Es = 200 * 1e3;
ec0 = 2 * fcu / Ec;
%ecu = 0.0038;
for c = 0:5e-3:h
fs = Es * (c - d) * ec_top / c;
Fs = fs * As * 1e-3;
lis = linspace(0, c, c * 10);
total_force = Fs;
total_moment = Fs * (d - h / 2) * 1e-3 * -1;
for i = 1:1:(length(lis) - 1)
if lis(i) <= ht && lis(i) >= h - ht
b_i = b;
else
b_i = bw;
end
h_i = lis(i + 1) - lis(i);
y_i = (h / 2) - lis(i) + (h_i / 2);
e_i = c - lis(i) + (h_i / 2);
ec_i = ec_top * (c - e_i) / c;
fc_i = fcu * ((2 * ec_i / ec0) - (ec_i / ec0) ^ 2);
Fc_i = fc_i * h_i * b_i * 1e-3;
M_i = Fc_i * y_i * 1e-3;
total_force = total_force + Fc_i;
moment = total_moment + M_i;
curvature = ec_top / c;
end
if -1 < total_force && total_force < 1
break;
end
end
end
"""
I ended up with this result:
>> axis_matlab
ans =
1.1270e-05
How do I get all return values from the function?
Additionally, I want to optimize this function as it iterates two many numbers to get the exact "c" value. Actually, the function was based on Python Language but I turned into Matlab document to get faster on running the code. As its known, there are different types for keeping the data (lists, tuples, sets and dicts.) in Python, to get the faster results, the list type must be adjusted properly. Is there any similar way to optimize the above function?

Akzeptierte Antwort

Star Strider
Star Strider am 31 Aug. 2019
If you want both outputs, ask for them!
Call your function as:
[curvature, moment] = axis_matlab
If your function is working correctly, both values will be returned to your workspace.
Additionally, I want to optimize this function as it iterates two many numbers to get the exact "c" value.
I have no idea what your function is doing. It may be as efficient as it can be. Some things just require iterating until the code arrives at the correct result.
  4 Kommentare
Said Bolluk
Said Bolluk am 1 Sep. 2019
The above code is just a part of the main code that I attached in the post. I will try to explain the code "moment_curvature_matlab" briefly. When a section is loaded, moment causes forces to occur in couples: compression and tensile. Concrete material produces the compression while steel reinforcement produces tensile forces. The neutral axis is where the total_force is equal to zero (in code, it get closer to zero: if statement). To find the neutral axis, I separated the concrete material which is above the neutral axis (compression zone) as it is a more precise way. After finding neutral axis depth "c" for each strain value (ec = 0 : 0.0038), I calculate the curvature and moment values and, finally plot this kind of behaviour. There would be tons of iteration if its desired to have more consistent values. I wonder if there is any list-manipulation to make code run faster.
Star Strider
Star Strider am 1 Sep. 2019
I do not understand what your code is doing. If you could provide symbolic expressions of the calculations you are doing, it might be possible to suggest an optimization function approach to it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by