Graphing multiple unique points in a line

조회 수: 1 (최근 30일)
Sean McLafferty
Sean McLafferty 2019년 11월 9일
댓글: 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!
Thanks in advance

채택된 답변

Walter Roberson
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
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by