Pointer Function inquiries.

Hello, I need help to implement a pointer function for my program, but I lost the class material to finish it. This code needs to be able to run with the multiple functions displayed with the same functions/derivative functions below int main(). I think I need to use void and typedef somewhere, but all my efforts result in errors or redefinitions. All in all, i have no idea how to do so.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 //Finding Zeros Project
//
//This program will calculate the bisection and newtons method

#include<iostream>
#include<cmath>

using namespace std;

double bisection(double a, double b, double tol, int maxIts);
double newton(double x0, double tol, int maxIts);
double f(double x);
double df(double x);

int main()
{
	double a = 1;
	double b = 5;
	double x0 = .523;
	double tol = .00001;
	int maxIts = 100;
	
	cout << "Bisection" << "\t" << "Newton" << endl;
	cout << bisection(a, b, tol, maxIts) << "\t\t" << newton(x0, tol, maxIts) << endl;
	
	
	return 0;
}



double bisection(double a, double b, double tol, int maxIts)
{
	double x, fpos, fneg, fx;
	int counter = 1;
	
	x = (fpos + fneg) / 2;
	if(f(a) < 0) 
	{
		fpos = b;
		fneg = a;
	}
	
	else 
	{
		fpos = a;
		fneg = b;
	}
		
	while (fabs(f(x)) > tol && counter <= maxIts)
	{	
		if(f(x) > 0)
		{
			fpos = x;
		}
		else 
		{
			fneg = x;
		}
		x = (fpos + fneg) / 2;
		
		counter++;
	}
	cout << counter << "  counters" << endl;
	return x;
	
}

double newton(double x0, double tol, int maxIts)
{

	int counter = 1;
	
	
	while (fabs(f(x0)) > tol && counter <= maxIts)
	{
		x0 = x0 - (f(x0))/(df(x0));

		
		counter++;
	}
	cout << counter << "\t\t";
	return x0;
}

double f(double x)
{
	return x * x - 3;
}

double df(double x)
{
	return 2 * x;	
}

double f(double x)
{
	double angle = 11.5 * (acos(-1) / 180);
	double D = 55;
	double l = 89;
	double h = 49;
	double A = l*sin(angle);
	double B = l*cos(angle);
	double C = ((h + 0.5*D)*sin(angle)-0.5*D*tan(angle));
	double E = ((h + 0.5*D)*cos(angle)-0.5*D);

	
	return A*sin(x)*cos(x)+B*sin(x)*sin(x)-C*cos(x)-E*sin(x);
}
double df(double x)
{
	return -89 * sin(23 * (acos(-1)) / 360) * sin(x) * sin(x) + 178 * cos(23 * (acos(-1)) / 360) * cos(x) * sin(x) + (153 * sin(23 * 
	(acos(-1)) / 360) / 2 - 55 * tan(23 * (acos(-1)) / 360) / 2)  *sin(x) + 89 * sin(23 * (acos(-1)) / 360) 
	* cos(x) * cos(x) - (153 * cos(23 * (acos(-1)) / 360) / 2 - 55 / 2) * cos(x);
}

double f(double x)
{
	double V = 12.4;
	double L = 10;
	double r = 1;
	double h;
	h = x;
	
	return .5 * acos(-1) * r * r * L - L * r * r * sin(h / r) - L * h * sqrt(r * r - h * h) -V;
}

double df(double x) 
{
	double L = 10;
	double r = 1;
	double h;
	h = x;
	
	return (-L * r * r) / (r * sqrt(1-((h/r) * (h/r)))) - (L * h * h) / sqrt(r * r - h * h);
}

double f(double x)
{
	const double AGRAV = -32.17;
	double w = .3923; //this is supposed to be .4 but it wasn't giving me and answer of -0.317055
	double xt = 1.7;
	double t = 1;
	double e = 2.718;
	
	return -(AGRAV / (2 * w * w)) * ( ((pow(e, w * t) - pow(e, -(w * t))) / 2) - sin(w * t)) - xt;
}

double df(double x)
{
	const double AGRAV = -32.17;
	double w = .3923; //this is supposed to be .4 but it wasn't giving me and answer of -0.317055
	double xt = 1.7;
	double t = 1;
	double e = 2.718;
	
	return ((AGRAV * (.5 * (pow(e, w*t) - pow(e, -(w*t))) - sin(w*t))) / pow(w,3)) - (((AGRAV / 2)
	* (.5 * (pow(t*e,w*t) + pow(t*e, w*t)) - t*cos(w*t))) / pow(w,2));
}

For how to write a pointer to a function and how to reference it, read up at The Function Pointer Tutorials (http://www.newty.de/fpt/fpt.html).

You have multiple functions named 'f' and multiple functions named 'df'. Each should have it's own name. You could go as simply as 'f1' and 'df1', 'f2' and 'df2', etc.

The point, then, is that you can pass a pointer to said functions as argument to the same routine:

 
  cout << newton(double x0, double tol, int maxIts, &f1) << "\n";

Hope this helps.
closed account (48T7M4Gy)
At the end of this article:
http://www.cplusplus.com/doc/tutorial/pointers/
Topic archived. No new replies allowed.