for loop and calculaion

3 次查看(过去 30 天)
Michael Green
Michael Green 2016-10-8
I am writing a code for a class project where we have to calculate the deflection and stress of 4 50 meter beams with varying height, thickness, width and cost, as a 100N load crosses it. I seem to have most of the code working correctly(though I could have the whole thing screwed up) but am stuck on calculating the deflection from point a < x < l where a is a 591 element array "a = (0:0.1:l);" and l is 50 meters. I keep getting errors either regarding "matrix dimensions must agree" or "subscript indices must either be real positive integers or logicals" I have been playing around with it but cant seem to get it to work, so any help would be appreciated. Thank you.
clear,clc, close all
E = 200; %define elastic modulus
el = 250; %define elastic limit
P = 100; %define load in N
h = [0.25,0.2,0.25,0.2]; %define height
w = [0.1,0.1,0.1,0.1]; %define width
t = [0.02,0.02,0.05,0.04]; %define thickness
cost = [129,104,158,126]; %define cost per meter
I = (((h.*w.^3)./12) - ((h-2*t).*(w-2*t).^3)/12); %calculate moment of inertia
c = h./2;
l = 50; %length in meters
a = (0:0.1:l); %define array along length of beam
b = (l-a); %define length from point a to end of beam
for n = 1:length(a)
s(1:4,n) = (P*a(n)*b(n).*c)./(I*l);
end
%plot(a,s)
for x = 1:length(a)/2 %calculate deflection from 1 to a, not sure if correct
for d = 1:4 %run calculation for each beam
d = ((P.*b(x).*x)./(6*l*E*I)) .* (l.^2 - x.^2 - b(x).^2);
end
end
%below is the section giving me trouble
for x = a-l
for d2 = 1:4
for mi = I(1:4)
d2 = (((P.*b)./(6.*l.*E.*I)).*((l./b).*(x-a).^3)+(l^2 - b.^2).*(x-x.^3))
end
end
end}

回答(2 个)

Massimo Zanetti
Massimo Zanetti 2016-10-8
Sure it doesn't work. This line
d2 = (((P.*b)./(6.*l.*E.*I)).*((l./b).*(x-a).^3)+(l^2 - b.^2).*(x-x.^3))
there are for example sizes b is 1x501 I is 1x3 and there are also others.
In element-wise operations all elements must be the same size.
  2 个评论
Michael Green
Michael Green 2016-10-8
Ok, I rewrote the code and it runs, hopefully correctly, could you please take a look and see if it seems to be correct or just giving wrong result? Thank you.
for x = length(a)-l
for d2 = 1:4
for mi = I(1:4)
d2 = -(((P*b)./(6*l*E*mi)).*((l./b).*((x-a).^3)+(l^2 - b.^2).*(x-x^3)))
end
figure
plot(d2)
end
end
Massimo Zanetti
Massimo Zanetti 2016-10-9
I don't know if it is correct.. it depends on your final goal. If I have to guess, your are not doing right.. what should be the final length of the result d2? 4 or 501?

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-10-9
1) When you have a "for" loop over one loop variable, and you change the value of the loop variable within the loop, then as soon as the next iteration starts, a hidden (unchanged) copy of the loop variable will be tested and if it is already at the last permitted value then the loop will be exited, leaving the loop variable at the value that it was modified to, but if the hidden copy of the loop variable was not the last permitted value then the loop variable will be set to the next permitted value, as if the loop variable had not been modified.
In other words, don't assign to variable "d2" inside "for d2" because at the very least it is confusing and it is probably not going to do whatever you intended.
2) The structure
for mi = I(1:4)
d2 = -(((P*b)./(6*l*E*mi)).*((l./b).*((x-a).^3)+(l^2 - b.^2).*(x-x^3)))
end
will do four iterations, setting mi to each of the first four values of I in turn (provided that I is a row vector, which it indeed is in this case.) During each of those 4 iterations, the entire variable d2 will be written over. As d2 does not appear on the right hand side, the effect is the same as
mi = I(4);
d2 = -(((P*b)./(6*l*E*mi)).*((l./b).*((x-a).^3)+(l^2 - b.^2).*(x-x^3)))
which is to say, only the last iteration would be stored.
You have the same two basic problems with
for d = 1:4 %run calculation for each beam
d = ((P.*b(x).*x)./(6*l*E*I)) .* (l.^2 - x.^2 - b(x).^2);
end
except to that you add the confusion of trying to figure out what you could possibly be wanting the output to be.
Your "for" loops should almost always be of the general form:
variable_values = row variable name, or explicit list of permitted values, or a colon range;
for variable_idx = 1 : length(variable_values)
variable = variable_values(variable_idx);
.... do some calculation ...
output_variable(variable_idx) = result;
end
Exception: if the list of permitted values is already (1 : some_number) then you can abbreviate the form to
for variable_idx = 1 : some_number
... do some calculation ...
output_variable(variable_idx) = result;
end
because in this limited case, variable_values(variable_idx) would be the same as variable_idx so it would be redundant to have a separate array.
In any case in which you are iterating over values that do not happen to be identical to positive integers beginning near 1, then you should be using the form of creating a vector of permitted values and then iterate over the indices into that vector and store the results indexed by the indices.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by