Graphing multiple unique points in a line

1 visualización (últimos 30 días)
Sean McLafferty
Sean McLafferty el 9 de Nov. de 2019
Comentada: Rena Berman el 12 de Dic. de 2019
Hi!
I'm writing a code in a for loop that is meant to find the value of "m" for various differnt values of theta. For each range of theta, the equation for m and the variables needed to find m may change. The problem I am having is that I can only get my code to graph points and not a line. Each point is a differnt color so it seems like my code is treating them as unique values. How do I get them to graph into a line? Heres the code a snippet of code:
for theta = 0 : pi/24 : pi
if (0 <= theta) && (theta < pi/4)
p = 5;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m = cy * (x + x1);
elseif (pi/4 <= theta) && (theta <= pi/2)
p = (28/pi)*(theta-(pi/4))+5;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m = cy * (x + x1);
elseif (pi/2 < theta) && (theta <= pi)
p = (6/pi)*(theta-pi/2)+12;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m = cy * (x + x1);
end
hold on;
plot(theta, m, 'o')
hold off;
end
If you plot this it just does dots, since I have 'o', but if I change it to '-k' the graph goes blank. Please help!
Thanks in advance

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2019
thetavals = 0 : pi/24 : pi;
for thetaidx = 1 : length(thetavals)
theta = thetavals(thetaidx);
if (0 <= theta) && (theta < pi/4)
p = 5;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m(thetaidx) = cy * (x + x1);
elseif (pi/4 <= theta) && (theta <= pi/2)
p = (28/pi)*(theta-(pi/4))+5;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m(thetaidx) = cy * (x + x1);
elseif (pi/2 < theta) && (theta <= pi)
p = (6/pi)*(theta-pi/2)+12;
h = .09 * sin(theta);
x = .09 * cos(theta);
b = asin(h / .2);
x1 = .2 * cos(b);
f = (p / cos(b));
cy = f * sin(b);
m(thetaidx) = cy * (x + x1);
else
error('theta invalid')
end
hold on;
plot(thetavals, m, 'o')
hold off;
end
I recommend that you learn how to use logical indexing.
  1 comentario
Sean McLafferty
Sean McLafferty el 9 de Nov. de 2019
Thanks Walter! This helped a lot. To be honest I dont even know what logical indexing is, so I will definetly look into it. Have a good weekend

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Networks en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by