C++ programming, Newtons Method.

In summary, the function you are to implement finds a root of the given function eff(x) using Newton's method. The given version of eff(x) implements f(x) = x2e-x-2 (where e is the base of the natual logarithm) and effPrime(x) implements its derrivative, but your code could be used to find a root of other functions by substituting different implementations of eff and effPrime. You should use both of the above techniques to stop iteration (i.e., it should stop when either condition is satisfied).
  • #1
Jtenbroek
2
0
Having a lot of trouble the my C++ course, loops especially. This is the assignment giving me trouble

The function you are to implement finds a root of the given function eff(x) using Newton's method. The given version of eff(x) implements f(x) = x2e-x-2 (where e is the base of the natual logarithm) and effPrime(x) implements its derrivative, but your code could be used to find a root of other functions by substituting different implementations of eff and effPrime. Your program should use both of the above techniques to stop iteration (i.e., it should stop when either condition is satisfied).

The function you are to implement is as follows:

double Newton(double x, double tol, int maxIt)
Finds a root of eff using Newton's method starting at x and stoping when successive approximations are within tol of each other or maxIt iterations have occured.



These files were given;
double
eff(double x)
{
return x*x * exp(-x) - 2;
}

double
effPrime(double x)
{
return 2.0 * x * exp(-x) - x * x * exp(-x);
}





This is all I have so far, not even sure how to begin

#include "assign4.h"
#include <cmath>

double Newton(double x, double tol, int maxIt); {
return x_1 = x_o - (f(x_o) / f(x_o));

}
 
Physics news on Phys.org
  • #2
You did not use the return statement properly, instead you would write

Code:
return x_o - (f(x_o) / f(x_o));

In other words, you shouldn't be returning x1 = something
just return something

EXCEPT:
I do not see the variable "x_o" defined.

Even if it was defined, this is not the correct equation for Newton's method.
 
  • #3
First step, can you describe Newton's method of successive approximation?

You can't write a program if you don't know the algorithm.
 
  • #4
MisterX said:
You did not use the return statement properly, instead you would write

Code:
return x_o - (f(x_o) / f(x_o));

In other words, you shouldn't be returning x1 = something
just return something
Actually, you can do it this way. In C and C++, the value of an assignment expression is the value that is stored in the variable on the left side of the assignment. I wouldn't do it this way, but it's not incorrect.


MisterX said:
EXCEPT:
I do not see the variable "x_o" defined.

Even if it was defined, this is not the correct equation for Newton's method.
 
  • #5
Mark44 said:
Actually, you can do it this way. In C and C++, the value of an assignment expression is the value that is stored in the variable on the left side of the assignment. I wouldn't do it this way, but it's not incorrect.

Yes, but the OP shouldn't do it this way. The only way it would make any sense would be if one needed to set a (non-local) variable to the return value of a function every time the function was called.
 
  • #6
MisterX said:
Yes, but the OP shouldn't do it this way. The only way it would make any sense would be if one needed to set a (non-local) variable to the return value of a function every time the function was called.
I agree.
 
  • #7
I think this is what they suggested I use in the assignment "y = x - f(x)/f'(x),"
 
  • #8
Jtenbroek said:
I think this is what they suggested I use in the assignment "y = x - f(x)/f'(x),"

This won't work. The compiler will complain about the symbol ' after f. Your header file defines two functions. Use them.
 
  • #9
Try solving the problem with pencil and paper and maybe you'll see how to program it.
Do one step at a time like a computer on a separate line.
 

1. What is C++ programming?

C++ programming is a high-level, general-purpose programming language commonly used for developing a wide range of applications, including operating systems, games, and scientific simulations.

2. What is Newton's Method?

Newton's Method is an iterative algorithm used to find the roots of a differentiable function. It is commonly used in numerical analysis and optimization problems.

3. How is C++ programming used in Newton's Method?

C++ programming is used to write the code for implementing Newton's Method. This involves defining the function, calculating the derivative, and setting up the iterative loop to find the root of the function.

4. What are the advantages of using C++ programming in Newton's Method?

C++ programming allows for efficient and fast execution of the algorithm, making it suitable for solving complex problems. It also offers a wide range of data structures and libraries that can be used for implementing Newton's Method.

5. Are there any limitations to using C++ programming in Newton's Method?

One limitation of using C++ programming in Newton's Method is that it requires a strong understanding of the language and its concepts. Additionally, the code may be more complex compared to other languages, making it more challenging to debug and maintain.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top