# Graphing multiple unique points in a line

2 ビュー (過去 30 日間)
Sean McLafferty 2019 年 11 月 9 日
Commented: Rena Berman 2019 年 12 月 12 日
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!

#### 1 件のコメント

Rena Berman 2019 年 12 月 12 日

サインイン to comment.

### 採用された回答

Walter Roberson 2019 年 11 月 9 日
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 件のコメント

Sean McLafferty 2019 年 11 月 9 日
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

サインイン to comment.