473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Newton's method

1
Hello. This is my first C++ code (and my first post).

I've created a code, that calculates the root of function f(x) = sin(x). I use 3 as x_0. Here's the output:
Expand|Select|Wrap|Line Numbers
  1. Insert the number of iterations. 
  2. 5
  3. Starting iteration for 5 rounds. Using xOld = 3
  4. 3.1425465430742778316641761193750426173210144042969
  5. 3.1415926533004769893864249752368777990341186523438
  6. 3.141592653589793115997963468544185161590576171875
  7. 3.141592653589793115997963468544185161590576171875
  8. 3.141592653589793115997963468544185161590576171875
  9.  
So the digits don't change after the third iteration. The code goes like this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.         // double tol = 1.0E-300;
  9.         int nMax;
  10.         int n;
  11.         double xOld;
  12.         double xNew;
  13.  
  14.         cout << "Insert the number of iterations. \n";
  15.         cin >> nMax;
  16.         cout << "Starting iteration for " << nMax << " rounds. Using xOld = 3" << endl;
  17.         xOld = 3;
  18.         cout.precision(50);
  19.  
  20.         for( n = 1; n <= nMax ; n++ ) 
  21.         {
  22.                 xNew = xOld - tan(xOld);
  23.                 cout << xNew << endl;
  24.                         // if( fabs((xNew - xOld) / xOld ) < tol ) break;
  25.                 xOld = xNew;
  26.         }
  27.  
  28. }
  29.  
I've commented the tolerancy parts off.

If anyone knows what the problem is, do tell. Also improvements are welcome. Thanks in advance.
Nov 2 '07 #1
1 4250
Banfa
9,065 Expert Mod 8TB
You have not actually asked a question or stated a problem or you could have stated the behaviour you were expecting as well as what actually happened. If you were a customer of mine I would have told you to go away and come back when you had a proper description of the problem.

You have stated this behaviour of your code

So the digits don't change after the third iteration. The code goes like this:
Which I assume is what you are querying, you were expecting the digits to continue changing?

OK 2 points to bear in mind,

1. there is no point setting an output precision of 50 decimal places, a double is only accurate to 15 decimal places.

2. Since the result you are getting mimics PI to 15 decimal places I would guess that it has found the correct answer in 3 iterations (with-in the limits of the precision of a double).



Basically your code is working correctly, if you want more precision then on some platforms the type long double gives a few extra places of precision (but still not 50).
Nov 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: sptutx | last post by:
Write a C program that uses Newton's Method to solve an equation in one variable. Try solving x^x = ln2 the deriviative of x^x is x^x(lnx + 1). The 'ln' function in C is log(), and the...
6
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
1
by: Zaid Kishawi | last post by:
hi all, plz i wanaa program Newton Raphson method by using c++, but my problem is how to identify the function??
11
by: kartikegarg | last post by:
can you help me please with this problem.. i want a c program using newton raphson method for solving 18 equations... the equations are not of degree greater than 1... i need the program to input...
1
by: dynamo | last post by:
Hi guys,i was wondering if anyone knows the code to solve equations using the newton raphson method in matlab.Or at least the algorithm.
2
by: thiofdelux | last post by:
I need a c++ program that uses newton raphson method to find the rooys of a function. The program would prompt to put in the degree of the ploynomial, the error bound 10^(n), and the first...
1
by: lionkng | last post by:
I need a simple code for finding roots in c programme using Newton-raphson method
1
by: ripal ruparelia | last post by:
how to solve x-e^-x=0 by newton raphson method
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.