Graphing multiple unique points in a line
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sean McLafferty
le 9 Nov 2019
Commenté : Rena Berman
le 12 Déc 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
Réponse acceptée
Walter Roberson
le 9 Nov 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.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!