Composition of Functions Calculator

Composition of Functions Calculator

Use 'x' as the variable (e.g., x^2 + 5*x – 2)

Results

(f ∘ g)(x)
f(g(x))
(g ∘ f)(x)
g(f(x))
function calculateComposition() { var fInput = document.getElementById('f_func').value.toLowerCase(); var gInput = document.getElementById('g_func').value.toLowerCase(); var xVal = parseFloat(document.getElementById('x_value').value); var resultsArea = document.getElementById('results_area'); if (fInput === "" || gInput === "" || isNaN(xVal)) { alert("Please enter both function expressions and a value for x."); return; } try { // Function to sanitize and evaluate var mathEval = function(expr, val) { // Replace ^ with ** for JS exponentiation var cleanExpr = expr.replace(/\^/g, "**"); // Handle implicit multiplication like 2x cleanExpr = cleanExpr.replace(/(\d)x/g, "$1*x"); // Replace x with the actual value in parentheses var substituted = cleanExpr.replace(/x/g, "(" + val + ")"); return eval(substituted); }; // Calculate (f ∘ g)(x) = f(g(x)) var gValue = mathEval(gInput, xVal); var fogValue = mathEval(fInput, gValue); // Calculate (g ∘ f)(x) = g(f(x)) var fValue = mathEval(fInput, xVal); var gofValue = mathEval(gInput, fValue); document.getElementById('fog_result').innerText = fogValue.toLocaleString(undefined, {maximumFractionDigits: 4}); document.getElementById('gof_result').innerText = gofValue.toLocaleString(undefined, {maximumFractionDigits: 4}); document.getElementById('fog_step').innerText = "g(" + xVal + ") = " + gValue.toFixed(2) + " → f(" + gValue.toFixed(2) + ")"; document.getElementById('gof_step').innerText = "f(" + xVal + ") = " + fValue.toFixed(2) + " → g(" + fValue.toFixed(2) + ")"; resultsArea.style.display = 'block'; } catch (e) { alert("Error in mathematical expression. Please use standard operators like +, -, *, /, ^ and ensure 'x' is the only variable."); } }

Understanding Function Composition

In mathematics, the composition of functions is an operation where you apply one function to the result of another. If you have two functions, f(x) and g(x), the composition (f ∘ g)(x) is read as "f of g of x".

The Core Formulas

  • 🔵 (f ∘ g)(x) = f(g(x)): You evaluate g(x) first, then plug that result into f.
  • 🔴 (g ∘ f)(x) = g(f(x)): You evaluate f(x) first, then plug that result into g.

Step-by-Step Example

Let's say we have the following functions:

  • f(x) = x + 5
  • g(x) = x2
  • x = 3

To find (f ∘ g)(3):

  1. Find g(3): 32 = 9.
  2. Plug 9 into f: f(9) = 9 + 5 = 14.
  3. Result: 14.

To find (g ∘ f)(3):

  1. Find f(3): 3 + 5 = 8.
  2. Plug 8 into g: g(8) = 82 = 64.
  3. Result: 64.

Important Note: Function composition is not usually commutative. This means that (f ∘ g)(x) is rarely equal to (g ∘ f)(x), as shown in the example above (14 vs 64).

Domain and Range Considerations

When working with compositions, the domain of (f ∘ g) is the set of all values x in the domain of g such that g(x) is in the domain of f. This is crucial when dealing with square roots or denominators that cannot be zero.

Common Use Cases

  • Physics: Calculating nested rates of change (e.g., how pressure changes over time as altitude changes).
  • Economics: Applying a tax function to a price function.
  • Computer Science: Piping the output of one algorithm as the input to the next in data processing.

Leave a Comment