Program description:
You are given a set of two functions: $$f=x^3-6x^2+x+5; g=(x-2)^2-6$$ Plot them using Matprolib on a user input segment [a; b].
My solution:
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interpolate # for smoothing
def func(x):
return [pow(x, 3) - 6 * pow(x, 2) + x + 5, pow((x-2), 2) - 6]
# plt.plot([1,5, -3, 0.5], [1, 25, 9, 0.5])
# plt.plot(1, 7, "r+")
# plt.plot(-1, 7, "bo")
f = []
f_1 = []
x = []
interval = [int(x) for x in input("Define segment (i.e. a b): ").split(' ')]
for val in range(interval[0], interval[1] + 1):
x.append(val)
f.append(func(val)[0])
f_1.append(func(val)[1])
linear_space = np.linspace(interval[0], interval[1], 300)
a_BSpline = interpolate.make_interp_spline(x, f)
b_BSpline = interpolate.make_interp_spline(x, f_1)
f_new = a_BSpline(linear_space)
f_1_new = b_BSpline(linear_space)
plt.plot(linear_space, f_new, color="#47c984",
linestyle="solid", linewidth=1)
plt.plot(linear_space, f_1_new, color="#fc6703",
linestyle="solid", linewidth=1)
plt.gca().spines["left"].set_position("zero")
plt.gca().spines["bottom"].set_position("zero")
plt.show()
Input: -10 10
Output:
Question: Is there any way to make this code more concise?
Thank you in advance.
interpolate.make_interp_spline
? If it is required, please update the problem statement and title to include the corresponding logic. \$\endgroup\$