フィルターのクリア

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 Center および File Exchange2-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