Actually I find it difficult solving mathematical problems using c++. I will be grateful if someone can assist debug and correct the mistakes. Here is what I have tried
First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!
Middle = n sigma i = 1 sqrt(i/x)
last = 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - ....X^n/n!
Final = first/last + middle
#include<iostream>
#include<cmath>
#include<math.h>
usingnamespace std;
int factorial(int n) // factorial function
{
if (n<=1)
return(1);
else
n=n*factorial(n-1); // notice we just keep calling this over until we get to 1!
return(n);
}
int first (int n) // function for first
{
while (n <= 1)
{
f = f + pow (x,n) / factorial (int n);
return (f);
n-=2;
}
}
int last (int n) // function for last
{
while (n <= 1)
{
l = l + 1 - pow (x,n) / factorial (int n);
return (l);
n-=2;
}
}
int sigma(int x, int n)
{
int i = 1, sum = 0;
int x, n;
while (i <= n)
{
sum += i * i;
i++;
sum = sqrt(sum/x)
return sum;
}
}
int main()
{
int x, n;
int first, middle, last, final;
char ans;
do {
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;
First = first (n);
Middle = sigma(x, n);
Last = last (n);
Final = Frist/(Last + Middle);
cout << “The value for first is << first << endl;
cout << “The value for middle is << middle << endl;
cout << “The value for last is << last << endl;
cout << “The value for final is << final << endl;
cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
getch ();
return 0;
}
Good news! Your factorial function appears to work nicely! Good use of recursion there!
I see some problems with the other parts though. Partial list:
1) Lines 69 through 72: You need a closing " after the word is. Also, the variables first, middle, last and final are never assigned values.
2) The terms being added in the functions first, last do not alternate sign as desired in the power series for the sine and cosine functions you are trying to evaluate there.
3) The variable x does not exist in the functions first and last - you need to pass it to them.
4) You are performing integer division on lines 21 and 32, which throws away the remainder. eg: 8/5 = 1 not 1.6 and 5/8 = 0 not 0.625. You probably want to be working with variables of type float or double in your math functions. With x an integer you can only evaluate sine(1) or sine(2), etc.
5) Use just n, not int n - in the calls to factorial on lines 21, 32.
6) The sqrt function in sigma doesn't work well with integer variables. Use float or double for x and sum.
7) The variables x and n are passed to sigma and are also declared as local variables. I'm not sure which would wind up getting used in the function.
8) A semi-colon is needed at the end of line 46.
9) The variables first, last in main are named the same as your functions. I would avoid this - even if the compiler would allow it. You capitalize these names on lines 64 through 67 but these variables don't exist. First is misspelled on line 67.
I hope this gets you started! Your problem is interesting and I want to see if I can sort it all out so I may post back with some modified code later. Best of luck!
Here is my program to evaluate just the power series for sine and cosine. Perhaps you can generalize from it to make your sigma function work. It checks the values obtained from the power series against the sin(x) and cos(x) library functions. I was surprised to find the values agree to 10 decimal places with only 12 terms summed in the series!!(ie. for n=12 as input). These power series converge pretty fast if x is kept in the principal range of -PI to +PI. I modified your factorial function to work with doubles instead of integers because the value of n! grows so fast that the maximum value of an integer is limiting here. Also, a double return type is needed anyways. Please let me know (via post here) if this was useful.
// Evaluates power series for sin(x) and cos(x) to n terms
// values entered are not checked so enter carefully x = -PI to +PI
// and n not too high. Keep eye on value of n! vs. DBL_MAX in output
// NOTE: n=12 gives about 10 digit accuracy (series converges rapidly)
// so no need for high n values
#include <iostream>// for use of cin, cout
#include <iomanip>// for use of setprecision
#include <cstdio>// for use of gets(), scanf()
#include <cmath>// for use of sin, cos, pow functions
#include <float.h>// for use of the constant DBL_MAX
usingnamespace std;
double factorial(double n)
{
if (n<=1.0)
return(1.0);
else
n=n*factorial(n-1.0); // notice we just keep calling this over until we get to 1!
return(n);
}
double SineN(constdouble& x, constint& n )// replaces first
{
double sum = 0.0;
if(n > 0)
for(int j=0; j<n; j++)
{ // successive terms are to alternate sign
if(j%2)// j is odd
sum -= pow(x,(double)(2*j+1))/factorial((double)(2*j+1));
else// j is even
sum += pow(x,(double)(2*j+1))/factorial((double)(2*j+1));
}
return sum;
}// end of SineN()
double CosineN(constdouble& x, constint& n )// replaces second
{
double sum = 1.0;
if(n > 0)
for(int j=1; j<n; j++)
{
if(j%2)// j is odd
sum -= pow(x,(double)(2*j))/factorial((double)(2*j));
else// j is even
sum += pow(x,(double)(2*j))/factorial((double)(2*j));
}
return sum;
}// end of CosineN()
int main()
{
int n = 0;// number of terms in series
double x = 0.0;// series parameter. Give value in radians
char repeat = 'y';
do// until user is done
{
cout << "Enter x (in radians): " ;
cin >> x;
cout << "Enter n: " ;
cin >> n;
cout << "n! = " << factorial((double)n) << endl;
cout << "DBL_MAX = " << DBL_MAX << endl;
cout << setprecision (10);// display result to 10 decimal places
cout << "SineN(x,n) = " << SineN(x,n) << endl;// evaluate using a power series
cout << "sin(x) = " << sin(x) << endl;// check against math library function
cout << "CosineN(x,n) = " << CosineN(x,n) << endl;
cout << "cos(x) = " << cos(x) << endl;
cout << "repeat (y/n)? " << flush;
cin >> repeat;
}while( repeat=='y');
return 0;
}
A more efficient method plus finding error bound.
These new functions find the values of sine(x) and cosine(x) to n terms via the recursion relation (ratio of successive terms) for the series. Each term is found in terms of the last term instead of each term being calculated individually. The maximum error is returned through a pointer so the functions can find a 2nd value for us.
// New methods to find sine(x) and cosine(x)
// these methods use the recursion relation for the series
// to generate the next term from the last.
// Advantages: 1) No need to find n! for each term.
// 2) No need to call pow(x,n) each term.
// 3) No need for math library functions for finding results
#include <iostream>
#include <iomanip>
#include <cmath>// only needed for checking results now
usingnamespace std;
double SineN2(constdouble& x, constint& n, double* p_err )
{
double a = 1.0*x;// 1st term
double sum = 0.0;
if(n > 0)
{
for(int j=1; j<=n; j++)// for sine
{
sum += a;
a *= -1.0*x*x/( (2*j+1)*(2*j) );// next term from last
// sum += a;
}
*p_err = a;// max. error = 1st term not taken for alternating series
}
else
cout << " n must be > 0";
return sum;
}// end of SineN2()
double CosineN2(constdouble& x, constint& n, double* p_err )
{
double a = 1.0;// 1st term
double sum = 0.0;
if(n > 0)
{
for(int j=0; j<n; j++)// for cosine
{
sum += a;
a *= -1.0*x*x/( (2*j+1)*(2*j+2) );// next term from last
// sum += a;
}
*p_err = a;// max. error = 1st term not taken for alternating series
}
else
cout << " n must be > 0";
return sum;
}// end of CosineN2()
int main()// for new methods
{
int n = 0;// number of terms in series
double x = 0.0;// series parameter. Give value in radians
double errorAmount = 0.0;// new functions find this value
char repeat = 'x';// take both values
do// until user is done
{
if( repeat == 'x' )
{
cout << "Enter x (in radians): " ;
cin >> x;
}
cout << "Enter n: " ;
cin >> n;
cout << setprecision (10);// display result to 10 decimal places
cout << "SineN2 = " << SineN2(x,n,&errorAmount) << endl;// using recursion relation
cout << "sin(x) = " << sin(x) << endl;// check against math library function
cout << "errorAmount = " << errorAmount << endl << endl;// display max. error
cout << "CosineN2 = " << CosineN2(x,n,&errorAmount) << endl;
cout << "cos(x) = " << cos(x) << endl;
cout << "errorAmount = " << errorAmount << endl << endl;
cout << "enter x for new x, n for new n only or any other to quit: " << flush;
cin >> repeat;
}while( repeat=='x' || repeat=='n');
return 0;
}
I wonder if OP will be seeing any of this?
I may return to cover more of the elementary transcendental function later.