MATLAB: Matlab doesn;t recognise a variable when I run the program

loops

Hi, I'm trying to write a loop where my data enters into either 1 of 2 equations depending on the condition but when I run it, it says that variable 'y' is not recognised and I don;t know why
Here is what I did and I can't figure out what I did wrong
L=25;
E=200^9;
I=350^(-6);
w=6^3;
x=linspace(0,28,51);
for i=0:51
if x>=0 & x<=L/2
y=-((w.*x)./(348.*E.*I)).*((16.*x.^3)-(24.*L.*x.^2)+(9.*L^3));
elseif x>=L/2 & x<=L
y=-((w.*x)./(384.*E.*I)).*((8.*x.^3)-(24.*L.*x.^2)+(17.*L^2.*x)-(L^3));
end
end
plot(x,y)
Here is a screenshot of my question of what I'm trying to do here

Best Answer

Your if statements aren't working because you're comparing a vector (x) to a scalar 0, L/2 or L. Instead what I think you're trying to do is
for ii=1:51
if x(ii) >= 0 & x(ii) <= L/2
y=-((w.*x)./(348.*E.*I)).*((16.*x.^3)-(24.*L.*x.^2)+(9.*L^3));
elseif x(ii) >= L/2 & x(ii) <= L
y=-((w.*x)./(384.*E.*I)).*((8.*x.^3)-(24.*L.*x.^2)+(17.*L^2.*x)-(L^3));
end
end
Notice that I also changed i -> ii because i is a built-in variable in matlab, so it's a good idea to avoid using it as your own variable.
Edit I just made another slight correction to the code - calling x(0) doesn't work, so you have to use for ii=1:51 rather than ii=0:51.