A
Andrew
Hi, I am trying to write a program to solve roots using the Newtonian
method of roots, and I am recieving several errors which I cannot
figure out... if anyone could give me a point in the right direction
it wuld be appreciaed.
Here is what I have so far:
#include <iostream>
#include <math.h>
using namespace std;
double getF(double x); //The orgional function
double getdF(double x); //The derivative of orgional function
//int getRoot(double &rootEst, double tolerance, int maxIterations);
const double e = 2.718281828459;
double getRoot(double &rootEst, double tolerance, int maxIterations)
{
int n = 1;
while( ( fabs(getF(rootEst)) > tolerance ) && ( n <=
maxIterations ) )
{
rootEst = rootEst - ( getF(rootEst) / getFd(rootEst) );
n++;
}
return n;
}
double getF( x )
{
return ( pow(x, 2) * pow(e, -1 * x) - 2 );
}
double getdF( x )
{
return ( 2 * pow(e, -1 * x) * x - pow(x, 2) * pow(e, -x) );
}
int main () {
double rootEst, //The initial estimate of the root
tolerance; //The stopping tolerance
int maxIterations,
curNumIterations;
cout << "Please enter the initial estimate of the root: ";
cin >> rootEst;
cout << "Please enter the tolerance: ";
cin >> tolerance;
cout << "Please enter the maximum allowed iterations: ";
cin >> maxIterations;
cout << endl << "Using the following data" << endl
<< "Initial guess: " << rootEst << endl
<< "Tolerance: " << tolerance << ", and" << endl
<< "Maximum iterations: " << maxIterations << "." << endl;
curNumIterations = getRoot(rootEst, tolerance, maxIterations);
if(curNumIterations > maxIterations) {
cout << "After " << maxIterations << " iterations no solution was
found!" << endl;
}
else {
cout << "The solution is: " << rootEst << " and it was found in "
<< curNumIterations << " iterations" << endl;
}
return 0;
}
And here are my compile errors:
------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling...
assign8.cpp
..\assign8.cpp(18) : error C3861: 'getFd': identifier not found
..\assign8.cpp(27) : error C2065: 'x' : undeclared identifier
..\assign8.cpp(28) : error C2448: 'getF' : function-style initializer
appears to be a function definition
..\assign8.cpp(33) : error C2448: 'getdF' : function-style initializer
appears to be a function definition
Build log was saved at "file://c:\Documents and Settings\Janet Lannon
\My Documents\Visual Studio 2005\Projects\test\test\Debug
\BuildLog.htm"
test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
I am a student and new to this, im not looking for someone to give me
the complete solution, but hopefully help me realise whats wong with
my own. thanks.
method of roots, and I am recieving several errors which I cannot
figure out... if anyone could give me a point in the right direction
it wuld be appreciaed.
Here is what I have so far:
#include <iostream>
#include <math.h>
using namespace std;
double getF(double x); //The orgional function
double getdF(double x); //The derivative of orgional function
//int getRoot(double &rootEst, double tolerance, int maxIterations);
const double e = 2.718281828459;
double getRoot(double &rootEst, double tolerance, int maxIterations)
{
int n = 1;
while( ( fabs(getF(rootEst)) > tolerance ) && ( n <=
maxIterations ) )
{
rootEst = rootEst - ( getF(rootEst) / getFd(rootEst) );
n++;
}
return n;
}
double getF( x )
{
return ( pow(x, 2) * pow(e, -1 * x) - 2 );
}
double getdF( x )
{
return ( 2 * pow(e, -1 * x) * x - pow(x, 2) * pow(e, -x) );
}
int main () {
double rootEst, //The initial estimate of the root
tolerance; //The stopping tolerance
int maxIterations,
curNumIterations;
cout << "Please enter the initial estimate of the root: ";
cin >> rootEst;
cout << "Please enter the tolerance: ";
cin >> tolerance;
cout << "Please enter the maximum allowed iterations: ";
cin >> maxIterations;
cout << endl << "Using the following data" << endl
<< "Initial guess: " << rootEst << endl
<< "Tolerance: " << tolerance << ", and" << endl
<< "Maximum iterations: " << maxIterations << "." << endl;
curNumIterations = getRoot(rootEst, tolerance, maxIterations);
if(curNumIterations > maxIterations) {
cout << "After " << maxIterations << " iterations no solution was
found!" << endl;
}
else {
cout << "The solution is: " << rootEst << " and it was found in "
<< curNumIterations << " iterations" << endl;
}
return 0;
}
And here are my compile errors:
------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling...
assign8.cpp
..\assign8.cpp(18) : error C3861: 'getFd': identifier not found
..\assign8.cpp(27) : error C2065: 'x' : undeclared identifier
..\assign8.cpp(28) : error C2448: 'getF' : function-style initializer
appears to be a function definition
..\assign8.cpp(33) : error C2448: 'getdF' : function-style initializer
appears to be a function definition
Build log was saved at "file://c:\Documents and Settings\Janet Lannon
\My Documents\Visual Studio 2005\Projects\test\test\Debug
\BuildLog.htm"
test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
I am a student and new to this, im not looking for someone to give me
the complete solution, but hopefully help me realise whats wong with
my own. thanks.