Law of Cosine Calculator

Law of Cosine Calculator

Use this calculator to find a missing side or angle of a triangle using the Law of Cosines. Enter three known values, and the calculator will solve for the missing one. Ensure you leave only one field blank.

.law-of-cosine-calculator { font-family: 'Arial', sans-serif; background-color: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); max-width: 600px; margin: 20px auto; border: 1px solid #ddd; } .law-of-cosine-calculator h2 { color: #333; text-align: center; margin-bottom: 20px; } .law-of-cosine-calculator p { color: #555; margin-bottom: 15px; line-height: 1.6; } .calculator-inputs label { display: block; margin-bottom: 5px; color: #333; font-weight: bold; } .calculator-inputs input[type="number"] { width: calc(100% – 22px); padding: 10px; margin-bottom: 15px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } .calculator-inputs button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin-right: 10px; transition: background-color 0.3s ease; } .calculator-inputs button:hover { background-color: #0056b3; } .calculator-inputs button:last-of-type { background-color: #6c757d; } .calculator-inputs button:last-of-type:hover { background-color: #5a6268; } .calculator-result { margin-top: 20px; padding: 15px; background-color: #e9ecef; border: 1px solid #dee2e6; border-radius: 4px; color: #333; font-size: 1.1em; font-weight: bold; min-height: 50px; display: flex; align-items: center; justify-content: center; text-align: center; } .calculator-result.error { background-color: #f8d7da; color: #721c24; border-color: #f5c6cb; } function calculateLawOfCosines() { var sideA = parseFloat(document.getElementById('inputSideA').value); var sideB = parseFloat(document.getElementById('inputSideB').value); var sideC = parseFloat(document.getElementById('inputSideC').value); var angleC_deg = parseFloat(document.getElementById('inputAngleC').value); var resultDiv = document.getElementById('lawOfCosineResult'); resultDiv.className = 'calculator-result'; // Reset class var inputs = [sideA, sideB, sideC, angleC_deg]; var missingCount = 0; var missingField = "; if (isNaN(sideA)) { missingCount++; missingField = 'sideA'; } if (isNaN(sideB)) { missingCount++; missingField = 'sideB'; } if (isNaN(sideC)) { missingCount++; missingField = 'sideC'; } if (isNaN(angleC_deg)) { missingCount++; missingField = 'angleC'; } if (missingCount === 0) { resultDiv.innerHTML = 'Error: All values are provided. Please leave one field blank to calculate.'; resultDiv.classList.add('error'); return; } if (missingCount > 1) { resultDiv.innerHTML = 'Error: Please provide exactly three values and leave only one field blank.'; resultDiv.classList.add('error'); return; } // Validate positive lengths for sides if ((!isNaN(sideA) && sideA <= 0) || (!isNaN(sideB) && sideB <= 0) || (!isNaN(sideC) && sideC <= 0)) { resultDiv.innerHTML = 'Error: Side lengths must be positive numbers.'; resultDiv.classList.add('error'); return; } // Validate angle C range if (!isNaN(angleC_deg) && (angleC_deg = 180)) { resultDiv.innerHTML = 'Error: Angle C must be between 0 and 180 degrees (exclusive) for a non-degenerate triangle.'; resultDiv.classList.add('error'); return; } if (missingField === 'sideC') { // Case: Solve for side c (SAS) var angleC_rad = angleC_deg * (Math.PI / 180); var c_squared = (sideA * sideA) + (sideB * sideB) – (2 * sideA * sideB * Math.cos(angleC_rad)); if (c_squared < 0) { resultDiv.innerHTML = 'Error: Invalid triangle. The given sides and angle cannot form a real triangle.'; resultDiv.classList.add('error'); return; } var calculatedSideC = Math.sqrt(c_squared); resultDiv.innerHTML = 'Calculated Side c: ' + calculatedSideC.toFixed(4) + ''; } else if (missingField === 'angleC') { // Case: Solve for angle C (SSS) // Check triangle inequality if (!((sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB + sideC > sideA))) { resultDiv.innerHTML = 'Error: The given side lengths do not form a valid triangle (Triangle Inequality Theorem).'; resultDiv.classList.add('error'); return; } var cosC = ((sideA * sideA) + (sideB * sideB) – (sideC * sideC)) / (2 * sideA * sideB); // Clamp cosC to [-1, 1] to prevent Math.acos from returning NaN due to floating point inaccuracies cosC = Math.max(-1, Math.min(1, cosC)); var angleC_rad = Math.acos(cosC); var calculatedAngleC_deg = angleC_rad * (180 / Math.PI); resultDiv.innerHTML = 'Calculated Angle C: ' + calculatedAngleC_deg.toFixed(4) + ' degrees'; } else { // Case: Missing sideA or sideB (more complex, requires quadratic equation or Law of Sines if another angle is known) resultDiv.innerHTML = 'Error: This calculator is designed to solve for Side c or Angle C. Please ensure either Side c or Angle C is the missing value.'; resultDiv.classList.add('error'); } } function clearLawOfCosines() { document.getElementById('inputSideA').value = "; document.getElementById('inputSideB').value = "; document.getElementById('inputSideC').value = "; document.getElementById('inputAngleC').value = "; document.getElementById('lawOfCosineResult').innerHTML = "; document.getElementById('lawOfCosineResult').className = 'calculator-result'; }

Understanding the Law of Cosines

The Law of Cosines is a fundamental theorem in trigonometry that relates the lengths of the sides of a triangle to the cosine of one of its angles. It is a generalization of the Pythagorean theorem, which only applies to right-angled triangles.

When to Use the Law of Cosines

The Law of Cosines is particularly useful in two main scenarios:

  1. Side-Angle-Side (SAS): When you know two sides of a triangle and the included angle (the angle between those two sides), you can use the Law of Cosines to find the length of the third side.
  2. Side-Side-Side (SSS): When you know the lengths of all three sides of a triangle, you can use the Law of Cosines to find the measure of any of the angles.

The Formulas

For a triangle with sides a, b, and c, and angles A, B, and C opposite those respective sides, the Law of Cosines can be stated as:

  • To find a side (e.g., side c):
    c² = a² + b² - 2ab cos(C)
    Similarly for a and b: a² = b² + c² - 2bc cos(A) b² = a² + c² - 2ac cos(B)
  • To find an angle (e.g., angle C):
    cos(C) = (a² + b² - c²) / (2ab)
    Similarly for A and B: cos(A) = (b² + c² - a²) / (2bc) cos(B) = (a² + c² - b²) / (2ac)

Our calculator focuses on solving for side c or angle C, given the other necessary inputs.

How to Use This Calculator

  1. To find a missing side (SAS): Enter the lengths of the two known sides (e.g., 'a' and 'b') and the measure of the included angle (e.g., 'C' in degrees). Leave the field for the third side ('c') blank.
  2. To find a missing angle (SSS): Enter the lengths of all three sides ('a', 'b', and 'c'). Leave the field for the angle opposite the side you want to find (e.g., 'C') blank.
  3. Click "Calculate". The result will appear below.
  4. Click "Clear" to reset all fields.

Example Scenarios

Example 1: Finding a Side (SAS)

Imagine you have a triangle where:

  • Side 'a' = 10 units
  • Side 'b' = 12 units
  • Angle 'C' = 80 degrees

You want to find the length of Side 'c'.

Using the calculator:

  1. Enter 10 in "Side 'a'".
  2. Enter 12 in "Side 'b'".
  3. Enter 80 in "Angle 'C'".
  4. Leave "Side 'c'" blank.
  5. Click "Calculate".

The calculator will output: Calculated Side c: ~14.2837

Example 2: Finding an Angle (SSS)

Consider a triangle with the following side lengths:

  • Side 'a' = 7 units
  • Side 'b' = 8 units
  • Side 'c' = 13 units

You want to find the measure of Angle 'C' (opposite side 'c').

Using the calculator:

  1. Enter 7 in "Side 'a'".
  2. Enter 8 in "Side 'b'".
  3. Enter 13 in "Side 'c'".
  4. Leave "Angle 'C'" blank.
  5. Click "Calculate".

The calculator will output: Calculated Angle C: ~120.0000 degrees

Leave a Comment