Numerical Integration Using JavaScript

Ciprian on Tuesday, May 17, 2022 in Methods, Events and Scopes

NEW! Learn JavaScript by example. Code snippets, how-to's and tutorials. Try now!

This is a quick reminder on how to use mathematical functions inside JavaScript.

Instructions

  • Use * to indicate multiplication: Type 4*x for 4x;
  • Use ^ to indicate powers: Type 4*x^3 for 4√ó3 and 12*x^-6 for 12x?6.
  • Use parentheses to delimit the argument of a function; i.e., type sin(x) rather than sin x.
  • Use parentheses to define the scope of an operation: For example, type 4*x*(x^2+1)^3 for 4x(x2+1)3 or 4^(2*x+1) for 42x+1 or (sin(x))^2 for (sin(x))2.
  • Do not type sin^2(x) for sin2(x), type (sin(x))^2 instead.
  • Do not use brackets [ ] or braces { }, use only parentheses to delimit a mathematics expression.
  • Functions you may use:

Trig: sin, cos, tan, cot, sec, csc;

Inverse Trig: asin, acos, atan;

Log/exponential: ln (natural log), or use log; exp, the natural exponential; Note: You can also enter the natural exponential exp(x) as e^x for ex.

Misc: sqrt, usage sqrt(x) for ?x (or, use exponential notation: x^(1/2)).

See each of the mathematical/trigonometry functions above in their JavaScript form below:

function multiply(num1, num2) {
    // Multiplies two numbers together
    return num1 * num2;
}

function power(base, exponent) {
    // Calculates the power of a base to an exponent
    return Math.pow(base, exponent);
}

function evaluateExpression(expression) {
    // Evaluates a mathematical expression as a string
    return eval(expression);
}

function square(x) {
    // Calculates the square of a number
    return Math.pow(x, 2);
}


function sin(x) {
    // Calculates the sine of an angle in radians
    return Math.sin(x);
}

function cos(x) {
    // Calculates the cosine of an angle in radians
    return Math.cos(x);
}

function tan(x) {
    // Calculates the tangent of an angle in radians
    return Math.tan(x);
}

function cot(x) {
    // Calculates the cotangent of an angle in radians
    return 1 / Math.tan(x);
}

function sec(x) {
    // Calculates the secant of an angle in radians
    return 1 / Math.cos(x);
}

function csc(x) {
    // Calculates the cosecant of an angle in radians
    return 1 / Math.sin(x);
}

function asin(x) {
    // Calculates the inverse sine of a value in radians
    return Math.asin(x);
}

function acos(x) {
    // Calculates the inverse cosine of a value in radians
    return Math.acos(x);
}

function atan(x) {
    // Calculates the inverse tangent of a value in radians
    return Math.atan(x);
}

function ln(x) {
    // Calculates the natural logarithm of a value
    return Math.log(x);
}

function log(x) {
    // Calculates the logarithm of a value
    return Math.log(x);
}

function exp(x) {
    // Calculates the natural exponential of a value
    return Math.exp(x);
}

function sqrt(x) {
    // Calculates the square root of a value
    return Math.sqrt(x);
}

Related posts

2 comments on “Numerical Integration Using JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *