473,387 Members | 1,545 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,387 software developers and data experts.

Using Taylor Series

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\nPart 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.
Oct 25 '06 #1
12 39100
Banfa
9,065 Expert Mod 8TB
The pseudo code to calculate the value of any series goes something like this

Expand|Select|Wrap|Line Numbers
  1. INITIALISE SERIES VALUE
  2.  
  3. FOR EACH TERM IN THE SERIES
  4.     SERIES VALUE = SERIES VALUE + VALUE OF TERM
  5. END FOR
  6.  
  7. OUTPUT SERIES VALUE
  8.  
Oct 25 '06 #2
sorry, i'm new to programming
could you give like a simple example of how to do it? how many double / float values would i need to define?
Oct 26 '06 #3
Banfa
9,065 Expert Mod 8TB
A minimum of 1 double value to hold the total and 2 integer values, 1 to hold the current term number and 1 to hold the number of terms to evaluate. You may need more for complex terms or to make the calculation more readable.

so taking ** to mean to the power of (e.g. 3 ** 2 = 9) to evaluate the series

1 / (2**0) + 1 / (2**1) + 1 / (2**2) + 1 / (2**3) + 1 / (2**4) + ... + 1 / (2**n)

i.e. the some of recipricals of the powers of 2 (this tends to the value 2)

the code could look something like

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>   /* For the pow function */
  2. #include <math.h>    /* For the pow function */
  3.  
  4. int main() 
  5.     double value;
  6.     int term;
  7.     int number_of_terms = 10; /* Note I have arbitarily chose this value for this case */
  8.  
  9.     value = 0.0;  /* Initialise series value */
  10.  
  11.     /* For each term required */
  12.     for( term=0; term<number_of_terms; term++)
  13.     {
  14.         value += 1.0 / pow(2.0, term); /* Add the value of this term to the total */
  15.     }
  16.  
  17.     printf( "The sum of the recipricol of the powers of 2 to %d terms is %f\n",
  18.             number_of_terms, 
  19.             value);
  20.     return 0;
  21. }
Oct 26 '06 #4
okay thanks
i'll see if that works and update if it does or not =]
Oct 27 '06 #5
okay, i tried to do what you said. this is the code i came up with.

#include <stdio.h>
#include <math.h>
#define PI 3.141592654

main()
{
int n_terms, n=0;
double angle_deg, angle_rad, csa, csa2, value;
printf("\n\nPart 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);
for(n=0; n<n_terms; n++){
value += 0.5 * 1 + (pow(-1.0, n) * pow((2 * angle_rad), (2 * n)) / (2 * n));
}
printf("cos^2(x) = %g", value);
}


is there a definition in C programming that lets me do factorials? because when i defined the value...i need the last (2 * n) to be (2 * n)!.
thanks
Oct 28 '06 #6
This is my program so far. I got the factorial to work.


#include <stdio.h>
#include <math.h>
#define PI 3.141592654

main()
{
int n_terms, n=0;
double angle_deg, angle_rad, csa, csa2, value, sum, factorial;
printf("\n\nPart 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);
for(n=0; n<n_terms; n++){
value = 0.5 * (1 + sum);
sum += (pow(-1.0, n_terms) * (pow((2. * angle_rad), (2. * n_terms))/ factorial));
factorial += (2 * (n_terms - n));
}
printf("cos^2(x) = %g\n", value);
}


however, the value for the taylor series will not come out correctly. anyone have any suggestions. this is what the taylor series should be.

cos^2(x)=0.5*(1+Sum[(-1)^n (2*x)^(2n)/(2n)!])
Oct 29 '06 #7
Banfa
9,065 Expert Mod 8TB
You never initialise the value factorial
Oct 30 '06 #8
jhex21
3
but what if there is no math.h

can you still make a program converting the sine of a number using taylor series?

how? pls help thnx..
Apr 29 '08 #9
Banfa
9,065 Expert Mod 8TB
but what if there is no math.h

can you still make a program converting the sine of a number using taylor series?

how? pls help thnx..
Why would there be no math.h?
Apr 29 '08 #10
jhex21
3
can nyou try it using the taylor series?
Apr 29 '08 #11
jhex21
3
Why would there be no math.h?
using this format:
in getting the sin of a value x:

sin(x) = x^1/1! - x^3/3! + x^5/5!....
Apr 29 '08 #12
Laharl
849 Expert 512MB
You do know that is a Taylor series, right? Whether or not <math.h> is there has no effect on that...and <math.h> is a standard library. It should always be there, unless you're on a microcontroller or something. Note that if you're using gcc, you need to compile with the -lm flag to actually have the math library included, as well as the #include.

Thread necromancy aside, you'd just need to write your own exponent function to replace pow(), as that's the only math.h function involved in the computations.
Apr 29 '08 #13

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

Similar topics

52
by: Dick Moores | last post by:
I need to figure out how to compute pi to base 12, to as many digits as possible. I found this reference, <http://mathworld.wolfram.com/Base.html>, but I really don't understand it well enough....
5
by: oz | last post by:
Hi, Where can i find code examples of connecting with MQ from VB.net(not using msmq bridge) BR Oz --- Outgoing mail is certified Virus Free.
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
mia023
by: mia023 | last post by:
Hi i just want to ask if i want to write a java program that compute sin x and cos x using Taylor series. I don't know the logic about how to write this program and how to get Taylor series through...
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: arperidot | last post by:
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 (k...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.