Created
March 19, 2020 01:46
-
-
Save john212/30fce10aade61a301b8981076b11742b to your computer and use it in GitHub Desktop.
Infinite Series For Sine Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Infinite Series for the Sine Function</title> | |
</head> | |
<body> | |
<h2>Infinite Series for the Sine Function</h2> | |
<P>This program will use one of the many infinite series formulas to calculate | |
an approximate value for the sine function. This infinite series for the sine | |
function is the Taylor series and for this case is the same as the Maclaurin series | |
for the sine function.</P> | |
<P>This particular infinite series converges rapidly, the first 8 tems | |
are within about 0.000000000000001 of the actual value of the sine value. Reminder, | |
the factorial symbol in math "!" means, for n!, to multiply by n(n-1)(n-2) until n(1), | |
Note: 0! = 1 by definition. For example: 4! = 4*3*2*1 = 24.</P> | |
<P>Javascript numbers are always double-precision 64 bit binary (base 2) numbers. There is | |
no "double precision" type.<br> | |
Reference: <a href="https://indepth.dev/here-is-what-you-need-to-know-about-javascripts-number-type/">https://indepth.dev/here-is-what-you-need-to-know-about-javascripts-number-type/</a></P> | |
<P>The infinite series for sin(x) where x is in radians and x is a Real number is:</P> | |
<P>sin(x) = x<sup>1</sup>/1! - x<sup>3</sup>/3! + x<sup>5</sup>/5! - x<sup>7</sup>/7! + x<sup>9</sup>/9!...the pattern continues to infinity.</P> | |
<P>This series is an alternating series, that is, the terms alternate between | |
plus and minus.</P> | |
<P>This program is written in JavaScript.</P> | |
<P>By Mr. C. March, 2020</P> | |
<P>Enter angle in radians, default is pi/3 ~ 1.047197551</P> | |
<P>Enter number of terms to sum, max is 10, default is 8.</P> | |
<P>*********************results********************************</P> | |
<script type="text/javascript"> | |
main(); // must call the main() function to run the program. | |
function main() // define the enveloping "main()" function. | |
{ //start main() function block | |
// **** Define and initialize variables **** | |
var x = ""; // angle, in radians to evaluate, input by user. | |
var default_x = Math.PI/3; | |
var nterms = ""; // number of terms in the infinite series to evaluate, input by user. | |
var default_nterms = 8; | |
var sumOfTerms = 0.0; | |
// ***************************************** | |
// **** Collect user input via popup boxes **** | |
x = prompt("Enter the angle to evaluate in radians", default_x); | |
nterms = prompt("Enter the number of terms of the infinite series to use", default_nterms); | |
// ******************************************** | |
/***** Convert user text input into decimal numbers **** | |
The parseFloat("string") function returns a number, | |
where "string" is the string value you want to parse. | |
The parseInt("string",radix) function parses a string argument and returns an integer | |
of the specified radix (the base in mathematical numeral systems). An integer between | |
2 and 36 represents the radix of the string. Be careful—this does not default to 10! | |
Set radix = 10 (decimal). Leading whitespace in this argument is ignored. Ex: parseInt(n,10). | |
*******************************************************/ | |
x = parseFloat(x); | |
nterms = parseInt(nterms,10); | |
// Data Validation - Ensure user input is > 0 for angle and terms and max terms = 10. | |
if((x <= 0) || (nterms <= 0) || (nterms > 10)) | |
{ //start data validation if block | |
document.write("<P>angle =", x, ", terms =" , nterms); | |
document.write("<P>angle and terms must be greater than zero, max value for number of terms is 10"); | |
return // stops execution of main() function | |
} //endof data validation if block | |
// Display the user input. | |
document.write("<P><b>angle =", x, " radians, terms =" , nterms,"</b></P>"); | |
document.write("<P><b>Javascript value for Math.sin(x) =", Math.sin(x),"</b></P>"); | |
// calculate each term in the infinite series for the sine | |
for (let i = 0; i <= nterms-1; i++) | |
{ // start nterm for loop | |
// Math.pow(base,exponent) returns "base" raised to the power of "exponent". | |
sumOfTerms = sumOfTerms + ( (Math.pow(-1,i)) * (1/(factorial(2*i + 1))) * Math.pow(x,2*i + 1) ); | |
document.write("<P>term = ", i + 1, " sin(x) = ", sumOfTerms ); | |
} // endof nterm for loop | |
// *************************************************** | |
// debugging code - comment out as needed | |
/* | |
console.log("isTriangle is ",isTriangle); | |
*/ | |
// *************************************************** | |
} //endof main() function block | |
// ***************************************************************************** | |
// function to return the factorial of any passed integer | |
// note: the set of integers is abbreviated "Z" | |
function factorial(_Z) // Z is set of integers, _Z means variable is an argument | |
{ //start factorial function block | |
var counter = _Z; // will use the integer argument as the starting value of a counter | |
var factorialZ = 1; | |
if(_Z === 0) | |
{//start if _Z is 0 if block | |
factorialZ = 1; | |
return factorialZ; | |
}//endof if _Z is 0 if block | |
while (counter >= 1) | |
{//start while block | |
factorialZ = counter*factorialZ; | |
counter = counter - 1; | |
}//endof while block | |
return factorialZ; | |
} //endof factorial function block | |
// ***************************************************************************** | |
</script> | |
<p> | |
<b> | |
For more information, visit:<br> | |
<a href="https://en.smath.com/cloud/worksheet/UBKaAzZ3">Infinite Series for the Sine Function</a><br> | |
<a href="https://en.smath.com/cloud/worksheet/LppF8NmG">Infinite Series for the Cosine Functio</a><br> | |
<a href="https://en.smath.com/cloud/worksheet/WvhTsoGS">Infinite Series for e</a><br> | |
<a href="https://en.smath.com/cloud/worksheet/buqQbeTd">Infinite Series for Pi</a><br> | |
<a href="https://en.smath.com/cloud/worksheet/47rawg6N">Comparing the Infinite Series for Pi, e, Sine, and Cosine</a><br> | |
</b> | |
<i>All SM=Studio Math files by Mr.C.</i> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment