Assignment of Electrical
Assignment of Electrical
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.interpolate import lagrange
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
1|Page
COMSATS University Islamabad (CUI), Wah Campus
Numerical Computations MTH375
Laboratory Work - 2024
method = method_var.get()
xp = np.linspace(min(x), max(x), 500) # Generate fine x points for smooth plot
try:
if method == "Linear":
interpolator = interp1d(x, y, kind="linear")
yp = interpolator(xp)
elif method == "Lagrange":
poly = lagrange(x, y)
yp = poly(xp)
else:
messagebox.showerror("Error", "Invalid interpolation method selected!")
return
2|Page
COMSATS University Islamabad (CUI), Wah Campus
Numerical Computations MTH375
Laboratory Work - 2024
ax.set_title(f'{method} Interpolation')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
canvas.draw()
except Exception as e:
messagebox.showerror("Error", f"Failed to interpolate: {e}")
3|Page
COMSATS University Islamabad (CUI), Wah Campus
Numerical Computations MTH375
Laboratory Work - 2024
# Plot Button
plot_button = tk.Button(frame, text="Plot", command=plot_interpolation)
plot_button.grid(row=0, column=2, padx=5)
# Justification Button
justify_button = tk.Button(frame, text="Justify Method", command=justify_method)
justify_button.grid(row=0, column=3, padx=5)
# Matplotlib Figure
fig, ax = plt.subplots(figsize=(6, 4))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas_widget = canvas.get_tk_widget()
4|Page
COMSATS University Islamabad (CUI), Wah Campus
Numerical Computations MTH375
Laboratory Work - 2024
canvas_widget.pack()
5|Page