Borrar filtros
Borrar filtros

Plotting multiple straight lines using random number loop

3 visualizaciones (últimos 30 días)
Stuart
Stuart el 16 de Abr. de 2012
Editada: Walter Roberson el 17 de Jun. de 2016
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 comentarios
Stuart
Stuart el 16 de Abr. de 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 el 8 de Jun. de 2015
Editada: Walter Roberson el 17 de Jun. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Thomas
Thomas el 16 de Abr. de 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 comentarios
Stuart
Stuart el 16 de Abr. de 2012
You've nailed it!
Yes! Cheers for that!
Stuart
Stuart el 16 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

Stuart
Stuart el 16 de Abr. de 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 comentarios
Thomas
Thomas el 16 de Abr. de 2012
You do not have to call it and the rectangle code in the for loop.
You can call it outside..
Stuart
Stuart el 16 de Abr. de 2012
Once again, you've nailed it.
Thank you again.

Iniciar sesión para comentar.


Gaspar Cid
Gaspar Cid el 17 de Jun. de 2016
Editada: Gaspar Cid el 17 de Jun. de 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 el 17 de Jun. de 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

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by