Graphing multiple unique points in a line

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 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.
  1 Comment
Sean McLafferty
Sean McLafferty on 9 Nov 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

Sign in to comment.

More Answers (0)

Categories

Find more on Networks in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!