필터 지우기
필터 지우기

Plotting multiple straight lines using random number loop

조회 수: 3 (최근 30일)
Stuart
Stuart 2012년 4월 16일
편집: Walter Roberson 2016년 6월 17일
Hi,
I'm trying to plot several lines of different gradient from the same point on a graph. The gradient is determined by the angle phi, which has a mean angle of 45 degrees and a standard deviation of 3.
Considering the equation of a straight line y=mx+c, c is determined by the gradient and coordinate the line passes through.
I am having a problem with getting multiple plots and I'm not sure why. Currently the plot is only a single line.
My code is:
n=100;
x0=3;
y0=4;
x=linspace(0,20,n);
for i=1:n;
phi(i)=45+3*randn;
m(i)=tand(phi(i));
c(i)= y0-m(i)*x0;
end
y=m(i)*x+c(i);
plot(x,y)
Can someone please help me find a solution for this problem?
Thanks.
  댓글 수: 3
Stuart
Stuart 2012년 4월 16일
Yes it is. I thought I'd present the basic problem rather than all the malarky that came with it!
I apologise if its agaisnt the boards rules / etiquette, it was driving me mad and needed help!
Anshuman  Tiwari
Anshuman Tiwari 2015년 6월 8일
편집: Walter Roberson 2016년 6월 17일
Stuart Try This :
n=100;
x0=3;
y0=4;
x=linspace(0,20,n);
for i=1:n;
phi(i)=45+3*randn;
m(i)=tand(phi(i));
c(i)= y0-m(i)*x0;
y=m(i)*x+c(i);
hold on
plot(x,y)
hold off
end
This will work

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

채택된 답변

Thomas
Thomas 2012년 4월 16일
I guess this is what you want..
n=100;
x0=3;
y0=4;
x=linspace(0,20,n);
y=[];
for i=1:n;
phi(i)=45+3*randn;
m(i)=tand(phi(i));
c(i)= y0-m(i)*x0;
y(i,:)=m(i)*x+c(i);
plot(x,y(i,:))
hold on
end
  댓글 수: 2
Stuart
Stuart 2012년 4월 16일
You've nailed it!
Yes! Cheers for that!
Stuart
Stuart 2012년 4월 16일
Hi Thomas,
Thanks for your help.
After modifying the code slightly it has stopped working. Can you see why?
(Answer has been posted below as another answer)
Your help is much appreciated.

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

추가 답변 (3개)

Stuart
Stuart 2012년 4월 16일
Hi, I've slightly modified the code you've helped me with and now it doesn't seem to work. x seems to be changing from what I specified and so the plot only works to a certain x value.
The new code is:
%Finding the maximum and minimum points of entering the hoop
%Initial launch data
x0=20;
y0=4;
n=50;
x=linspace(0,27,n);
y=[];
mean_phi=atand((7.5-y0)/(26.425-x0));
sd_phi=1;
for i=1:n;
%Find the mean angle phi and then actual phi.
phi(i)=mean_phi+sd_phi*randn;
m(i)=tand(phi(i));
c(i)= y0-m(i)*x0;
y(i,:)=m(i)*x+c(i);
plot(x,y(i,:))
hold on
axis equal
Courtlay
hring2= rectangle('position',[26.324 7.399 0.202 0.202], 'curvature',[1,1],'EdgeColor',[0 0 0],'LineWidth',0.02);
end
What have I done wrong?
  댓글 수: 4
Thomas
Thomas 2012년 4월 16일
You do not have to call it and the rectangle code in the for loop.
You can call it outside..
Stuart
Stuart 2012년 4월 16일
Once again, you've nailed it.
Thank you again.

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


Gaspar Cid
Gaspar Cid 2016년 6월 17일
편집: Gaspar Cid 2016년 6월 17일
Hey there guys,
Sorry to bring back this question, but i'm trying to plot multiple RADIAL straight lines from x0 and y0 and i can't do it (not random radial lines, the idea is that they fill a circle), how should i modify this code to make this happen? I guess that must be some change in phi...
I would really appreciate some help,
Thanks

Gaspar Cid
Gaspar Cid 2016년 6월 17일
Well, i finally did this
N=20000
n=100;
x0=3774;
y0=-352;
x=linspace(-N,N,n);
y=[];
for i=1:n;
phi(i)=(360./n).*i;
m(i)=tand(phi(i));
c(i)= y0-m(i)*x0;
y(i,:)=m(i)*x+c(i);
plot(x,y(i,:))
hold on
end
It's seems to work, so i guess i answered myself haha

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by