Code Explainer

Sine Series Calculator

This function approximates the sine of an angle using a Taylor series expansion, allowing users to specify terms for increased accuracy. It outputs the calculated sine value and the relative error compared to the actual sine function.


Empty image or helper icon

Prompt

FUNCTION calculate_sine_series(x, n)
    DECLARE approx_sin = 0
    DECLARE actual_sin = CALL math.sin(x)
    
    FOR i FROM 0 TO n DO
        DECLARE factorial = 1
        FOR j FROM 1 TO (2*i + 1) DO
            factorial = factorial * j
        
        IF i MOD 2 == 0 THEN
            DECLARE term = POWER(x, (2*i + 1)) / factorial
        ELSE
            DECLARE term = -POWER(x, (2*i + 1)) / factorial
        END IF
        
        approx_sin = approx_sin + term
        
        IF actual_sin != 0 THEN
            DECLARE error_percent = (ABS(actual_sin - approx_sin) / ABS(actual_sin)) * 100
        ELSE
            DECLARE error_percent = 0
        END IF
        
        PRINT "Term:", term
        PRINT "Approximate sin(x):", approx_sin
        PRINT "Percent Relative Error:", error_percent
    END FOR
END FUNCTION

// Example of calling the function
DECLARE angle = 1.0  // angle in radians
DECLARE order = 10   // number of terms to compute
CALL calculate_sine_series(angle, order)

Answer

Code Explanation of calculate_sine_series

Overview

The provided code defines a function named calculate_sine_series, which approximates the value of the sine function for a given angle, x, using a Taylor series expansion. It allows the user to specify the number of terms (n) to include in the series, enhancing accuracy with each added term.

Code Breakdown

Function Declaration

FUNCTION calculate_sine_series(x, n)
  • This line declares a function named calculate_sine_series that takes two parameters:
    • x: the angle in radians for which the sine is calculated.
    • n: the number of terms to be included in the approximation.

Variable Initialization

DECLARE approx_sin = 0
DECLARE actual_sin = CALL math.sin(x)
  • approx_sin: This variable is initialized to 0 and will hold the cumulative sum of the series terms.
  • actual_sin: This calls the built-in sine function to get the actual value of sin(x), providing a point of comparison for accuracy.

Loop Through Terms

FOR i FROM 0 TO n DO
  • A loop is set to iterate n+1 times (from 0 to n), marking each iteration with index i.

Factorial Calculation

DECLARE factorial = 1
FOR j FROM 1 TO (2*i + 1) DO
    factorial = factorial * j
END FOR
  • A nested loop computes the factorial of (2*i + 1), which is needed in the sine series formula.
  • The factorial builds on itself, multiplying by j for each integer from 1 to (2*i + 1).

Term Calculation

IF i MOD 2 == 0 THEN
    DECLARE term = POWER(x, (2*i + 1)) / factorial
ELSE
    DECLARE term = -POWER(x, (2*i + 1)) / factorial
END IF
  • A conditional evaluates the current term's index:
    • If i is even, the term is positive.
    • If i is odd, the term is negative.
  • It uses the formulas:
    • Term = ( \frac{x^{(2i+1)}}{(2i+1)!} )

Cumulative Approximation and Error Calculation

approx_sin = approx_sin + term

IF actual_sin != 0 THEN
    DECLARE error_percent = (ABS(actual_sin - approx_sin) / ABS(actual_sin)) * 100
ELSE
    DECLARE error_percent = 0
END IF
  • The current term is added to approx_sin.
  • The code then calculates the percent relative error:
    • If actual_sin is not zero, the relative error is calculated as:
      • Error Percent = ( \frac{|actual_sin - approx_sin|}{|actual_sin|} \times 100 )
    • If actual_sin equals zero, the error is set to 0.

Display Results

PRINT "Term:", term
PRINT "Approximate sin(x):", approx_sin
PRINT "Percent Relative Error:", error_percent
  • After calculating each term, the term, approximate sine, and percent error are printed for the user’s information.

Function Call Example

DECLARE angle = 1.0  // angle in radians
DECLARE order = 10   // number of terms to compute
CALL calculate_sine_series(angle, order)
  • An example that defines an angle of 1.0 radians and orders 10, then calls the calculate_sine_series function with these parameters.

Key Concepts

Taylor Series

The sine function can be represented using a Taylor series:

  • The series converges towards the function value as more terms are added.
  • The formula for sine is:
    • ( \sin(x) = \sum_{i=0}^{\infty} \frac{(-1)^i x^{(2i+1)}}{(2i + 1)!} )

Factorial

  • Factorials, denoted as n!, are the products of all positive integers up to n. They grow very quickly and are a crucial part of calculating permutations, combinations, and series expansions.

Modular Arithmetic

  • The expression i MOD 2 == 0 checks if i is even, which determines the sign of each term in the series.

Alternative Example

For variance, the function could also be implemented in Python with native features:

import math

def calculate_sine_series(x, n):
    approx_sin = 0
    actual_sin = math.sin(x)
    
    for i in range(n + 1):
        factorial = 1
        for j in range(1, 2 * i + 2):
            factorial *= j
        
        term = ((-1) ** i) * (x ** (2 * i + 1)) / factorial
        approx_sin += term
        
        error_percent = (abs(actual_sin - approx_sin) / abs(actual_sin)) * 100 if actual_sin != 0 else 0
        
        print(f"Term: {term}, Approximate sin(x): {approx_sin}, Percent Relative Error: {error_percent}")

# Example call
angle = 1.0
order = 10
calculate_sine_series(angle, order)

This example in Python reflects similar logic while employing built-in functionality for reading clarity.


This structured breakdown provides a comprehensive understanding of the calculate_sine_series function, emphasizing its construction, purpose, and relevant mathematical principles.

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 function approximates the sine of an angle using a Taylor series expansion, allowing users to specify terms for increased accuracy. It outputs the calculated sine value and the relative error compared to the actual sine function.