I need help with Taylor Series
Part A:
Scan the angle in degrees x_deg. Express this angle in
radians by using x=PI*x_deg/180, and calculate Y=cos^2(x)
by using the math.h library of functions (pow() and cos()
functions). Compare the so calculated value of Y=cos^2(x)
with the approximate value y obtained by using n_term
terms of the Taylor series
cos^2(x)=0.5*(1 +Sum[(-1)^n (2*x)^(2n)/(2n)!]),
where n goes from 0 to n_term. Print the relative error
100*(Y-y)/Y.
Scan an integer value of n_term. Evaluate (2n)! by an embedded
for-loop statement. Use two do/while statements to continue
the calculations for different n_term and different x_deg. For
example, use flag=1 to continue calculations for different n_term
within the inner do/while loop, and flag=0 to exit that loop.
Use Flag=1 to continue calculations for different x_deg
within the outer do/while loop, and Flag=0 to exit that loop
and go to Part B. (Recall that 0!=1).
This is my program:
#include <stdio.h>
#include <math.h>
#define PI 3.141592654
main()
{
int n_terms, n=0;
double angle_deg, angle_rad, csa, csa2, taylor, sum;
printf("\n\nPar t A:\nCalculation of True and Approximate Values of cos^2(x)\n\n");
printf("Enter x_deg: \n");
scanf("%lf", &angle_deg);
angle_rad = angle_deg * (PI/180.);
csa = cos(angle_rad);
csa2 = pow(csa, 2.);
printf("True value of cos^2(x) = %f\n\n", csa2);
printf("n_term approximation of cos^2(x)\n\n");
printf("Enter number of terms:\n");
scanf("%d", &n_terms);
printf("\n%d term approximation\n ", n_terms);
}
so far everything shows up correctly. I'm just stuck on how to use the for loop to calculate the taylor series.
Part A:
Scan the angle in degrees x_deg. Express this angle in
radians by using x=PI*x_deg/180, and calculate Y=cos^2(x)
by using the math.h library of functions (pow() and cos()
functions). Compare the so calculated value of Y=cos^2(x)
with the approximate value y obtained by using n_term
terms of the Taylor series
cos^2(x)=0.5*(1 +Sum[(-1)^n (2*x)^(2n)/(2n)!]),
where n goes from 0 to n_term. Print the relative error
100*(Y-y)/Y.
Scan an integer value of n_term. Evaluate (2n)! by an embedded
for-loop statement. Use two do/while statements to continue
the calculations for different n_term and different x_deg. For
example, use flag=1 to continue calculations for different n_term
within the inner do/while loop, and flag=0 to exit that loop.
Use Flag=1 to continue calculations for different x_deg
within the outer do/while loop, and Flag=0 to exit that loop
and go to Part B. (Recall that 0!=1).
This is my program:
#include <stdio.h>
#include <math.h>
#define PI 3.141592654
main()
{
int n_terms, n=0;
double angle_deg, angle_rad, csa, csa2, taylor, sum;
printf("\n\nPar t A:\nCalculation of True and Approximate Values of cos^2(x)\n\n");
printf("Enter x_deg: \n");
scanf("%lf", &angle_deg);
angle_rad = angle_deg * (PI/180.);
csa = cos(angle_rad);
csa2 = pow(csa, 2.);
printf("True value of cos^2(x) = %f\n\n", csa2);
printf("n_term approximation of cos^2(x)\n\n");
printf("Enter number of terms:\n");
scanf("%d", &n_terms);
printf("\n%d term approximation\n ", n_terms);
}
so far everything shows up correctly. I'm just stuck on how to use the for loop to calculate the taylor series.
Comment