File size: 2,411 Bytes
1ebf109 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import streamlit as st
import sympy as sp
# Function to calculate and explain physics problems
def solve_physics_problem(equation, **kwargs):
explanation = ""
# Parse the equation and input values
try:
eq = sp.sympify(equation)
symbols = eq.free_symbols
# Check for sufficient input
if len(kwargs) < len(symbols) - 1:
explanation += "\nInsufficient inputs provided to solve the equation."
return explanation, None
# Substitute known values
eq_substituted = eq
for symbol in symbols:
if str(symbol) in kwargs:
eq_substituted = eq_substituted.subs(symbol, kwargs[str(symbol)])
# Solve for the unknown
unknowns = [s for s in symbols if str(s) not in kwargs]
if len(unknowns) != 1:
explanation += "\nUnable to determine a single unknown variable."
return explanation, None
solution = sp.solve(eq_substituted, unknowns[0])
explanation += f"Step 1: Start with the equation: {equation}\n"
explanation += f"Step 2: Substitute the known values into the equation: {eq_substituted}\n"
explanation += f"Step 3: Solve for the unknown variable {unknowns[0]}:\n"
explanation += f"Solution: {unknowns[0]} = {solution[0]}\n"
return explanation, solution[0]
except Exception as e:
explanation += f"Error: {e}\n"
return explanation, None
# Streamlit App
def main():
st.title("Physics Calculator")
st.write("This app helps you solve physics problems step-by-step. Provide the equation and the known parameters.")
# Input Section
equation = st.text_input("Enter the equation (e.g., F = m * a):", "F = m * a")
num_params = st.number_input("Enter the number of known parameters:", min_value=1, step=1)
input_params = {}
for i in range(num_params):
param_name = st.text_input(f"Enter the name of parameter {i + 1}:")
param_value = st.number_input(f"Enter the value of {param_name}:")
input_params[param_name] = param_value
if st.button("Solve"):
explanation, solution = solve_physics_problem(equation, **input_params)
st.subheader("Explanation:")
st.text(explanation)
if solution is not None:
st.subheader("Solution:")
st.write(solution)
if __name__ == "__main__":
main() |