MuhammadHananKhan123's picture
Update app.py
1ebf109 verified
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()