Saturday, August 8, 2026

Robotic Arm Angle Approximation || C Lab Programs

 Program 05: Approximate sin(x) using Taylor series. 

Problem Statement:: A sensor in a robotic arm needs to calculate the angle of rotation in real-time, but the hardware doesn't support built-in trigonometric functions. Develop a C program to approximate the value of sin(x) using a series expansion method for improved performance. 

Problem Description:

  • Input: A single real number x (in degrees) representing the angle.

  • Output: A real number representing the approximated value of sin(x), computed using the Taylor Series expansion.

  • Constraints: Angle should be between -100 and 100 degrees; accuracy error tolerance $\le$ 0.001; avoid math library functions like sin(), cos(), or pow().

  • Method: Use Taylor series expansion: $\sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \dots$ 

Pgm Logic:

  1. Start.

  2. Input the angle x in degrees.

  3. Convert degrees to radians: $x_{rad} = x \times \frac{\pi}{180}$.

  4. Initialize: sum = x_rad, nume = x_rad, fact = 1, and i = 2.

  5. Enter a loop:

    • Update factorial: fact = fact * i * (i + 1).

    • Update numerator with alternating sign: nume = -nume * x_rad * x_rad.

    • Calculate current term: term = nume / fact.

    • Add term to sum.

    • Increment i by 2.

    • Repeat while fabs(term) >= 0.0001.

  6. Print the final sum as the approximate sin(x).

  7. Stop. 

Program Code:

// Purpose: To approximate the value of sin(x) using the Taylor series expansion method.

#include <stdio.h>

#include <math.h>

#define PI 3.142


void main()

{

    float sum, term, x, nume;

    int deg, i = 2;

    float fact = 1.0;

    printf("Enter angle in degrees: ");

    scanf("%d", &deg);

    x = (deg * PI) / 180.0;

    sum = x;

    nume = x;

    do

    {

        fact = fact * i * (i + 1);

        nume = -nume * x * x;

        term = nume / fact;

        sum += term;

        i += 2;

    } while (fabs(term) >= 0.0001);

    printf("The approximate value of sin(%d) is: %.4f\n", deg, sum);

}


Output: 

Enter angle in degrees: 30 

The approximate value of sin(30) is: 0.5000 

RESULT: Thus the program has been executed and the output was verified. 

Remarks: This program was compiled and run in the Code::Blocks IDE. It demonstrates how to perform complex mathematical operations using only basic arithmetic loops. 

Program Explanation: The program converts degree input to radians as required by the Taylor formula. It then iteratively calculates each term of the infinite series, adding them to a running total until the individual terms become smaller than the required precision threshold (0.0001).


For all 2026 published C Lab Program posts Index page: click here

For all 2026 published articles list:click here

…till the next post, bye-bye & take care