Pseudo Code Generator

Sine Function Evaluation Using Infinite Series

This pseudo code computes the sine of an angle using its series expansion, prints computed values and percent relative error for increased accuracy, illustrating the approximation process through iterative term evaluation.


Empty image or helper icon

Prompt

The sine function can be evaluated by the following infinite series:

Write an algorithm to implement this formula so that it computes and prints out the values of sin x as
each term in the series is added. Compute and print in sequence the values for

Up to order term n. For each of the preceding, compute and display the percent relative error as

Answer

Pseudo Code for Evaluating the Sine Function Using Infinite Series

Problem Statement

The sine function can be approximated using an infinite series. This pseudo code will compute the sine of a given angle x up to a specified term n, print each computed value, and display the percent relative error.

Sine Series Expansion

The sine function can be expressed as: [ \text{sin}(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + ... ] Where ( n ) is the order of the term in the series that is included for the approximation.

Definitions

  • x: Input angle in radians.
  • n: Maximum order term to calculate.
  • actual_value: Actual sine value calculated using a built-in function.

Pseudo Code

FUNCTION calculate_sine_series(x, n)
    INITIALIZE sine_value = 0.0
    INITIALIZE term_sign = 1
    INITIALIZE factorial = 1
    INITIALIZE power_of_x = x
        
    PRINT "Term Number | Sine Value | Percent Relative Error"

    FOR i FROM 0 TO n DO
        IF i > 0 THEN
            factorial = factorial * (2 * i) * (2 * i + 1) // Update factorial for (2i-1)!
            power_of_x = power_of_x * x * x // Update x^2 for the next term
        END IF
        
        term = term_sign * (power_of_x / factorial) // Calculate the term
        
        sine_value = sine_value + term // Update sine value
        percent_error = ABS((sine_value - sin(x)) / sin(x)) * 100 // Calculate percent relative error

        PRINT i, "|", sine_value, "|", percent_error

        term_sign = -term_sign // Alternate the sign for the next term
    END FOR
END FUNCTION

Explanation of Pseudo Code

  • Initialization: Variables for accumulating the sine value, keeping track of the sign, computing the factorial, and the power of x are initialized.

  • Loop: A loop runs from 0 to n, where:

    • The factorial value is updated for each new term in the series.
    • The power of x is updated by multiplying the previous power by x * x (to account for ( x^2 ) increase).
  • Term Calculation: Each term of the sine series is calculated using the current powers of x and the factorial.

  • Error Calculation: The percent relative error is computed and printed for each term, comparing the current sine value against the actual sine value.

  • Output: The number of terms, computed sine value, and percent error are printed in a formatted manner.

Conclusion

This pseudo code provides a clear and structured approach for calculating the sine function using an infinite series. It highlights the essential operations and how to track changes to the sine value, providing insight into the accuracy as more terms are evaluated.

For further learning on this topic and enhancing your skills in data science, consider exploring the courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This pseudo code computes the sine of an angle using its series expansion, prints computed values and percent relative error for increased accuracy, illustrating the approximation process through iterative term evaluation.