Looping with two vectors ?

How can i use a for loop with two vectors ? I keep getting the message "Subscript indices must either be real positive integers or logicals." when trying to run the code. Here it is:
x = [3 3.9 5.1 6.7 10];
y = [0 3 5.5 7.4 9];
xq = 0:0.001:10;
yq = interp1(x,y,xq,'spline');
for ii = xq(3002:10001), yq(3002:10001);
V = (pi*yq(ii))/3 * (xq(ii)^2 + xq(ii)*3 + 9);
display(V);
end
xq and yq are my vectors.

Respuestas (2)

James Tursa
James Tursa el 25 de Oct. de 2016
Editada: James Tursa el 25 de Oct. de 2016

1 voto

It is unclear why you have a loop at all, but maybe this is what you want?
for ii = 3002:10001
Or maybe you just need to form V in a vectorized fashion without a loop?
V = (pi*yq)/3 .* (xq.^2 + xq*3 + 9);

3 comentarios

Frederico Borges
Frederico Borges el 26 de Oct. de 2016
I need to input from position 3002 to 10001 of both xq and yq vectors in the V equation(it's a volume equation). I have to loop so i can plot all the values in a figure. Thank You!
James Tursa
James Tursa el 26 de Oct. de 2016
What would you be plotting? I.e., what is along the x-axis and what is along the y-axis?
Frederico Borges
Frederico Borges el 26 de Oct. de 2016
Editada: James Tursa el 26 de Oct. de 2016
I need to fill with "water" a container, and the equation V is the volume equation. So i need a loop to fill the space in the figure. The figure (container) is plotted by this code:
x = [3 3.9 5.1 6.7 10];
y = [0 3 5.5 7.4 9];
xq = 0:0.001:10;
yq = interp1(x,y,xq,'spline'); %yq = interp1(x,y,xq,'pchip');
plot(xq,yq,'k');
axis([-10 10 0 10]);
hold on
plot(-xq,yq,'k');
xlabel('x');
ylabel('y');
plot([-10 10], [9 9],'k');
plot([-3 3], [0 0],'k');

Iniciar sesión para comentar.

VBBV
VBBV el 8 de Nov. de 2022

0 votos

V = (pi*yq(3002:10001)/3 .* (xq(3002:10001).^2 + xq(3002:10001)*3 + 9);
May be you can just vectorize the equation with given range as above. Or you could also use for loop as below
for ii = 3002:10001
V(ii) = (pi*yq(ii))/3 * (xq(ii)^2 + xq(ii)*3 + 9);
display(V);
end

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 25 de Oct. de 2016

Respondida:

el 8 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by