How do I return multiple array values?
38 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Star Strider
on 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 Comments
Star Strider
on 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.
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!