Clear Filters
Clear Filters

Plotting multiple straight lines using random number loop

3 views (last 30 days)
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 Comments
Stuart
Stuart on 16 Apr 2012
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 on 8 Jun 2015
Edited: Walter Roberson on 17 Jun 2016
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

Sign in to comment.

Accepted Answer

Thomas
Thomas on 16 Apr 2012
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 Comments
Stuart
Stuart on 16 Apr 2012
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.

Sign in to comment.

More Answers (3)

Stuart
Stuart on 16 Apr 2012
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 Comments
Thomas
Thomas on 16 Apr 2012
You do not have to call it and the rectangle code in the for loop.
You can call it outside..

Sign in to comment.


Gaspar Cid
Gaspar Cid on 17 Jun 2016
Edited: Gaspar Cid on 17 Jun 2016
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 on 17 Jun 2016
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

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!