Monday, 10 March 2025

Python Program to solve a linear differential equation using SciKit and polt the result in Matplotlib

Copied!

#Experiment No 4:Program to solve a linear differential equation using SciKit and polt the result in Matplotlib

#Name and Roll

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

print("The general form of the linear differential equation is:")
print("dy/dx+p(x)*y=q(x)\n")

#Get user input for the differential equation coefficients
p = float(input("Enter the coefficient for y (p): "))
q = float(input("Enter the constant term (q): "))
y0 = float(input("Enter the initial condition y(0): "))

#Define the differential equation
def linear_diff_eq(x,y):
    dydx=-p*y+q
    return dydx

#Interval of integration
x_span = (0,10)
#points where the soultion is computed
x_eval = np.linspace(0,10,100)

#Solve the differential equation
solution = solve_ivp(linear_diff_eq,x_span,[y0],t_eval=x_eval)

#Display the complete differential equation on the output screen
equation_str=f"dy/dx+{p}*y={q}"

print(f"The complete differential equation is:{equation_str}")

# Plot the result
plt.plot(solution.t,solution.y[0],label='y(x)')
plt.xlabel('x')
plt.ylabel('y')
plt.title(f'Solution of Linear Differential Equation\n{equation_str}')
plt.legend()
plt.grid(True)
plt.show()


1 comment:

  1. Calculate max deflection at free end

    delta_max=-1(5*w*L**4)/(384*E*1)

    print(f"Maximum deflection at free end: (delta_max 1000:.4f) mm²)

    Plot deflection curve

    plt.figure(figsize=(8,6))

    plt.plot(x* 1000, deflection 1000, label="Beam Deflection", color="b")

    plt.xlabel("Length of Beam (mm)")

    plt.ylabel("Deflection (mm)")

    plt.title("Cantilever Beam Deflection Due to Self-Weight")

    plt.legend()

    plt.grid(True)

    plt.show()

    ReplyDelete

Thanks for comment

Popular Posts