please not that the loop keeps working up to the 4th iteration and it stops and the 5th iteration and reports the error!
error in using syms variable in a for loop
4 views (last 30 days)
Show older comments
the error message is
Error using subsrefIndex exceeds matrix dimensions.
Error in sym/subsref (line 771)R_tilde = builtin('subsref',L_tilde,Idx)
Error in span_depth1 (line 143) x=vpa(x(2),3);
Error in small_loop (line 12) y=span_depth1(fc1,mu_fy,Wl,k3,k1,L);))
and this is my loop
data = importdata('case4.txt');
rerults=sym(ones(1,5));
for i=1:5
fc1 = data(i, 1);
mu_fy = data(i, 2);
Wl = data(i, 3);
k3= data(i, 4);
k1= data(i, 5);
L= data(i, 6);
y=span_depth1(fc1,mu_fy,Wl,k3,k1,L);
rerults(i)=y;
end
the function that I am calling in the loop is attached "span_depth1"
3 Comments
Answers (1)
Walter Roberson
on 4 Nov 2017
Edited: Walter Roberson
on 4 Nov 2017
Line 94
mu_fy=mu_fy*6.895e3; % convert to SI unit ( N/m2)
is within
while mu_r>row_max
which in turn is within
while h-h1>0.001
so you keep multiplying mu_fy by 6.895e3 until eventually it overflows to inf. Everything goes downhill from there. The normrnd results become inf and nan, the row becomes 0 and NaN, the mu_r becomes NaN, that makes As into NaN, which makes y into NaN, and solve(NaN, x) becomes empty. At that point you generate an error when you try to index into the empty result.
I would point out that this kind of problem tends to occur less when you do not re-use variable names for different purposes. For example if you had used
mu_fy_si = mu_fy*6.895e3;
and then calculated with mu_fy_si, then it would have been pulling mu_fy out of the parameters each time and no overflow would have occurred. That would just have made it less optimal to have the conversion inside the double while instead of outside of both, but the problem would not have occurred.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!