Here's what I have:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pi = 0;
long i;
long n;
cout << "Enter the value of n: ";
cin >> n;
cout << endl;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
pi = pi + (1 / (2 * i + 1));
else
pi = pi - (1 / (2 * i + 1));
pi = 4 * pi;
}
cout << endl << "pi = " << pi << endl;
return 0;
}
However, this does not give the desired output. For example, n = 9, pi = 262144. I know it's possible to just store pi as a constant and get the result that way, but I'm wondering what I am doing wrong using the algorithm above.
Because of long i;, (1 / (2 * i + 1)) will be zero for any i>0.
When i=0, you got pi=1;
for i=1,9, you got pi = 4*pi;
That's why you got 262144 which is 4^8.
You might want
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
pi = pi + (1.0 / (2 * i + 1));
else
pi = pi - (1.0 / (2 * i + 1));
}
pi = 4 * pi;
Related
First of all, I'm new to coding and C++. I did my researches for my problem on the internet but the solutions are not quite worked out for me.
I'm trying to get approximation of Pi with Liebniz formula which is:
Pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9...
The code is compilable and runnable. The problem is I get 4 as answer for every number of iterations (n value). Here is the main part of the code.
int main() {
int i ;
double a=1 ;
double pi4 = 0 ;
long n;
cout << "Number of iterations? ";
cin >> n;
for (i=1; i <= (n) ; i += 2) {
pi4 = pi4 + a * (1 / i);
a = -a;
}
cout.precision(20);
cout << "Pi = " << (pi4 * 4) << endl;
return 0;
Integer math. 1 / i will be 0 for all but the first iteration.
You can remove the reciprocal and just use a / i.
pi4 += a / i;
In c++ dividing by an int is this in other languages: floor(a / int).
Convert "i" to a double, or "tell" the compiler, this is a floating-point division. (Because this is an integer dividing, or Euclidean division [https://en.wikipedia.org/wiki/Euclidean_division]
[https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division])
#include <iostream>
using namespace std;
int main() {
int i ;
double a=1 ;
double pi4 = 0 ;
long n;
cout << "Number of iterations? ";
cin >> n;
for (i=1; i <= (n) ; i += 2) {
pi4 = pi4 + a / i;
// pi4 = pi4 + a * (1.0 / i);
a = -a;
}
cout.precision(20);
cout << "Pi = " << (pi4 * 4) << endl;
return 0;
}
My task is to find the root of a function with both the Newton Raphson and the bisection method within an error margin of 10E-7.
The point of all that is, that we learn that the Newton Raphson method is faster and more effective.
Now for some reason I come to the opposite result. Although I am aware that the initial guess of the root in both methods strongly affects the number of necessary iterations. But I entered a similar guess in both algorithms and my fellow students dont get the result I do.
Bisection method:
#include <iostream>
#include <iomanip>
using namespace std;
//Declaring the given function
double func1(double x) {
return 0.00000000027 * (x - 10000000) - 0.16460351745 * (-1 + ((1000000000) / (x))) * 1 / (sqrt(x));
}
int main() {
std::fixed;
//Initial guess: root ist at 10 to the 7.
double x1 = 10000000;
double x2 = 1000000000;
double eps = 0.0000001;
int i = 0;
double x0[100000];
x0[0] =0;
//Exception handler
if (func1(x1) * func1(x2) > 0) {
cout << "Root is not inside the bracket.";
goto end;
}
goto start;
//Bisection Algorithm
while (abs(x0[i] - x0[i-1]) >= eps) {
start:
i = i + 1;
x0[i] = 0.5 * (x1 + x2);
if (func1(x1) * func1(x0[i]) < 0) {
x2 = x0[i];
}
else {
x1 = x0[i];
}
}
cout << endl << "Bisection Method: " << fixed << setprecision(10) << x0[i] << endl << "Iterations: " << i << endl << endl << endl << endl << endl;
end:
return 0;
}
}
Newton Raphson:
#include <iostream>
#include <iomanip>
using namespace std;
// Declaring the function and its derivative
double func1(double x) {
return 0.00000000027 * (x - 10000000) - 0.16460351745 * (-1 + ((1000000000) / (x))) * 1 / (sqrt(x));
}
double funcderiv1(double x) {
return 0.00000000027+((0.1646035174)/(2*x*x*sqrt(x)))*(30000000-x);
}
int main()
{
std::fixed;
double eps = 1;
double x_start = 10000000;
double c;
int i = 0;
while (eps >= 0.0000001) {
c = x_start - ((func1(x_start)) / (funcderiv1(x_start)));
eps = abs(func1(x_start) / funcderiv1(x_start));
x_start = c;
i = i + 1;
}
cout << fixed << setprecision(5) << "RESULT " << c << endl << " Iterations: " << i << endl;
}
The root is at 17903534.23630
Does anyone know why my bisection method needs 55 iterations while Newton Raphson takes like 82?
For the function
f(x) = A * (x - B) - C * (D / x - 1) / sqrt(x)
A = 0.00000000027
B = 10000000
C = 0.16460351745
D = 1000000000
the correct derivative is:
f'(x) = A - C (x - 3D) / (2 * x * x * sqrt(x))
Compare this with your expression:
g(x) = A - C (x - 3B) / (2 * x * x * sqrt(x))
After fixing the formula (by adding two zeros), your code makes 6 iterations:
RESULT 17903534.23630
Iterations: 6
This question already has an answer here:
Dividing two integers to produce a float result [duplicate]
(1 answer)
Closed 2 years ago.
// PI = 4 - (4/3) + (4/5) - (4/7) ... for 100 first statements
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
double PI = 0.0;
int a = 1;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
PI += double(4 / a);
}
else {
PI -= double(4 / a);
}
a += 2;
}
cout << "PI Number is : " << PI;
cout << endl;
return 0;
}
I tried this code in visual studio 2015 to give me the answer of PI number value but it returns "PI Number is : 3" and I want it to return a float or a double number.
What should I do?
In double(4 / a), the 4 / a part evaluates to an integer and it is already truncated by the time you cast it to double. What you want to do is 4.0 / a instead, and no need for an explicit cast.
4 / a is an integer division and your conversion double(…) happens after that division, so the result will never have something after the decimal point. e.g. 4/5 results in 0.
You need to change 4 from an integer to a double 4.
The issue in your code is when it's computing the value:
double PI = 0.0;
int a = 1;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
PI += double(4 / a);
}
else {
PI -= double(4 / a);
}
a += 2;
}
You shouldn't do double(4 / a) but rather (double)4 / a or 4.0 / a
double PI = 0.0;
int a = 1;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
PI += (double)4 / a;
}
else {
PI -= (double)4 / a;
}
a += 2;
}
I'm having trouble compiling this program with #include. I see that if I comment out this line it compiles.
MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
I am unable to change the header since I need to run this code in ROS and I have to use the Eigen library built within. I am using the code as described in this link
How to fit a bounding ellipse around a set of 2D points.
Any help is greatly appricated.
pound include iostream
pound include Eigen/Dense
using namespace std;
using Eigen::MatrixXd;
int main ( )
{
//The tolerance for error in fitting the ellipse
double tolerance = 0.2;
int n = 12; // number of points
int d = 2; // dimension
MatrixXd p(d,n); //Fill matrix with random points
p(0,0) = -2.644722;
p(0,1) = -2.644961;
p(0,2) = -2.647504;
p(0,3) = -2.652942;
p(0,4) = -2.652745;
p(0,5) = -2.649508;
p(0,6) = -2.651345;
p(0,7) = -2.654530;
p(0,8) = -2.651370;
p(0,9) = -2.653966;
p(0,10) = -2.661322;
p(0,11) = -2.648208;
p(1,0) = 4.764553;
p(1,1) = 4.718605;
p(1,2) = 4.676985;
p(1,3) = 4.640509;
p(1,4) = 4.595640;
p(1,5) = 4.546657;
p(1,6) = 4.506177;
p(1,7) = 4.468277;
p(1,8) = 4.421263;
p(1,9) = 4.383508;
p(1,10) = 4.353276;
p(1,11) = 4.293307;
cout << p << endl;
MatrixXd q = p;
q.conservativeResize(p.rows() + 1, p.cols());
for(size_t i = 0; i < q.cols(); i++)
{
q(q.rows() - 1, i) = 1;
}
int count = 1;
double err = 1;
const double init_u = 1.0 / (double) n;
MatrixXd u = MatrixXd::Constant(n, 1, init_u);
while(err > tolerance)
{
MatrixXd Q_tr = q.transpose();
cout << "1 " << endl;
MatrixXd X = q * u.asDiagonal() * Q_tr;
cout << "1a " << endl;
MatrixXd M = (Q_tr * X.inverse() * q).diagonal();
cout << "1b " << endl;
int j_x, j_y;
double maximum = M.maxCoeff(&j_x, &j_y);
double step_size = (maximum - d - 1) / ((d + 1) * (maximum + 1));
MatrixXd new_u = (1 - step_size) * u;
new_u(j_x, 0) += step_size;
cout << "2 " << endl;
//Find err
MatrixXd u_diff = new_u - u;
for(size_t i = 0; i < u_diff.rows(); i++)
{
for(size_t j = 0; j < u_diff.cols(); j++)
u_diff(i, j) *= u_diff(i, j); // Square each element of the matrix
}
err = sqrt(u_diff.sum());
count++;
u = new_u;
}
cout << "3 " << endl;
MatrixXd U = u.asDiagonal();
MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
MatrixXd c = p * u;
cout << A << endl;
cout << c << endl;
return 0;
}
If I replace the obvious pound include bogus by
#include <iostream>
#include <Eigen/Dense>
it compiles just fine. It also runs, prints some numbers and returns 0.
I am trying to calculate Pi using this formula:
http://functions.wolfram.com/Constants/Pi/06/01/01/0005/
And this is the code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double n;
cin >> n;
long double first_part = 0.0, second_part = 0.0, pi = 0.0;
for(int i = 0; i <= n; i++)
{
first_part += (pow(-1, n)) / ((2 * n + 1) * pow(5, 2 * n + 1));
second_part += (pow(-1, n)) / ((2 * n + 1) * pow(239, 2 * n + 1));
}
pi = (first_part * 16) - (second_part * 4);
cout << pi << endl;
return 0;
}
But something goes wrong. For example, for n = 300 it outputs 6.65027e-420.
I really cannot find my mistake.
Please help me.
Thank you very much.
You're using the wrong variable:
for(int i = 0; i <= n; i++)
^^^^^
iterating over 'i'
But:
first_part += (pow(-1, n)) / ((2 * n + 1) * pow(5, 2 * n + 1));
second_part += (pow(-1, n)) / ((2 * n + 1) * pow(239, 2 * n + 1));
^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^
all operations use 'n'
You reached the limit of floating point accuracy:
#include <cmath>
#include <iostream>
int main()
{
// This will print inf (infinite)
std::cout << std::pow(5.0, 600.0) << "\n"; // pow(5, 2 * n + 1))
return 0;
}
Change your code replacing all n to i in the for loop:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long double n;
cin >> n;
long double first_part = 0.0, second_part = 0.0, pi = 0.0;
for(int i = 0; i <= n; i++)
{
first_part += (pow(-1, i)) / ((2 * i + 1) * pow(5, 2 * i + 1));
second_part += (pow(-1, i)) / ((2 * i + 1) * pow(239, 2 * i + 1));
}
pi = (first_part * 16) - (second_part * 4);
cout << pi << endl;
return 0;
}
I ran the above code and found outout:
3.14159
Careful: pow(239, 2 * n + 1)) can overflow second_part! Because double has the range:
1.7E +/- 308 (15 digits)
Reference here.