Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Daniel Pollard
Daniel Pollard el 10 de Dic. de 2020
Editada: Daniel Pollard el 10 de Dic. de 2020
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.
  3 comentarios
Daniel Pollard
Daniel Pollard el 10 de Dic. de 2020
x is a vector. In this case it's a row of numbers in ascending order, starting at zero for the first element of x and ending at 25 for the 51st element. If you simply type in x >= 0, without the brackets, Matlab reads that and says "yep! All the values of x satisfy that condition" and so it is a vector of true, or 1. You need to tell Matlab "I want you to tell me if this particular component of x is greater than or equal to zero." I can tell that's what you were trying to do, and got halfway there with the loop. However you need to index the vector - that's what the brackets do. Typing x(3) >= 0 returns true if the third element of x is greater than or equal to zero.
That leads me to your second question. Why can't you use ii=0:51? Well, for loops run through these values and run the code for each value in the vector 0:51. The first value is ii=0, so Matlab starts the code and the first thing it hits is x(ii). Well, ii=0. What's the zeroth element of x? Well there isn't one, as that doesn't really make any sense to ask. Some programming languages start indexing from zero, but Matlab indexes from 1.
Remember that if you write out the numbers from 1 to 51 then you'll have written 51 different numbers. No need to include the zero anyway.
yap guang zhe
yap guang zhe el 11 de Dic. de 2020
i understand now, thanks for your explanation

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by