Can you help me with what my teacher wants. First I am going to show you what she email me, then my coding. Please help me by over writing it. Thank you so much.
HER MESSAGE TO ME:
I tried to correct your HW7 for computational (Newton’s method) but it needs to be redone completely. I am not sure why you are calculating the derivatives using the definition (and commented out “return 1-cos(x)” which is correct). I am also not sure why you have an interval a,b and do certain things that needed to be done in the bisection method For Newton’s method you need just one starting value and then use the formula
x1=x0-f(x0)/g(x0) where g0 is the derivative, to get a value closer to the root
MY CODING FOR TWO PROGRAMS:
PROGRAM1
Write a program that calculates the root of a function x-sin(x)-4 using Newton method using a for loop:
int main()
{
cout << "Start of the interval: ";
double a/* = 0*/;
cin >> a;//beginning of interval
cout << "End of the interval: ";
double b/* = 50*/;
cin >> b;//end of interval
cout << "Enter a maximum number of iterations: ";
int K/* = 200*/;
cin >> K;//maximum number of iterations
cout << "Precision: ";
double E/* = 0.01*/;
cin >> E;//precision (E < 1)
Dx = E / 10;//change function
double firstApprox;
int n = 0;
if (f(a)*f2d(a) > 0)//determining the initial value
firstApprox = a;
else
firstApprox = b;
double prev = firstApprox - E * 2;
double x = firstApprox;
for (n = 0; n < K; n++)
{
prev = x;
double y = f(x);
x -= y / fd(x);// Newton iteration
if (abs(x - prev) < E)
{
cout << "Root of the equation: " << x << " , amount of iterations: " << n << "." << endl;
system("pause");
return 0;
}
}
cout << "Exceeded the maximum number of iterations!!!" << endl;
system("pause");
return 0;
}
PROGRAM2
Write a program that calculates the root of a function x-sin(x)-4 using Newton method using a while loop:
#include <iostream>
#include <cmath>
int main()
{
cout << "Start of the interval: ";
double a/* = 0*/;
cin >> a;//beginning of interval
cout << "End of the interval: ";
double b/* = 50*/;
cin >> b;//end of interval
cout << "Enter a maximum number of iterations: ";
int K/* = 200*/;
cin >> K;//maximum number of iterations
cout << "Precision: ";
double E/* = 0.01*/;
cin >> E;//precision (E < 1)
Dx = E / 10;//change function
double firstApprox;
int n = 0;
if (f(a)*f2d(a) > 0)//determining the initial value
firstApprox = a;
else
firstApprox = b;
double prev = firstApprox - E * 2;
double x = firstApprox;
while (abs(x - prev) > E)
{
prev = x;
double y = f(x);
x -= y / fd(x);// Newton iteration
n++;
if (n > K)
{
cout << "Exceeded the maximum number of iterations!!!" << endl;
system("pause");
return 0;
}
}
cout << "Root of the equation: " << x << " , amount of iterations: " << n << "." << endl;
system("pause");
return 0;
}
int main(){
cout << "Start of the interval: ";
double a/* = 0*/;
cin >> a;//beginning of interval
cout << "End of the interval: ";
double b/* = 50*/;
cin >> b;//end of interval