5
I have a problem where I need to find the intersection of two mathematical functions. I have the formula of the first function and two coordinates, where ab and cd are my points. To pass these coordinates to a function, I use the line equation by two points, and this is my problem.
The first function is represented by f(x) = |x| - cos(3x), who create this drawing:
Since the formula is already ready, I wrote it in C# with:
static Vector2 CossineFormula(int x)
{
double y = Math.Sqrt(Math.Pow(Math.Abs(x), 2)) - Math.Cos(x * 3);
return new Vector2(x, y);
}
So far, so good. Now, I have two points that I mentioned, and I can’t go on with the two-point line equation because I can’t define a function dynamically, or at least I can’t find how.
The intersection is given by the continuation of the points ab and cd until you create the interception Ef:
I can’t find that intersection because I can’t pass the two points to mathematical function.
I tried to use the formula of the ramp (Slope) as it is in the above mentioned tutorial, but I stopped in this part (in the tutorial is Step 2):
static Vector2 CossineIntersection(Vector2 a, Vector2 b)
{
double distance = Math.Sqrt(Math.Pow(b.X - a.X, 2) + Math.Pow(b.Y - a.Y, 2));
double slope = (b.Y - a.Y) / (b.X - a.X);
double intercept = (2 * slope); // onde continuo com b?
}
Slope was calculated, but at this stage:
Substitute the Slope for’m' in the Slope Intercept form of the Equation
y = mx + b
I went to y = (2 * slope) but I didn’t have to continue b.
I also asked about how to find this intersection in Mathematics.
All the ways of doing intersection, I can’t perform in programming. I know it’s possible, but I can’t find anywhere anything like.
Note: is not an intersection between two lines, but between a line and a function.


Thanks for your help. I tested your code using the dots
0, 4and1, 3and the intersection gaveX = 3,033andY = 3.051. In this function calculator, these same points give an intersection of approximatelyX = 2.35andY = 1.66. What is wrong?– CypherPotato
@Cypherpotato Fixed. Sorry I had made a mess initially.
– Victor Stafusa
Victor, I think you got confused in the condition to define bisection. Do not just check the sign of
d, which is the midpoint, but rather compares it to the values ofv1andv2current. In this example, you updatedv1whend > 0. This would work if the curve were increasing, but it is decreasing. Ifd > 0you will need to update the point that is also positive, be itv1orv2.– Woss
Considering the above comment, I think it only worked by "luck" because the curve is decreasing and you update
v2whenv3 > 0. This same condition will not work if the curve is increasing (or I’m wrong?).– Woss
@Andersoncarloswoss It is based on the fact that the
x1always produces av1negative and that thex2always produces av2positive. This is not something very general, but to make it into something more general, I would also have to embrace the choice ofx1and ofx2initial and parameterize the function to be bisected, which would leave a little more complicated than the author of the question needs.– Victor Stafusa
Ah, truth, there’s this premise xD I was already thinking of something for when it was
v1positive andv2negative also haha– Woss