I'm trying to find Intersection points between a line, which is given by 2 points, and a generic function (I want it to be able to handle polynomial functions, circles/ovals, lines and all combinations). The code works fine for some cases, but I'm in a world of hurt, if the line becomes vertical/horizontal.
1) How should I change the f[x] to allow lines to be vertical or horizontal. I don't allow P1=P2, so line is always defined.
(*Initial Settings*)
Clear["Global`*"]
P1 = {-2, 2};
P2 = {2, -2};
f[x_] := (P1[[2]] - P2[[2]])/(P1[[1]] - P2[[1]]) (x - P1[[1]]) +
P1[[2]];
g[x_] := Piecewise[{{x^2, x <= 0}, {x, 0 < x < 1}, {x^2, x >= 1}}];
gr1 = Plot[f[x], {x, -6, 6}];
gr2 = Plot[g[x], {x, -6, 6}];
points = Graphics[{Red, PointSize[0.015], Point[P1], Point[P2]}];
(*Finding the solutions*)
IntersectionPointsX = x /. Solve[f[x] == g[x], Reals];
IntersectionPoints = {};
Do[AppendTo[
IntersectionPoints, {IntersectionPointsX[[k]],
f[IntersectionPointsX[[k]]]}], {k, Length[IntersectionPointsX]}];
If[Length[IntersectionPoints] > 0,
Show[gr1, gr2, points,
ListPlot[IntersectionPoints, PlotStyle -> PointSize[0.02]],
Axes -> True, PlotRange -> {{-6, 6}, {-6, 6}}, AspectRatio -> 1] ,
Show[gr1, gr2, points, Axes -> True, PlotRange -> {{-6, 6}, {-6, 6}},
AspectRatio -> 1] ]
If I choose these settings:
P1 = {-3.75, -4};
P2 = {-1.5, 0};
g[x_] := Piecewise[{{Undefined, x < -4}, {x, -4 < x < -2}, {Sqrt[
2^2 - (x)^2], -2 < x < 2}, {Undefined, x >= 2}}]
I'm missing one solution. The problem is that g[x] "jumps" the right spot and the vertical line is only graphical, so the solve cant find it.
2) Is there an easy way to fix g[x] here, so I can solve for all intersections?
3) Using only half of the circle function isn't quite optimal either probably, but fixing that isn't a priority atm.
All general improvements to code also welcome of course, I have still a lot to learn.
Thanks in advance.
EDIT:
I changed line function to parametric, which solved all horizontal/vertical problems I had earlier. Then I changed g[x] into parametric, using trivial settings of x=t But solve still doesn't find all solutions.
For example: w/o parameterization Mathematica finds both solutions
Clear["Global`*"]
f[x_] := -x + 1;
g[x_] := Piecewise[{{x^2, x <= 0}, {x, True}}];
Solve[f[x] == g[x]]
{{x -> 1/2}, {x -> 1/2 (-1 - Sqrt[5])}}
then I parameterize and lose one point:
Clear["Global`*"]
f[t1_] := {t1, -t1 + 1};
g[t2_] := Piecewise[{{{t2, t2^2}, t2 <= 0}, {{t2, t2}, True}}];
Solve[f[t1] == g[t2]]
{{t2 -> 1/2, t1 -> 1/2}}
Perhaps Piecewise isn't the best way to combine those parametric functions or my trivial parameterization is not good enough?
All comments are welcome and thanks for all answers!
f
andg
as parametric functionsf[t1_]:={fx[t1],fy[t1]}
andg[t2_]:={gx[t2],gy[t2]}
, then solve the system of equationsSolve[Thread[f[t1]==g[t2]],{t1,t2}]
$\endgroup$p
+t*p2` wherep1
,p2
are the points it goes through. Then set ``p+t*p2=={x,g[x]}
and solve forx
(you have two eqn in two unknowns). $\endgroup$Solve[PiecewiseExpand[f[t1] == g[t1]]]
? $\endgroup$