lines that meet each other

6 visualizaciones (últimos 30 días)
Riha Z
Riha Z el 9 de Mzo. de 2024
Editada: Riha Z el 21 de Mzo. de 2024
Hello
I make two sets of lines that meet each other from two different origins at a distance of 10 cm from each other. The angle of the lines is a multiple of 5. The points that I specified are the meeting points of the lines with an angle of 5 theta and 5 (theta + 1). I want a program in MATLAB that connects these points.
  2 comentarios
Voss
Voss el 9 de Mzo. de 2024
Movida: Voss el 9 de Mzo. de 2024
D = 10;
p1 = [D/2 0];
p2 = [-D/2 0];
th = 0:5:90;
costh = cosd(th);
sinth = sind(th);
a1 = p1(:)+D*[-costh; sinth];
a2 = p2(:)+D*[costh; sinth];
for ii = 1:numel(th)
line([p1(1) a1(1,ii)],[p1(2) a1(2,ii)])
line([p2(1) a2(1,ii)],[p2(2) a2(2,ii)])
end
axis equal
Riha Z
Riha Z el 9 de Mzo. de 2024
Movida: Voss el 9 de Mzo. de 2024
Thank you for your guidance
I mean the curved code that I marked with the pencil.

Iniciar sesión para comentar.

Respuestas (2)

John D'Errico
John D'Errico el 9 de Mzo. de 2024
Editada: John D'Errico el 9 de Mzo. de 2024
I think you have what is really a simple question. But you need to formulate it in mathematics, and that is often difficult without practice and experience.
What are the equations of the line families?
We have one line family that passes through the point (-5,0), and has various positive slopes, given in degrees. A second family lives symmetrically on the other side of the origin through (5,0) and has correspondingly negative slopes.
First, FORMULATE THE FAMILIES OF LINES! I'll use symbolic tools here. The parameter S1 is a slope, in degrees. But we can convert that to an actual slope easily enough using a tangent function.
I'll use positive numbers for both S1 and S2, adding a negative sign in the formulas.
syms x
syms S1 S2 positive
LineFam1 = tand(S1)*(x + 5)
LineFam1 = 
LineFam2 = tand(-S2)*(x - 5)
LineFam2 = 
You can see that when x == -5, line family 1 crosses the x axis. Line family 2 crosses the axis at x==+5. I'll plot one pair of lines...
fplot(subs(LineFam1,S1,30),[-5,5])
hold on
fplot(subs(LineFam2,S2,35),[-5,5])
hold off
legend('Line family 1','Line Family 2')
grid on
As you can see, the intersection point lies on the right side of the y axis, and above zero.
Can we solve for where the curves intersect? That part is easy enough.
xsol = solve(LineFam1 == LineFam2,x)
xsol = 
That is the x coordinate of the solution. And the y coordinate can be found by substituting into either of the equations.
ysol = subs(LineFam1,x,xsol)
ysol = 
Yes, I know, that does not look very pretty, but things will get better eventually, well, or not. Its just mathematics. What matters is the plot. Next, we need to see that the intersections you are looking for are those where S1 is different from S2 by exactly 5 degrees. So put that into the formulas. If S2 is larger than S1, then the intersection point will lie on the right hand side of the y axis. (Remember I am using positive numbers for both S1 and S2, the slope S2 is negated in the line equation.)
xsol = subs(xsol,S2,S1 + 5)
xsol = 
ysol = subs(ysol,S2,S1 + 5)
ysol = 
We can see the result is this thing as a function of the slope S1. Finally, plot the resulting curve of intersections. I'll vary the slope parameter S1 (in degrees) from 5 degrees to 60 degrees. Note that when S1 is greater than 45 degrees, the curve actually starts to move away from the y axis. This will get more pronounced when S1 grows larger yet.
fplot(xsol,ysol,[5,60])
xlim([0,2])
ylim([0,10])
grid on
As you can see, we get a nice (what seems to be hyperbolic) curve of intersection points, where the parameter of interest is S1, the slope of line 1 (IN DEGREES, HOW BARBARIC!) The axes on that plot are still x and y.
With only a little extra effort, I could overlay the actual lines on top of that. But what you seem to care about is the curve of the intersection points, and I derived that for you, in the form of the locus (xsol,ysol).
We can generate the negative intersection point locus easily enough. There we would have S2=S1-5 instead.
Finally, if the difference in slopes was larger or smaller than the desired 5 degrees, this would simply generate a subtly different hyperbolic curve of intersections. It would still have the same characteristic shape. With yet more effort than this surely deserves, we could probably even show if the locus truly does lie on a conic form, so a true hyperbola.
Again, all of this was easy enough, almost trivially direct, once I formulated the question properly in terms of mathematics. That is often the difficult part.
  3 comentarios
John D'Errico
John D'Errico el 17 de Mzo. de 2024
I'm sorry, but I see no need to have a loop there, nor any if test at all. The code I wrote draws the exact curve you asked to see, using no loop at all, or any if test.
Riha Z
Riha Z el 17 de Mzo. de 2024
This code does not work for me, can you explain more.

Iniciar sesión para comentar.


Matt J
Matt J el 9 de Mzo. de 2024
  2 comentarios
Riha Z
Riha Z el 9 de Mzo. de 2024
Thanks, but this link doesn't show anything for me.
Matt J
Matt J el 9 de Mzo. de 2024
Aren't you trying to find intersections of lines?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by