473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with sine taylor series

hey guys, the question asks to
Scan the angle in degrees using x=PI*x_deg/180 and calculate Y=sin^2(x).
Calculate sin^2(x) approximately by using n terms (n>0) the Taylor series
sin(x)=Sum[(-1)^k x^(2k+1)/(2k+1)!] (k goes from 0 to n-1)
Scan an integer value of n. Evaluate (2k+1)! by an embedded for-loo statement. Use two do/while statements to continue the calculations for different n and different x_deg.
Calculate the error between the Y value and the approximate value (y) obtained from the Taylor series with n terms. When calculating this error, use if/else statement: if x_deg is not equal to 0., 180, and 360 degrees, print the Relative Error in percents is 100*(y-Y)/Y; else print the Absolute Error is y-Y.

This is what i have so far.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define PI 4*atan(1.0)
  4. main()
  5. {
  6. double x_deg, x, Y, y1, y2, n_fact;
  7. int ask1, ask2, n_term, k, i;
  8. do
  9. {
  10. printf("\nEnter x_deg: ");
  11. scanf("%lf", &x_deg);
  12. x = x_deg * (PI/180.);
  13. Y = pow(sin(x), 2);
  14. printf("True value of sin^2(x) = %g\n\n", Y);
  15. printf("n_term approximation of sin^2(x)\n\n");
  16. do
  17. {
  18. printf("Enter number of terms: \n");
  19. scanf("%d", &n_term);
  20. printf("%d term approximation\n", n_term);
  21. y1 = 1.0;
  22. for(k=n_term;k>0;k--)
  23. {
  24. n_fact = 1.0;
  25. for(i=(2*k)+1;i>0;i--)
  26. {
  27. n_fact *= i;
  28. }
  29. y1 += (pow(-1,k)/(n_fact+1))*(pow(x,(2*k)+1));
  30. y2 = pow(y1, 2);
  31. }
  32. printf("sin^2(x) = %g\n", y2);
  33. printf("Relative Error = %g percent\n\n", 100*(y2-Y)/Y);
  34. printf("Do you want a new n_term: y=1 or n=0\n");
  35. scanf("%d", &ask2);
  36. }
  37. while(ask2!=0);
  38. printf("Do you want a new x_deg: y=1 or n=0\n");
  39. scanf("%d", &ask1);
  40. } while(ask1!=0);
  41. }
  42.  
>>>the problem is that i get the wrong taylor approximation and thus wrong percent error. I think the problem may be with my embedded for-loop/factorial method but i have tried for hours and cant seem to find the porblem.
Feb 10 '10 #1
1 9629
donbock
2,426 Expert 2GB
  1. The formula involves a sum over k, where k goes from 0 to n-1. In your code k goes from 1 to n.
  2. In the y1 calculation you divide by (n_fact+1). I don't believe the "+1" is correct.
  3. You compute relative error for all values of x_deg. The problem statement tells you to instead compute absolute error for three particular angles. Recognizing a particular value is difficult with floating point. Perhaps it would be better to enter x_deg as an integer.

By the way, here are some miscellaneous comments that probably aren't causing an error.
  • Use CODE tags when you post source code here.
  • I would not #define PI as a function call. Either #define it as a literal number (and type in lots of digits) or make it a static const variable within main with an initializer that calls atan().
  • Assign sin(x) to y, then y = y*y. This is faster and possibly more accurate than pow(sin(x),2).
  • Your k-loop counts down. It would look more like the original formula if you counted up.
  • Your loop for computing V! counts down from V to 1. You could have stopped at 2.
  • pow(-1,k) is overkill. -1 to an integral power is either +1 or -1, depending on if the exponent is even or odd.
  • y2=y1*y1 is better.
Feb 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

143
by: suri | last post by:
Hello I downloaded glibc and tried looking for the code that implements the sine function i couldnt find the file. i went to the math directory and found math.h.. i guess that needs to be...
0
by: Pallav | last post by:
i'm trying to build a lookup sine table to run some code in an embedded processor without floating point unit. the table is 256 values for 360 degrees. my code is below. the fixed point base is...
9
by: Water Cooler v2 | last post by:
What is the inverse of the sine function? I've been pulling my hair and I have no clue nor memory of how to go about solving this little expression: sine-1 (read sine inverse) of (1-(x/y)) An...
12
by: wuzertheloser | last post by:
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...
2
by: chb1980 | last post by:
this is a question..about Taylor series... 1) Find the Taylor series approximation of order 2 (n=2) for the funtion f(x,y)=sin(xy) about (x。,y。)=(π/4,π/4) using this approximation , compute the...
2
by: chb1980 | last post by:
this is a question..about Taylor series... 1) Find the Taylor series approximation of order 2 (n=2) for the funtion f(x,y)=sin(xy) about (x。,y。)=(π/4,π/4) using this approximation , compute the...
1
by: ninita | last post by:
Hi everybody! Does anyone know how to program in java to put the tayor series?? I really need help!
1
by: pancholijaymin | last post by:
can i get the solution immediately for the cpp program onthe taylor series which has prototype as follows:- double getTaylorSeries(double xValue, int maxTerm): Where, the parameter xValue...
1
by: Ben Stone | last post by:
I am trying to write C code for sin(x) using the first 5 terms of the taylor series. I am new to programming and am not allowed to use loops. I don't have much yet, but what I have so far is: /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.