Newton Method

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:

#include <iostream>
#include <cmath>

using namespace std;

double Dx;//change function

double f(double x)//function
{
return x - sin(x) - 4;
}

double fd(double x)// 1 derivative
{
return (f(x + Dx) - f(x)) / Dx;
//return 1 - cos(x);
}

double f2d(double x)// 2 derivative
{
return (fd(x + Dx) - fd(x)) / Dx;
//return sin(x);
}

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>

using namespace std;

double Dx;//change function

double f(double x)//function
{
return x - sin(x) - 4;
}

double fd(double x)// 1 derivative
{
return (f(x + Dx) - f(x)) / Dx;
//return 1 - cos(x);
}

double f2d(double x)// 2 derivative
{
return (fd(x + Dx) - fd(x)) / Dx;
//return sin(x);
}

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;
}
¿what in the wording is making you confused?

1
2
3
4
5
double fd(double x)// 1 derivative
{
   //return (f(x + Dx) - f(x)) / Dx;
   return 1 - cos(x); //use the analytical solution
}


1
2
3
4
5
6
7
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 
get rid of that
replace it by
1
2
3
std::cout << "Initial approximation ";
double x;
std::cin>>x;


1
2
3
4
5
6
if (f(a)*f2d(a) > 0)//determining the initial value
   firstApprox = a;
else
   firstApprox = b;
double prev = firstApprox - E * 2;
double x = firstApprox;
get rid of that
Message for ne555:
Thank you so much for your help. I am a physics major, unfortunately computer science is not my thing. i do appreciate this help
Topic archived. No new replies allowed.