I need to create a function template to approximate a zero of f(x) using newton\
ID: 3635788 • Letter: I
Question
I need to create a function template to approximate a zero of f(x) using newton's method. Also, I need all of the header files and mechanics of the program to be things that people in a first c++ class would be able to create. Here is my wrong code that needs to be created. Also, it needs to be able to work in 20 or so iterations.I'm having trouble calling the functions. and I'm probably not sending or calling the right things. I appreciate the help.
#include <iostream>
using namespace std;
#include <cmath>
const int nmax = 20;
const double error = 1.0e-6;
int n = 1;
double function(double);
double functionDer(double);
double newton( double);
int main()
{
while ((abs(x) > error) && (n < nmax + 1))
{
x -= (function(x)/functionDer(x));
n++;
}
return n;
}
cout << "Give an initial approximation to a root. ";
cin >> x;
double newton(x)
{
if (n > nmax)
cout << "In " << nmax << " iterations, no solution was found. ";
else
cout << "The solution is " << x << " and it was found in " << n << " iterations. ";
return n;
}
double function (double x)
{
return x*x*x;
}
double functionDer(double x)
{
return3*x*x;
}
Explanation / Answer
Hi, you've made several errors in the code syntax which I've corrected. The code is now syntactically correct - it will compile and run. #include #include using namespace std; const int nmax = 20; const double error = 1.0e-6; int n = 1; double function(double); double functionDer(double); double newton( double); int main() { double x; cout > x; while ((abs(x) > error) && (n nmax) coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.