Showing posts with label Taylor and Maclaurin Series. Show all posts
Showing posts with label Taylor and Maclaurin Series. Show all posts

Taylor and Maclaurin Series

Lets try with Taylor and Maclaurin Series. We start by supposing that ƒ is any function that can be represented by a power series:

Let’s try to determine what the coefficients cn must be in terms of ƒ. To begin, notice that if we put x = a in Equation 1, then all terms after the first one are 0 and we get

ƒ(a) = c0

If we differentiate the series in equation 1 term by term...

Here we just convert the degree in radian as we know that
1800 = πc

And the substitution of x = a in equation 2

ƒ'(a) = c1

Now we differentiate both sides of Equation 2 and obtain

Again we put x = a in Equation 3. The result is

ƒ''(a) = 2c2

Let’s apply the procedure one more time. Differentiation of the series in Equation 3 gives

ƒ'''(a) = 2.3c3 = 3!c3

By now you can see the pattern. If we continue to differentiate and substitute x = a, we obtain

Solving this equation for the nth coefficient cn, we get

This formula remains valid even for n = 0 if we adopt the conventions that 0! = 1 and ƒ(0) = ƒ.

Thus we have proved the following theorem.
THEOREM: If ƒ has a power series representation (expansion) at a, that is, if

Substituting this formula for cn back into the series, we see that if ƒ has a power series expansion at a, then it must be of the following form.

This equation is called the Taylor series of the function ƒ at a (or about a or centered at a). For the special case a = 0 the Taylor series becomes

This case arises frequently enough that is is given the special name Maclaurin series.

EXAMPLE 1: Find the Maclaurin series for sin x and prove that it represents sin x for all x.
Solution: We arrange our computation in two columns as follows:

Since the derivatives repeat in a cycle of four, we can write the Maclaurin series as follows:

See the C program to find out sin(x)

EXAMPLE 2: Find the Maclaurin series for cosx and prove that it represents cosx for all x.
Solution: We arrange our computation in two columns as follows:

See the C program to find cos(x)

Collected from Calculus Website, by Kiryl Tsishchanka


C program For Trigonometric ratio.

Lets try with the library function to calculate the trigonometric ratio. Here use the built in function. The C library function double cos(double x) returns the cosine of a radian angle x. Correspondingly the sin(x) and tan(x)

Here we just convert the degree in radian using 1800 = πc (Radian)


// Using Library function
#include<stdio.h>
void main(){
    float tempt, degree;
    printf("Enter the number \(In degree\)\n");
    scanf("%f",&degree);
    tempt = degree;
    degree = degree * 3.14159265359 / 180;
    printf("sin\(%.2f\) = %1.2f\n",tempt, sin(degree));
    printf("cos\(%.2f\) = %1.2f\n",tempt, cos(degree));
    printf("tan\(%.2f\) = %1.2f\n",tempt, tan(degree));


}

Now try with Taylor and Maclaurin Series Following are these equations.. we will transform them in our program.

#include<math.h>
#include<stdio.h>
double  factorial(int num){
        double fact =1;
        int i;
        for(i=1;i<=num;i++){
            fact = fact*i;
        }

        return fact;
}

void main(){
    double tempt, x,sum=1.0;
    int term,counter;
    int  sign =-1.0;
    printf("Enter the term number till you want to calculate the value\n");
    scanf("%d",&term);
    printf("Enter the value of x\n");
    scanf("%lf",&x);
    tempt=x;

//ex
    for(counter=1;counter <= term ; counter++){
        sum = sum + ((pow(x,counter))/(factorial(counter)));
    }
 printf("So the Taylor series of ex around %.1lf is %.15lf \n",tempt,sum);

//sinx
x = x * 3.14159265359 / 180;
sum = x;
for(counter=3;counter<=term;counter+=2){
   sum = sum + (sign * (pow(x,counter))/factorial(counter));
}
printf("The Taylor series of sin(x) around %.1lf is %.15lf \n",tempt,sum);


// cosx
 sum = 1.0;
 sign = -1.0;
 for(counter=2;counter <= term ; counter += 2){
        sum = sum + (sign *(pow(x,counter))/factorial(counter));
        sign = sign * (-1);
    }
 printf("The Taylor series of cos(x) around %.1lf is %.15lf \n",tempt,sum);


}


Explanation

Here we use a user defined function factorial which takes an integer value to calculate it's factorial and the function also return a double type integer.

Here we use some variables
x = the variable of Taylor series.
sum = to hold the value of cos, sin and ex
tempt = to hold the value of x, as we change the value of x into radian.
term = Number of term of Taylor series.
counter = loop counter.
sign = to make negative number.


ex

We initialize the sum with 1, and the series of ex also start with 1 then just use it's corresponding arithmetic expression

sin(x)

Again we initialize the sum with 1 since the series of cos(x) start with 1, the counter variable start with 2 and each time it increases by 2, as the series goes. and the sign variable also changes its negativity after each iteration.

cos(x)

Notice that the sin(x) series start with x so again we initialize the sum with x and goes as like the Taylor series.