Menausar/ExponentialGD
0
1import streamlit as st2import numpy as np3import pandas as pd4import matplotlib.pyplot as plt5from io import BytesIO6 7st.set_page_config(page_title="Exponential Growth & Decay Applets", layout="wide")8 9st.title("๐ Exponential Growth & Decay Interactive Applets")10st.write("Select a student scenario, adjust parameters, and explore exponential models.")11 12# -----------------------------13# Utility Functions14# -----------------------------15def exponential_model(initial, rate, time):16 return initial * np.exp(rate * time)17 18def export_plot(fig):19 buf = BytesIO()20 fig.savefig(buf, format="png")21 buf.seek(0)22 return buf23 24# -----------------------------25# Scenario Selector26# -----------------------------27scenarios = {28 "Energy / Battery Decay (Cheer, Robotics, Gaming, Submersibles)": "decay",29 "Savings, Revenue, Attendance, Views Growth": "growth",30 "Heart Rate / Recovery / Fatigue": "decay",31 "Bacteria / Germ Growth & Treatment": "both",32 "Cooling / Heat Decay": "decay",33 "Skill Improvement (Accuracy, Efficiency)": "growth",34}35 36scenario = st.selectbox("Choose an Applet Scenario", list(scenarios.keys()))37 38# -----------------------------39# Sidebar Controls40# -----------------------------41st.sidebar.header("๐ง Model Parameters")42 43initial = st.sidebar.slider("Initial Value", 1.0, 1000.0, 100.0)44rate = st.sidebar.slider(45 "Growth (+) or Decay (โ) Rate",46 -2.0, 2.0, -0.3 if scenarios[scenario] == "decay" else 0.3,47 step=0.0148)49time_max = st.sidebar.slider("Time Duration", 1, 100, 30)50 51time = np.linspace(0, time_max, 300)52 53# -----------------------------54# Model Calculation55# -----------------------------56values = exponential_model(initial, rate, time)57 58# Optional comparison model (used for bacteria, treatment, cleaning, etc.)59comparison = None60if scenarios[scenario] == "both":61 treatment_rate = st.sidebar.slider("Treatment / Cleaning Effectiveness", -3.0, -0.1, -1.0)62 comparison = exponential_model(initial, treatment_rate, time)63 64# -----------------------------65# Plot66# -----------------------------67fig, ax = plt.subplots()68 69ax.plot(time, values, label="Exponential Model", linewidth=2)70 71if comparison is not None:72 ax.plot(time, comparison, linestyle="--", label="With Treatment / Cleaning")73 74ax.set_xlabel("Time")75ax.set_ylabel("Quantity")76ax.set_title("Exponential Growth & Decay Model")77ax.legend()78ax.grid(True)79 80st.pyplot(fig)81 82# -----------------------------83# Data Table84# -----------------------------85data = pd.DataFrame({86 "Time": time,87 "Value": values88})89 90if comparison is not None:91 data["With Treatment"] = comparison92 93st.subheader("๐ Model Data")94st.dataframe(data.head(10))95 96# -----------------------------97# Export Section98# -----------------------------99st.subheader("โฌ๏ธ Export Results")100 101col1, col2 = st.columns(2)102 103with col1:104 csv = data.to_csv(index=False).encode("utf-8")105 st.download_button(106 label="Download Data (CSV)",107 data=csv,108 file_name="exponential_model_data.csv",109 mime="text/csv"110 )111 112with col2:113 img_buf = export_plot(fig)114 st.download_button(115 label="Download Graph (PNG)",116 data=img_buf,117 file_name="exponential_model_graph.png",118 mime="image/png"119 )120 121st.success("Adjust sliders to explore different real-world exponential scenarios!")