Monday, 10 March 2025

Python Program to plot Hermite Spline on the output screen

Copied!

#Program to plot Hermite Spline on the output screen
#Name and Roll

import numpy as np

import matplotlib.pyplot as plt

#User input for start and end points
x0,y0=map(float,input("Enter start point (x0 y0):").split())
x1,y1=map(float,input("Enter end point (x1 y1):").split())
tx0,ty0=map(float,input("Enter start tangent vector (tx0 ty0):").split())
tx1,ty1=map(float,input("Enter end tangent vector (tx1 ty1):").split())

#Generate parameter values
num_points=100
t=np.linspace(0,1,num_points)

#Compute Hermite basis functions/Blending Functions 
h00=(2*t**3)-(3*t**2)+1
h10=(t**3)-(2*t**2) + t
h01=(-2*t**3)+(3*t**2)
h11=t**3 - t**2    

#Compute x and y coordinates
x_curve = h00*x0 + h10*tx0 + h01*x1 + h11*tx1
y_curve = h00*y0 + h10*ty0 + h01*y1 + h11*ty1

#Plot the curve
plt.figure(figsize=(8,6))  
plt.plot(x_curve,y_curve,label="Cubic Hermite Curve")
plt.scatter([x0,x1],[y0,y1],color='red',label="Endpoints")
plt.quiver([x0,x1],[y0,y1],[tx0,tx1],[ty0,ty1],color='green',angles='xy',scale_units='xy',scale=1,label="Tangent Vectors")
plt.legend()
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Parametric Cubic Curve)")
plt.grid()
plt.show()

1 comment:


  1. #Experiment No 4:Program to solve a Second-order differential equation using SciKit and polt the result in Matplotlib

    #Name and Roll
    #Sahil Kirtane 27

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

    print("The general form of the Second-order linear differential equation is:")
    print("d^2y/dx^2 + p*dy/dx + q*y = r/n")

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

    # Define the differential equation
    def second_order_diff_eq(x, y):
    dydx = y[1]
    d2ydx2 = -p * y[1] - q * y[0] + r
    return [dydx, d2ydx2]

    # Initial condition
    initial_condition = [y0, dy0]

    # Interval of integration
    x_span = (0, 10)

    # Points where the solution is computed
    x_eval = np.linspace(0, 10, 100)

    # Solve the differential equation
    solution = solve_ivp(second_order_diff_eq, x_span, initial_condition, t_eval=x_eval)

    # Display the complete differential equation on the output screen
    equation_str = f"d^2y/dx^2 + {p}*dy/dx + {q}*y = {r}"
    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 Second-order Equation\n${equation_str}$')
    plt.legend()
    plt.grid(True)
    plt.show()

    ReplyDelete

Thanks for comment

Popular Posts