How to solve: Error: Output argument "variable_name" (and maybe others) not assigned during call to "function_name" ?

I try to get the moment-curvature diagram for the pre-stressed concrete beam section. However, I see this error when I run the code:
function [] = moment_curvature_matlab()
curvature_list = zeros(1, 1000000);
moment_list = zeros(1, 1000000);
ecu = 0.0038;
for ec_top = 0:5e-4:ecu
[curvature, moment] = axis_matlab(ec_top);
curvature_list = [curvature_list, curvature];
moment_list = [moment_list, moment];
end
plot(curvature_list, moment_list);
end
%%%%%%%
function [curvature, moment] = axis_matlab(ec_top)
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
%%%%%%
>> moment_curvature_matlab
Output argument "curvature" (and maybe others) not assigned during call to "moment_curvature_matlab>axis_matlab".
Error in moment_curvature_matlab

 Accepted Answer

If you set a break point just before your i-loop and step through the code, you'll notice that 1) the code never enters the i-loop and 2) the c-loop only has 2 iterations until the condition at the end is met and the 'break' ends the function.
Just as the error message indicates, "curvature" is never assigned because the code never gets to that line.
I suggest learning how to debug using the 2 links above. Step through your code, line by line and determine if each line is behaving the way you expect it to.

6 Comments

Thank you for your reply. I just attached the code. Could you please run it? Because as I mentioned before, the main function "axis_matlab(ec_top)" works properly. It iterates until the force equilibrium is satisfied and when it is, the code breaks. It can be replaced by "return" comment but will that be the solution though? I might be wrong but it is my observation. By the way, thanks for sharing those useful links above.
I just ran the code in your attachment and it contains the same error. Please follow these steps so you can see what's going on.
  1. Open the code you attached in the matlab editor.
  2. On the left side click the black dash mark at line 27 (fs = Es * (c - d) * ec_top / c;) which will place a red circle at that line (see image below).
  3. Press F5 which will run the code and it will stop at the red break point.
  4. Press F10 to step through each line of code. Look at each variable, the values stored in each variable. As you step through the code, notice that you never enter the i-loop and that you leave the c-loop after 2 iterations.
190831 183737-MATLAB R2019a - academic use.jpg
I performed what you expalined above and the i loop never works as you said. I do not understand the reason. To stop the function (if statement), the function first determine the concrete contribution which comes from the i loop. How come it breaks without knowing the values coming from the i loop? Could you suggest some solutions for that? I also updated the code for your review.
"I performed what you expalined above and the i loop never works as you said"
That's because the file you attached in the comment above differs from what you gave us previously. The file attached to your question and copy-pasted to your question is not the same code as what you just attached.
The new version no longer has the error because "curvature" is now assigned outside of the i-loop so if the code never enters the i-loop, the curvature variable still gets assigned.
Looks like that problem is solved.
Lesson learned: if you're working with a different code than us volunteers, there's no way we can really help solve the problem.
Actually I mentioned that I updated the code. Putting curvature out of the I loop obviously will return the value, but my latest question is not related to returning curvature. The function still doesn't get in the I loop while there is no way to break unless if statement is satisfied.
The function does enter the i-loop.
I added a waitbar to your c-loop so I can monitor progress (see code below). Your c-loop has up to 182000 iteration and within each iteration you have a nested i-loop with a variable amount of iterations. So this code could take a very long time.
Note that I haven't tried to understand the code so I can't make recommendations on a faster solution. As is often the case, the best solution is to practice matlab debugging by putting a break at the start of your c-loop, running the code, and stepping through it line by line to understand how the iterations are progressing.
wb1 = waitbar(0,'c-loop'); % Create waitbar
for c = 1e-10:5e-3:h
% Update waitbar #1
waitbar(find(1e-10:5e-3:h == c) / numel(1e-10:5e-3:h), wb1)

Sign in to comment.

More Answers (2)

Hello. You have a wrong values in for loop:
for i = 1:1:(length(lis) - 1)
The variable lis is empty because in line
lis = linspace(0, c, c * 10)
the variable 'c' equals 0, so you sequence is from 0 to 0 with 0 values :)

3 Comments

Hello. Thank you for your reply. Are you sure about it? You see, the loop starts with zero so the lis array will be zero. However, it gets extended with the numbers related to "c" value. Also, the code "axis_matlab(ec_top)" runs perfectly by itself.
Just try it. Change following lines:
  • for c = 0:5e-3:h TO for c = 1e-3:5e-3:h
  • lis = linspace(0, c, c * 10) TO lis = linspace(0, c, 10)
Okay. Your point is correct but I guess MATLAP skips that numerical error and gives the correct results. Anyways, thank you for helping on this one.

Sign in to comment.

I appreciate your effort and thank you. Actually the duration of the code was my another problem. Matlab suggested me to initiate the moment and curvature arrays with zero arrays so there will be a faster performance as they gets result over each iteration. But I couldn't see any difference. There needed to be higher amount of iteration to get more consistent results. I'll try to explain briefly the code so you might suggest some solutions for making it faster. The axial_matlab determines a c value which is the neutral axis depth of the section where the summation of compression forces(Fc coming from i loop) and tension forces(Fs) is equal to zero. To calculate the compression force coming from the contribution of concrete (Fc), I separated the area above the neutral axis c and calculated the cross sectional area and strain value for that specific area respectively. That's what the i loop does simply. And in the main function moment_curvarute, I calculate the c values for each strain value ec_top. Calculating the c value has the higher priority so the number of iteration must go to a higher level. I hope it makes sense to you.

Categories

Find more on Loops and Conditional Statements 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!