Solving Equations with Function Pointers Difficulty

Hey everyone!
I'm trying to code a program that is designed to run an infinite for loop that finds the (x, y) coordinate at which two functions intersect. I've completed writing the program but when running the program, nothing happens.

This portion is where I believe my mistake was made. Any pointers or hints as to what went wrong is appreciated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (;;) {
		// Calculate the y-coordinate's of the Midline.
		midLine = (low + high) / 2;
		fMid = f(midLine);
		gMid = g(midLine);
		// if the y-coordinate of fMid == gMid, break and return midLine
		if (fMid == gMid) break;

		if (fLow < gLow && fHigh > gHigh) {
			high = midLine;
			fHigh = fMid;
			gHigh = gMid;
		}
		else {
			low = midLine;
			fLow = fMid;
			gLow = gMid;
		}
	}




I believe the problem lies in my math logic but no matter how I think of it, I can't get my brain to understand where I went wrong. I'd appreciate any tips or hints as to where my logic went faulty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <stdio.h>
#include <stdlib.h>
#include <math.h>

double solveEq (double(*f)(double x), double(*g)(double x), double low, double high);
double quadratic (double);
double cubic (double);

int main() {
	double lo = 0, hi = 0;
	printf ("Enter two values for lo and hi: ");
	scanf_s ("%lf%lf", &lo, &hi);
	double x = solveEq(quadratic, cubic, lo, hi);
	printf("\nSuccessful. x = %f.\n", x);
	system("Pause");
	return 0;
}

double solveEq (double(*f)(double x), double(*g)(double x), double low, double high) {
	double fLow = (*f)(low);
	double fHigh = (*f)(high);
	double gLow = (*g)(low);
	double gHigh = (*g)(high);
	// check if fLow == gLow intersects or fHigh == gHigh. If they do, break and return X. 
	if (fLow == gLow) return low;
	if (fHigh == gHigh) return high;
	double midLine = 0, fMid = 0, gMid = 0;
	for (;;) {
		// Calculate the y-coordinate's of the Midline.
		midLine = (low + high) / 2;
		fMid = f(midLine);
		gMid = g(midLine);
		// if the y-coordinate of fMid == gMid, break and return midLine
		if (fMid == gMid) break;

		if (fLow < gLow && fHigh > gHigh) {
			high = midLine;
			fHigh = fMid;
			gHigh = gMid;
		}
		else {
			low = midLine;
			fLow = fMid;
			gLow = gMid;
		}
	}
	return midLine;
}

double quadratic(double x) {
	return x*x;
}

double cubic(double x) {
	return x*x*x;
}
Last edited on
You are making the problem twice as difficult as you need. Define a single function
h = g - f
and then you are asking where h = 0, with low and high (of one function only) corresponding to negative or positive.

As far as your own code is concerned you would have to replace line 36 and following by a single comparison of fmid and gmid and determining whether to let mid replace low or high.

You also don't test whether your initial conditions represent a legitimate starting point.
Topic archived. No new replies allowed.