HELP WITH C++!! program to find roots using newtons method?
-
I have an assignment to write a code to find the root of f(x)=x^(2)e^(-x)-2 using newtons method, y = x - f(x)/f '(x). I have very little experience writing code, this is my first course in it and I'm finding it very confusing and frustrating, especially when what I have to write code for is not very straightforward. I have come up with some mess of code that I cannot seem to progress. I'm just not sure what the procedure to find the root would be. VERY FRUSTRATED!!! any help is GREATLY appreciated Here is the assignment: The procedure is to repeatedly compute a new estimate of the root, y, using a previous estimate of the root, x, and the following computation: y = x - f(x)/f'(x), where f'(x) is the derivative of f(x). If this is repeated often enough (each time replacing x with the new estimate, y), and the initial estimate of the root is close enough to the actual root, then the values of x and y will converge to each other and the root. Two methods are used to stop the iteration process: 1)when the absolute value of the difference between x and y is less than some specified tolerance, 2)when the number of iterations reaches a specified maximum number, maxIterations. In this assignment you will create a C++ program that implements the function described below. We have provided the following files: assign4.h This file contains the declarations for the function you are to implement (newton) as well as the functions to be used (eff and effPrime), which are defined in effExp.cpp. effExp.cpp This file contains the definitions of the functions to be used (eff and effPrime) by your newton function. 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. Sample Data x = -1.5, tol = 0.0001, maxIt = 10 should result in -0.901201. Here is the crap I've been able to come up with: #include "effExp.cpp" #include <iostream>; using namespace std; #include <cmath>; double newton(double x, double tol, int maxIt); int main () { double root root = newton(-1.5,0.0001,10); cout << "The root of f(x) = x2e-x-2 is estimated to be " << root; return 0; } double newton(double x, double tol, int maxIt) { double lastGuess; double deltaG; double result; do{ lastGuess = x; deltaG = lastGuess-x; result = x - eff/effPrime; x = (lastGuess + result)/2 while(|deltaG| > tol); return x; } } Most of this probably doesn't make sense or isn't correct. Any help is appreciated as I have very little experience
-
Answer:
Here are the problems with your code that jump out at me: 1. The using namespace std; line should go after the #include directives, not in the middle of them like it is now. 2. You should not be including effExp.cpp using #include. Instead, you should be including assign4.h, which should contain any function prototypes and constants that are used in effExp.cpp and your main source file. The header file needs to be included so that the compiler knows the functions and constants you use exist. The fleshed out code in effExp.cpp will be compiled and linked alongside your main code file and does not need to be included. Just the header, which is why they exist. 3. This line is wrong: while(|deltaG| > tol); To do absolute value, use this function from cmath: http://www.cplusplus.com/reference/clibrary/cmath/fabs/ There may be other mistakes I didn't catch. Good luck with your project!
Andrew at Yahoo! Answers Visit the source
Related Q & A:
- How To Find Email Using Facebook?Best solution by Yahoo! Answers
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Help with c++ grading program.Best solution by Yahoo! Answers
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.