import numpy as np import matplotlib.pyplot as plt def stress_analysis(force, die_width, die_height, material_strength, temperature_change=50, alpha=1e-5, elastic_modulus=200000, fatigue_strength=150): try: stress = force / (die_width * die_height) safety_factor = material_strength / stress thermal_stress = elastic_modulus * alpha * temperature_change fatigue_stress = fatigue_strength x = np.linspace(1, 100, 100) stress_curve = stress * x / 100 material_strength_curve = np.full_like(x, material_strength) safety_factor_curve = material_strength_curve / stress_curve thermal_stress_curve = np.full_like(x, thermal_stress) fatigue_strength_curve = np.full_like(x, fatigue_stress) fig, ax = plt.subplots(figsize=(10, 6)) ax.plot(x, stress_curve, label="Stress (σ)", color="blue") ax.plot(x, material_strength_curve, label="Material Strength (σ_y)", color="green") ax.plot(x, safety_factor_curve, label="Safety Factor (SF)", color="orange") ax.plot(x, thermal_stress_curve, label="Thermal Stress (σ_thermal)", color="purple") ax.plot(x, fatigue_strength_curve, label="Fatigue Strength (σ_fatigue)", color="brown") ax.axhline(1, color="red", linestyle="--", label="Critical Safety Threshold (SF=1)") ax.set_title("Combined Stress Analysis Parameters") ax.set_xlabel("Operational Range (%)") ax.set_ylabel("Parameter Value (MPa or Unitless)") ax.legend() ax.grid() plt.tight_layout() plt.close(fig) return f"Safety Factor: {round(safety_factor, 2)}", fig except Exception as e: return f"Error in stress analysis: {str(e)}", None