Pi Approximation using Leibniz formula - c++

I can't seem to find what is wrong with my code.
It should not give me any negative answers as long as I type in any integer higher than 0.
#include <iostream>
#include <math.h>
using namespace std;
int k;
double getPi(int = k) {
double num1, num2=0;
const int p = 4;
for(int i = 1; k >= i; i ++) {
num1 = pow(-1, k + 1)/(2 * k - 1);
num2 = num2 + num1;
}
return num2 * p;
}
int main() {
cout << "Iterations: ";
cin >> k;
cout << "Pi is approximated to be " << getPi(k);
}

Use i instead of k in below line
num1 = pow(-1, k + 1)/(2 * k - 1);

Related

In Cpp for is not working properly from the function

I need to do this exercise, with struct and include the function in the struct to do the calculations, but for some reason, it's not working.
I recommended you to check the image for a better idea.
#include <iostream>
using namespace std;
struct Alfa{
double h,x,n,a;
void shuma(){
cout << "enter n: "; cin >> n;
cout << "enter a: "; cin >> a;
for (int i = 1; i >= n; i++){
x = 2 * i + a;
}
};
};
int main() {
Alfa alf;
alf.shuma();
alf.h = (alf.x / 2) + 3;
cout << alf.h;
return 0;
}
You are not following the formula in the image. Use this instead:
double sum = 0;
for (int i = 1; i <= n + 1; i++) {
if (i != 4) {
sum += 2 * i + a;
}
}
h = x / 2 + 3 * sum;
for (int i = 1; i >= n; i++) means i initiated to 1, while i>=n do the loop, then inc i. So when n is bigger then 1, it will never enter the loop. Maybe you want for (int i = 1; i <= n+1; i++) ?

Which algorithm should I use to calculate the given math sequence?

The user must enter a number n (1; infinity). Then the program does this:
cos1/sin1 * (cos1+cos2)/(sin1+sin2) * … * (cos1+cos2+...+cos⁡ n)/(sin1+sin2+...+sin ⁡n )
I tried to calculate that:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
double res;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++)
{
res = cos(i) / sin(i);
}
cout << res;
}
But I don't know which algorithm would do it correctly.
Since you need to divide the two sums in each step, you need to store those sums, and multiply an accumulating product with the result of dividing them.
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
double cos_sum = 0.0;
double sin_sum = 0.0;
double res = 1.0;
for (int i = 1; i <= n; i++)
{
cos_sum += cos(i);
sin_sum += sin(i);
res *= cos_sum / sin_sum;
}
cout << res;
}
There are basically three quantities that accumulate during the iteration. They are:
C[N] = cos1+cos2+cos3+cos3+ .... cosN
S[N] = sin1+sin2+sin3+sin3+ .... sinN
X[N] = C[1] / S[1] * C[2] / S[2] * ... C[N] / S[N]
The recursive relations are:
C[0] = 0
C[N + 1] = C[N] + cosN+1
S[0] = 0
S[N + 1] = S[N] + sinN+1
X[0] = 1
X[N + 1] = X[N] * C[N+1] / S[N+1]
Using this it should be straightforward to write the loop:
#include <iostream>
int main() {
int n;
double X = 1;
double C = 0;
double S = 0;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++) {
C += cos(i);
S += sin(i);
X *= C / S;
}
cout << X;
}

how to Calculate this series

#include <iostream>
#include <math.h>
using namespace std;
int fact(int number)
{
unsigned long long int p = 1;
if (number == 0) {
return p;
}
for (int i = 1; number >= i; i++) {
p = p * i;
}
return p;
}
int main()
{
long long int a, x, sum = 0, result;
int n ;
cin >> a;
cin >> x;
cin >> n;
for (int k = 0; n >= k; k++) {
result = fact(n) / (fact(k) * fact(n - k));
sum = sum + (result * pow(x, k) * pow(a, n - k));
}
cout << sum;
return 0;
}
I want to calculate this series
So I considered the long long int sum, but the sum number sometimes gets too big. What can I do to save the sum number without using library?
First of all I would suggest to use binomial theorem -- what you are computing is just pow(x+a, n)
If you want to do this through series, do not compute the binomial coefficient using factorials but something like this
int bin_coeff(int n, int k){
int lim = k > n/2 ? k : n - k;
int sum = 1;
for (int i = n; i > lim; i--){
sum *= i;
}
for (int i = 2; i < (n - lim + 1); i++){
sum /= i;
}
return sum;
}

Newton Interpolation in C++

I had to write Newton Interpolation in C++, but I have some problems...
In addition I had to use function which returns array and I couldn't use two-dimensional arrays.
It's my code:
#include <iostream>
using namespace std;
void interpol(double *, double *, int, int);
double *countDivide(double *y, double *x, int n);
int main()
{
int n;
cout << "Give ammount of nodes: " << endl;
cin >> n;
double arg;
double *x = new double[n];
double *y = new double[n];
for(int i = 0; i < n; i++)
{
cout << "x[" << i << "] = ";
cin >> x[i];
cout << "y[" << i << "] = ";
cin >> y[i];
}
cout << "Give arg: ";
cin >> arg;
double* b = countDivide(y, x, n);
interpol(b, x, n, arg);
return 0;
}
double *countDivide(double *y, double *x, int n)
{
int i, j;
double z;
for (i = 0; i < n; j++)
{
z = y[0];
for (j = 0 ; j > n - i; j++)
{
y[j] = (y[j] - y[j - 1]) / (x[i + j] - x[j]);
}
y[n - i] = z;
}
return y;
}
void interpol(double *p, double *x, int n, int arg)
{
double w;
double sum = 0;
int j, i;
for (i = n - 1; i >= 0; i--)
{
w = 1;
for (j = 0; j < i; j++)
w*= (arg - x[j]);
w *= p[j];
sum += w;
}
cout << sum << endl;
}
But program is stopping after
cin >> arg
What's wrong? I don't know what is wrong with that code, because I spent much time on it...
Thanks for your help.
This loop doesnt look right
for (i = 0; i < n; j++)

Calculating eigenvalues of an infinite banded matrix using LAPACK

I'm new to C++ and am trying to figure out how to use LAPACK to find the eigenvalues of an infinite banded matrix (anharmonic oscillator problem). I know that I'm calculating the matrix correctly as I've checked the values and they all match up. However, I'm not sure if I'm passing the values to the subroutine correctly or if I've got something mixed up as the eigenvalues that are being returned are not what I am expecting. I'm using the dsbtrd subroutine to compute this. Here's the manual for that: http://www.netlib.org/lapack/explore-html/d0/d62/dsbtrd_8f.html
Any ideas on where I might be going wrong?
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
//SUBROUTINE DSBTRD( VECT, UPLO, N, KD, AB, LDAB, D, E, Q, LDQ, WORK, INFO )
extern "C" {
void dsbtrd_(const char *vect, const char *uplo, int *n, int *kd, double *ab, int *ldab, double d[], double e[], double *q, int *ldq, double work[], int *info);
}
#define MAX 14
int main(){
// Values needed for dsbtrd
const char *vect = "V";
const char *uplo = "U";
int n;
int ldab = MAX;
int ldq = MAX;
int info;
double ab[MAX][ldab];
double d[MAX];
double e[MAX];
double q[MAX][ldq];
double work[MAX];
//other values needed
int i,j,delta;
double eps;
double a[MAX][MAX];
double g[MAX][MAX];
//Read in value of eps and n
cout <<"Enter epsilon: \n";
cin >> eps;
cout << "Epsilon = " << eps << "\n";
cout <<"Enter n: \n";
cin >> n;
cout << "n = " << n << "\n";
if(n >= MAX){
cerr << "n is great than max \n";
}
//Build matrix g
for(j = 0; j < n; j++){
for(i = 0; i < n; i++){
int m = min(i,j);
if(i == j){
g[i][j] = 1.5*(pow(m,2) + m +.5);
}else if( i == j + 2 || i == j - 2){
g[i][j] = (m + 1.5)*sqrt((m+1)*(m+2));
}else if(i == j + 4 || i == j -4){
g[i][j] = .25*sqrt((m+1)*(m+2)*(m+3)*(m+4));
}else{
g[i][j] = 0;
}
}
}
//Build the starting matrix a
//row i, column j
for(j = 0; j < n; j++){
for(i = 0; i < n; i++){
if(i == j){
delta = 1;
}else{
delta = 0;
}
a[i][j] = (i + .5)*delta + eps*g[i][j];
}
}
//Build the matrix ab
// ab(kd+1+i-j,j) = a(i,j) for max(1,j-kd)<=i<=j
int kd = n - 1;
for(j = 1; j <= n; j++){
for(i = max(1,j-kd); i <= j; i++){
ab[j-1][kd+i-j] = a[j-1][i-1];
}
}
//Solve for eigenvalues
dsbtrd_(vect, uplo, &n, &kd, &ab[0][0], &ldab, d, e, &q[0][0], &ldq, work, &info);
//Check for success
if(info == 0)
{
//Write answer
for(i = 0; i < n; i++){
cout << "Eigenvalue " << i << ": " << d[i] << "\n";
}
}
else
{
//Write error
cerr << "dsbtrd returned error " << info << "\n";
}
return info;
}
DSBTRD doesn't calculate eigenvalues. It reduces the matrix to tridiagonal form; you're pulling out the main diagonal of the resulting tridiagonal matrix and pretending that those are the eigenvalues, but they aren't.
You need to call DSTERF (or one of a few other routines) on the resulting tridiagonal form to get the eigenvalues.
For more details, consult the LAPACK User's Guide.