Write a C++ program file that has three functions:
void substitution (double&, int&);
void newton (double&, int&);
and
int main( ).
The main function prints the following function to the screen:
f(x)=x3 + ln(x) − 2
and asks the user to guess the value of the root. The main function then calls the functions substitution and newton. The substitution function determines the root using the user's guess and the substitution method. The newton function determines the root using the user's guess and the newton method (fyi: the derivative of ln(x) is 1/x). The main function then prints to the screen, for each method, the root, the number of iterations required, and the value of f(x) for the root determined.
Use 0.0001 tolerance when determining the roots. The value double& in the void functions is the guess value transferred to the function, and the root value transferred back from the function after it has run. To minimize coding, it is recommended you also use a single-result function double fofx(double) to calculate the value of f(x).
Hint: The root is between 1 and 2.
Caution: if you guess a root too far from the solution, the methods may not work, particularly the substitution method.
1) My problem is how to actually incorporate the formula into the program.
I've looked at similar examples but i don't know how to incorporate that equation.
2) I also don't understand how to have void substitution and void newton without getting an error message. Could i just add both statements to the program?
Thanks. I figured out how to run both the void substitution and void newton separately but it doesn't seem possible to combine the 2.
Here's what I did:
void substitution(double a,double b,
double x0, double& root, int& iteration)
{
double x, xcalc; // Local variables
iteration=0; // Initializes iterations to zero
x=x0;
do
{
++iteration;
xcalc=fxcalc(a,b,x);
cout<<"xcalc = "<<xcalc<<endl;
x=xcalc;
}
// Check if estimate for the root is within the tolerance
while ((fabs(fx(a,b,xcalc)) >= tol )&& (iteration < max_iter));
root=xcalc;
}
// Definition of function "fx" -- calculates a*x^3 + b*sin(x) + c
double fx(double a, double b, double x)
{
return a*pow(x,3)+b*log(x)-2;
}
// Definition of function "fxcalc" -- calculates pow((-b*x*x-c*x-d0/a,1./3.)
double fxcalc(double a, double b, double x)
{
return pow((-b*log(x))/a,1./3.);
}
// finds the root to f(x) = x^3+ln(x)-2
using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
// Declaration of functions used
void newton(double ,double, double, double&, int& );
double fx(double, double, double);
double fprimex(double, double, double);
const double tol=0.0001; // Tolerance for convergence
const int max_iter=50; // Maximum iterations allowed
// Main program
int main()
{
int iteration; // Number of iterations
double a, b; // Constants in f(x)
double x0; // Initial guess of root
double root; // Root determined by Newton Method
cout<<"Enter a and b"<<endl<<"separated by a space ";
cin>>a>>b;
cout<<"\nEnter an initial value for x: ";
cin>>x0;
newton(a, b, x0, root, iteration);// Call "newton"
// Output
cout<<"\nThe root is = "<<root<<endl;
cout<<"The number of iterations was = "<<iteration<<endl;
cout<<"The value of f(x) at the root = "<<fx(a,b,root)<<endl<<endl;
system("pause");
return 0;
}
// Definition of function "newton"
// Receives a, b and x0 values from main program
// Returns root and the iterations required
void newton(double a,double b,
double xold, double& root, int& iteration)
{
double xnew; // Local variables
iteration=0; // Initialize iterations to zero
do
{
++iteration;
xnew = xold - fx(a,b,xold)/fprimex(a,b,xold);
cout<<"xnew = "<<xnew<<endl;
xold = xnew;
}
while ((fabs(fx(a,b,xnew)) >= tol )&& (iteration < max_iter));
root=xnew;
}
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
double fx(double a, double b, double x)
{
return a*pow(x,3)+log(x)-2;
}
// Defines "fprimex" -- calculates the derivative of f(x)
double fprimex(double a, double b, double x)
{
return 3*a*pow(x,2)+1/x;
}