POP Lab Manual

Program 7

Develop a program to compute sin(x)/cos(x) using Taylor series approximation and compare the result with the built-in library function. The program implements Taylor series expansion for both sine and cosine functions and demonstrates numerical analysis techniques.
Taylor Series Formulas:
sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + ...
cos(x) = 1 - x²/2! + x⁴/4! - x⁶/6! + ...
tan(x) = sin(x) / cos(x)

Algorithm

  1. Initialize: Define number of terms for Taylor series approximation
  2. Create Factorial Function: Implement function to calculate n!
  3. Taylor Sin Function: Compute sin(x) using series: x - x³/3! + x⁵/5! - ...
  4. Taylor Cos Function: Compute cos(x) using series: 1 - x²/2! + x⁴/4! - ...
  5. Input Angle: Read angle value in radians from user
  6. Calculate Approximation: Compute tan(x) = sin(x)/cos(x) using Taylor series
  7. Library Comparison: Calculate tan(x) using built-in tan() function
  8. Display Results: Show both results and compute difference for analysis

Flowchart

START
Input x
Taylor sin(x)
Taylor cos(x)
tan(x) = sin(x)/cos(x)
Library tan(x)
Compare & Display
STOP

Code


#include <stdio.h>
#include <math.h>


#define TERMS 10   // Number of terms for approximation

// Function to compute factorial
double fact(int n) {
    double f = 1;
    for (int i = 1; i <= n; i++)
        f *= i;
    return f;
}

// Function to compute sin(x) using Taylor series
double taylor_sin(double x) {
    double sum = 0;
    for (int i = 0; i < TERMS; i++) {
        int power = 2 * i + 1;
        double term = pow(-1, i) * pow(x, power) / fact(power);
        sum += term;
    }
    return sum;
}

// Function to compute cos(x) using Taylor series
double taylor_cos(double x) {
    double sum = 0;
    for (int i = 0; i < TERMS; i++) {
        int power = 2 * i;
        double term = pow(-1, i) * pow(x, power) / fact(power);
        sum += term;
    }
    return sum;
}

int main() {
    double x;
    printf("Enter angle in radians: ");
    scanf("%lf", &x);

    double sinx = taylor_sin(x);
    double cosx = taylor_cos(x);

    // Taylor approximation
    double approx_tan = sinx / cosx;

    // Library function
    double builtin_tan = tan(x);

    printf("\nUsing Taylor Series:\n");
    printf("sin(x) ≈ %.6lf\n", sinx);
    printf("cos(x) ≈ %.6lf\n", cosx);
    printf("tan(x) ≈ %.6lf\n", approx_tan);

    printf("\nUsing Library Function:\n");
    printf("tan(x) = %.6lf\n", builtin_tan);

    printf("\nInference: Difference = %.6lf\n", fabs(approx_tan - builtin_tan));

    return 0;
}

Output


// Output 1 - Small angle (π/6 ≈ 0.5236 radians)
Enter angle in radians: 0.5236

Using Taylor Series:
sin(x) ≈ 0.500000
cos(x) ≈ 0.866025
tan(x) ≈ 0.577350

Using Library Function:
tan(x) = 0.577350

Inference: Difference = 0.000000

// Output 2 - Medium angle (π/4 ≈ 0.7854 radians)
Enter angle in radians: 0.7854

Using Taylor Series:
sin(x) ≈ 0.707107
cos(x) ≈ 0.707107
tan(x) ≈ 1.000000

Using Library Function:
tan(x) = 1.000000

Inference: Difference = 0.000000

// Output 3 - Larger angle (π/3 ≈ 1.0472 radians)
Enter angle in radians: 1.0472

Using Taylor Series:
sin(x) ≈ 0.866025
cos(x) ≈ 0.500000
tan(x) ≈ 1.732051

Using Library Function:
tan(x) = 1.732051

Inference: Difference = 0.000001

// Output 4 - Analysis for accuracy
Enter angle in radians: 1.5708

Using Taylor Series:
sin(x) ≈ 1.000000
cos(x) ≈ 0.000000
tan(x) ≈ 1570.796327

Using Library Function:
tan(x) = 1633.123935

Inference: Difference = 62.327608
          
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.