Finding the root of a function using secant method

hey! I want to find the root of an equation f(x) by using the secant method (http://en.wikipedia.org/wiki/Secant_method).

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
  #include <math.h>
#include <iostream>
using namespace std;

//Define function f(x)
double f(double x, double a, double b)
{
	
		return sin((a*x)/(1+x*x))*atan(b*x)+atan(x);
	
}

int main()

{
//Input a,b parameters constants, lower and upper approximation points as well as a precision value N

double a, b, N, lowerB, upperB;

cout <<"Give me a value for constant a: ";
cin >> a;

cout <<"Give me a value for constant b: ";
cin >> b;

cout <<"Give me a precision:";
cin>> N;

cout <<"Give me lower and upper approximations:";
cin>>lowerB >> upperB;

double RootFinderSMNew(double f, double a, double b, double lowerB, double upperB, int N);

{

	double f_left=f(lowerB, a, b),now=lowerB+N,f_right=f(now, a, b);
	while(f_left*f_right>0 && now<lowerB)
	{
		f_left=f_right;
	    now+=N;
	    f_right=f(now, a, b);
	}
	return now-N/2;
}

	cout << "A root is: "<<
	RootFinderSMNew(f,a,b,upperB,lowerB,N) << endl;
return 0;
}


1) Do I have to keep the function f and RootFinderSMNew in different cpp files?

2) When compiling I get the following errors:

1>------ Build started: Project: Assignment2.1, Configuration: Debug Win32 ------
1> Find Roots Secant method.cpp
1>c:\users\lizi\documents\visual studio 2010\projects\assignment2b\assignment2b\find roots secant method.cpp(46): warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
1>c:\users\lizi\documents\visual studio 2010\projects\assignment2b\assignment2b\find roots secant method.cpp(50): error C2664: 'RootFinderSMNew' : cannot convert parameter 1 from 'double (__cdecl *)(double,double,double)' to 'double'
1> Context does not allow for disambiguation of overloaded function
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What am I doing wrong ? (Probably a lot of things. Any help is appreciated)

Thank you!
What are you doing on line 32?
Topic archived. No new replies allowed.