Physics Formula Solver

Kinematics

Ohm's Law

Please enter valid numbers for all input fields.

`; resultArea.style.display = 'block'; return; } const calculationFunction = formula.solve[solveForVarKey]; const args = Object.keys(formula.variables) .filter(key => key !== solveForVarKey) .map(key => inputValues[key]); let resultValue; try { resultValue = calculationFunction(...args); } catch (e) { console.error("Calculation error:", e); resultArea.innerHTML = `

Error during calculation. Check inputs or formula logic.

`; resultArea.style.display = 'block'; return; } if (isNaN(resultValue) || !isFinite(resultValue)) { resultArea.innerHTML = `

Could not calculate the result. This might be due to division by zero or other invalid operations (e.g., square root of a negative number for real results).

`; } else { const resultVarDetails = formula.variables[solveForVarKey]; let resultHTML = `

Calculation Result:

`; resultHTML += `

Formula: ${formula.equation}

`; resultHTML += `

Solving for: ${resultVarDetails.name} (${solveForVarKey})

`; resultHTML += `
Inputs:
`; Object.keys(rawInputValuesForDisplay).forEach(key => { const varInfo = formula.variables[key]; resultHTML += `

${varInfo.name} (${key}): ${rawInputValuesForDisplay[key]} ${varInfo.unit}

`; }); resultHTML += `
Result:
`; resultHTML += `

${resultVarDetails.name} (${solveForVarKey}): ${resultValue.toFixed(4)} ${resultVarDetails.unit}

`; // Rounded to 4 decimal places resultArea.innerHTML = resultHTML; // Store for PDF pfs_currentCalculationResult = { toolName: "Physics Formula Solver", category: categoryData.displayName, formula: formula.equation, solveFor: `${resultVarDetails.name} (${solveForVarKey})`, inputs: Object.keys(rawInputValuesForDisplay).map(key => ({ name: `${formula.variables[key].name} (${key})`, value: rawInputValuesForDisplay[key], unit: formula.variables[key].unit })), result: { name: `${resultVarDetails.name} (${solveForVarKey})`, value: resultValue.toFixed(4), unit: resultVarDetails.unit } }; downloadButton.style.display = 'inline-block'; // Show PDF button } resultArea.style.display = 'block'; } function pfs_openTab(evt, tabName) { let i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("pfs-tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("pfs-tablink"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } const currentTabElement = document.getElementById(tabName); if (currentTabElement) { currentTabElement.style.display = "block"; } if (evt && evt.currentTarget) { evt.currentTarget.className += " active"; } else if (tablinks.length > 0 && tabName) { // Fallback for programmatic call for (i = 0; i < tablinks.length; i++) { if (tablinks[i].getAttribute('onclick').includes(tabName)) { tablinks[i].className += " active"; break; } } } pfs_updateTabNavigationButtons(); // Clear results and hide PDF button when switching tabs const resultAreas = document.querySelectorAll('.pfs-result-area'); resultAreas.forEach(area => { area.style.display = 'none'; area.innerHTML = ''; }); document.getElementById('pfs_downloadPdfButton').style.display = 'none'; pfs_currentCalculationResult = null; // Reset form in the newly opened tab (optional, but good UX) const categoryKey = tabName.replace('Tab',''); // e.g. kinematicsTab -> kinematics if (pfs_physicsFormulas[categoryKey]) { const formulaSelect = document.getElementById(`${categoryKey}FormulaSelect`); const solveForSelect = document.getElementById(`${categoryKey}SolveForSelect`); const inputsContainer = document.getElementById(`${categoryKey}InputsContainer`); if (formulaSelect) formulaSelect.value = ""; if (solveForSelect) solveForSelect.innerHTML = ''; if (inputsContainer) inputsContainer.innerHTML = ""; } } function pfs_navigateTab(direction) { const tablinks = Array.from(document.getElementsByClassName("pfs-tablink")); let currentIndex = -1; for(let i=0; i= 0 && nextIndex < tablinks.length) { tablinks[nextIndex].click(); // Simulate click to trigger pfs_openTab } } pfs_updateTabNavigationButtons(); } function pfs_updateTabNavigationButtons() { const tablinks = Array.from(document.getElementsByClassName("pfs-tablink")); let currentIndex = -1; for(let i=0; i [input.name, `${input.value} ${input.unit}`]); if (typeof doc.autoTable === 'function') { doc.autoTable({ startY: yPos, head: [['Variable', 'Value']], body: inputTableBody, theme: 'grid', headStyles: { fillColor: [0, 123, 255] }, // Primary color margin: { left: indent } }); yPos = doc.lastAutoTable.finalY + sectionSpacing; // Get Y position after table } else { // Fallback if autoTable is not available for some reason data.inputs.forEach(input => { doc.text(`${input.name}: ${input.value} ${input.unit}`, indent + 5, yPos); yPos += lineSpacing; }); yPos += lineSpacing; // Extra space before result } doc.setFontSize(12); doc.text("Result:", indent, yPos); yPos += lineSpacing; doc.setFontSize(14); doc.setFont(undefined, 'bold'); doc.text(`${data.result.name}: ${data.result.value} ${data.result.unit}`, indent + 5, yPos); doc.setFont(undefined, 'normal'); doc.save("physics_calculation_result.pdf"); }
Scroll to Top