Find and display the values of both sides of the following mathematical series expression and
an absolute difference of both sides. User can input either of angles in degree 90, 60, 30 etc.
sin(x) = x −
x
3
3!
+
x
5
5!
−
x
7
7!
+
x
9
9!
Once the user enters the angle in degrees. Your program should do the following (Write separate
functions for a, b, c, and d)
a) LHS Result
b) RHS Result
c) Difference
d) First term, series of two terms, series of three terms, series of four terms, series of five terms.
#include <iostream>
#include <math.h>
#include <iomanip>
#define PI 3.14159;
using namespace std;
int factorial(int x){
int res = 1;
int i = 1;
while(i <= x){
res *= i;
i++;
}
return res;
}
float LHS(float angle){
return sin(angle);
}
float RHS(float angle){
return angle - pow(angle, 3)/factorial(3) + pow(angle, 5) / factorial(5) - pow(angle, 7) / factorial(7) + pow(angle, 9)/factorial(9);
}
float Difference(float angle){
return abs(LHS(angle) - RHS(angle));
}
void Nterms(float angle){
cout<<"First Term: "<<angle<<endl;
cout<<"Series of two terms: "<<fixed<<setprecision(5)<<angle - pow(angle, 3)/factorial(3)<<endl;
cout<<"Series of three terms: "<<fixed<<setprecision(5)<<angle - pow(angle, 3)/factorial(3) + pow(angle, 5) / factorial(5)<<endl;
cout<<"Series of four terms: "<<fixed<<setprecision(5)<<angle - pow(angle, 3)/factorial(3) + pow(angle, 5) / factorial(5) - pow(angle, 7) / factorial(7)<<endl;
cout<<"Series of five terms: "<<fixed<<setprecision(5)<<RHS(angle)<<endl;
}
float degToRad(float angle){
return angle / 360 * 2 * PI;
}
int main(){
float deg;
cout<<"Input angle in degrees: ";
cin>>deg;
float rad = degToRad(deg);
cout<<"LHS Result: "<<fixed<<setprecision(5)<<LHS(rad)<<endl;
cout<<"RHS Result: "<<fixed<<setprecision(5)<<RHS(rad)<<endl;
cout<<"Difference: "<<fixed<<setprecision(5)<<Difference(rad)<<endl;
Nterms(rad);
return 0;
}
Comments
Leave a comment