Plotting multiple straight lines using random number loop

3 次查看(过去 30 天)
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
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..

请先登录,再进行评论。


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