Law of Cos Calculator

Law of Cosines Calculator body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f8f9fa; color: #333; line-height: 1.6; margin: 0; padding: 0; } .calculator-container { max-width: 800px; margin: 40px auto; background-color: #ffffff; padding: 30px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); } h1, h2 { color: #004a99; text-align: center; margin-bottom: 20px; } .input-section, .result-section, .article-section { margin-bottom: 30px; padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #fdfdfd; } .input-group { margin-bottom: 15px; display: flex; align-items: center; flex-wrap: wrap; } .input-group label { display: block; margin-bottom: 5px; font-weight: bold; color: #004a99; flex: 1 1 150px; /* Responsive flex basis */ min-width: 120px; } .input-group input[type="number"], .input-group select { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; flex: 2 2 200px; /* Responsive flex basis */ box-sizing: border-box; } .input-group input[type="number"]:focus, .input-group select:focus { border-color: #004a99; outline: none; box-shadow: 0 0 0 2px rgba(0, 74, 153, 0.2); } .calculate-button { display: block; width: 100%; padding: 12px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; font-size: 1.1rem; cursor: pointer; transition: background-color 0.3s ease; margin-top: 20px; } .calculate-button:hover { background-color: #218838; } #result { background-color: #e9ecef; padding: 20px; text-align: center; font-size: 1.5rem; font-weight: bold; color: #004a99; border-radius: 5px; margin-top: 20px; } #result p { margin: 5px 0; } .article-section h2 { text-align: left; margin-bottom: 15px; color: #004a99; } .article-section p, .article-section ul { margin-bottom: 15px; } .article-section ul { list-style-type: disc; margin-left: 20px; } @media (max-width: 600px) { .input-group { flex-direction: column; align-items: stretch; } .input-group label { margin-bottom: 10px; flex-basis: auto; } .input-group input[type="number"], .input-group select { flex-basis: auto; width: 100%; } .calculator-container { margin: 20px; padding: 20px; } }

Law of Cosines Calculator

Input Values

Enter two side lengths and the included angle, or three side lengths.

Side c (given a, b, C) Side b (given a, c, B) Side a (given b, c, A) Angle C (given a, b, c) Angle B (given a, c, b) Angle A (given b, c, a)

Results will be displayed here.

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 for all triangles, not just right-angled ones. For any triangle with side lengths \(a\), \(b\), and \(c\), and their opposite angles \(A\), \(B\), and \(C\) respectively, the Law of Cosines states:

  • \(c^2 = a^2 + b^2 – 2ab \cos(C)\)
  • \(b^2 = a^2 + c^2 – 2ac \cos(B)\)
  • \(a^2 = b^2 + c^2 – 2bc \cos(A)\)

These formulas allow you to calculate:

  • The length of a missing side when you know two sides and the included angle (SAS case).
  • The measure of a missing angle when you know all three side lengths (SSS case).

When to Use the Law of Cosines

The Law of Cosines is particularly useful in the following scenarios:

  • SAS (Side-Angle-Side): If you know the lengths of two sides and the measure of the angle between them, you can find the length of the third side.
  • SSS (Side-Side-Side): If you know the lengths of all three sides, you can find the measure of any of the angles.

It's important to note that the Law of Cosines works for any triangle, whether it's acute, obtuse, or right-angled. For right-angled triangles, it simplifies to the Pythagorean theorem because the cosine of 90 degrees is 0.

Calculating Angles from Sides

You can rearrange the Law of Cosines to solve for an angle. For example, to find angle \(C\):

\( \cos(C) = \frac{a^2 + b^2 – c^2}{2ab} \)

Then, you would use the inverse cosine function (arccosine) to find the angle:

\( C = \arccos\left(\frac{a^2 + b^2 – c^2}{2ab}\right) \)

Ensure that your angle inputs and outputs are in degrees as specified by the calculator.

function calculateTriangle() { var sideA = parseFloat(document.getElementById("sideA").value); var sideB = parseFloat(document.getElementById("sideB").value); var sideC = parseFloat(document.getElementById("sideC").value); var angleA_deg = parseFloat(document.getElementById("angleA").value); var angleB_deg = parseFloat(document.getElementById("angleB").value); var angleC_deg = parseFloat(document.getElementById("angleC").value); var calculationType = document.getElementById("calculationType").value; var resultDiv = document.getElementById("result"); resultDiv.innerHTML = "Results will be displayed here."; // Reset result var radiansPerDegree = Math.PI / 180; var degreesPerRadian = 180 / Math.PI; var calculatedSide = null; var calculatedAngle_deg = null; var errorMessages = []; // Function to check if a number is valid and positive (for lengths) var isValidLength = function(val) { return !isNaN(val) && val > 0; }; // Function to check if a number is valid (for angles, can be 0) var isValidAngle = function(val) { return !isNaN(val); }; // Input validation based on calculation type if (calculationType === "sideC" || calculationType === "sideB" || calculationType === "sideA") { if (calculationType === "sideC") { if (!isValidLength(sideA) || !isValidLength(sideB) || !isValidAngle(angleC_deg)) { errorMessages.push("For calculating Side c, sides a and b, and Angle C must be valid positive numbers."); } } else if (calculationType === "sideB") { if (!isValidLength(sideA) || !isValidLength(sideC) || !isValidAngle(angleB_deg)) { errorMessages.push("For calculating Side b, sides a and c, and Angle B must be valid positive numbers."); } } else if (calculationType === "sideA") { if (!isValidLength(sideB) || !isValidLength(sideC) || !isValidAngle(angleA_deg)) { errorMessages.push("For calculating Side a, sides b and c, and Angle A must be valid positive numbers."); } } } else if (calculationType === "angleC" || calculationType === "angleB" || calculationType === "angleA") { if (!isValidLength(sideA) || !isValidLength(sideB) || !isValidLength(sideC)) { errorMessages.push("For calculating an angle, all three side lengths must be valid positive numbers."); } } if (errorMessages.length > 0) { resultDiv.innerHTML = "" + errorMessages.join("") + ""; return; } if (calculationType === "sideC") { var angleC_rad = angleC_deg * radiansPerDegree; var c_squared = Math.pow(sideA, 2) + Math.pow(sideB, 2) – 2 * sideA * sideB * Math.cos(angleC_rad); calculatedSide = Math.sqrt(c_squared); resultDiv.innerHTML = "Calculated Side c: " + calculatedSide.toFixed(4) + ""; } else if (calculationType === "sideB") { var angleB_rad = angleB_deg * radiansPerDegree; var b_squared = Math.pow(sideA, 2) + Math.pow(sideC, 2) – 2 * sideA * sideC * Math.cos(angleB_rad); calculatedSide = Math.sqrt(b_squared); resultDiv.innerHTML = "Calculated Side b: " + calculatedSide.toFixed(4) + ""; } else if (calculationType === "sideA") { var angleA_rad = angleA_deg * radiansPerDegree; var a_squared = Math.pow(sideB, 2) + Math.pow(sideC, 2) – 2 * sideB * sideC * Math.cos(angleA_rad); calculatedSide = Math.sqrt(a_squared); resultDiv.innerHTML = "Calculated Side a: " + calculatedSide.toFixed(4) + ""; } else if (calculationType === "angleC") { var cosC = (Math.pow(sideA, 2) + Math.pow(sideB, 2) – Math.pow(sideC, 2)) / (2 * sideA * sideB); if (cosC 1) { resultDiv.innerHTML = "Invalid side lengths for a triangle. Cosine value out of range."; return; } calculatedAngle_deg = Math.acos(cosC) * degreesPerRadian; resultDiv.innerHTML = "Calculated Angle C: " + calculatedAngle_deg.toFixed(4) + "°"; } else if (calculationType === "angleB") { var cosB = (Math.pow(sideA, 2) + Math.pow(sideC, 2) – Math.pow(sideB, 2)) / (2 * sideA * sideC); if (cosB 1) { resultDiv.innerHTML = "Invalid side lengths for a triangle. Cosine value out of range."; return; } calculatedAngle_deg = Math.acos(cosB) * degreesPerRadian; resultDiv.innerHTML = "Calculated Angle B: " + calculatedAngle_deg.toFixed(4) + "°"; } else if (calculationType === "angleA") { var cosA = (Math.pow(sideB, 2) + Math.pow(sideC, 2) – Math.pow(sideA, 2)) / (2 * sideB * sideC); if (cosA 1) { resultDiv.innerHTML = "Invalid side lengths for a triangle. Cosine value out of range."; return; } calculatedAngle_deg = Math.acos(cosA) * degreesPerRadian; resultDiv.innerHTML = "Calculated Angle A: " + calculatedAngleA_deg.toFixed(4) + "°"; } // Optional: Add a check for triangle inequality for SSS angle calculations if (calculationType.startsWith("angle")) { if (!((sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB + sideC > sideA))) { resultDiv.innerHTML = "The given side lengths do not form a valid triangle (triangle inequality violated)."; return; } } }

Leave a Comment