I have problem to make code of deflection of beam

2 views (last 30 days)
I got this error, 'The variable 'y' appears to change size on every loop iteration. Consider preallocating for speed. please help me ...
function beam()
E = 210* 10^9;
L = 1;
W0= 4000;
I = (0.05 * 0.12^3) / 12;
x=linspace(0,1,101);
for i=1:101
if x(i) < 0.5
y(i) = ((W0* L)/(4 * E * I)) * x(i)^2 - (W0/( 6 * E* I))* x(i)^3;
else
y(i) = ((W0*L)/(4 * E * I)) * x(i)^2 - (W0 / (6 * E * I)) * x(i)^3 + (W0 / (6 *E * I))*(x(i)- L/2)^3;
end
end
plot(x,y);
grid off;
end
Thanks

Answers (1)

Joseph Cheng
Joseph Cheng on 20 May 2015
That is not an error. It is a warning. which based on the size of your function and number of iterations should not be an issue. It is saying if y will grow significantly large it may take some time to add additional data to it unless you pre-allocate the array.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!