from matplotlib import pyplot as plt
import numpy as np

N = 16

Rs = np.arange(0,1600,100)
print(Rs)

Us = np.array([
    0.1, 0.0, 1.7, 2.6, 3.4, 4.0, 4.6, 5.0, 5.4, 5.7, 6.0, 6.3, 6.5, 6.7, 6.9, 6.9
])
Is = np.array([
    .015, .015, .012, .011, .010, .009, .008, .007, .007, .006, .006, .005, .005, .005, .005, .005
])
len(Us), len(Is)

plt.grid()
plt.scatter(Is, Us, marker="d", color="brown", s=6)
plt.xlabel("$I$")
plt.ylabel("U(I)")

coeffs_U = np.polyfit(Is, Us, 1)  # линейная аппроксимация
approx_Is = np.linspace(min(Is), max(Is), 100)
approx_Us = np.polyval(coeffs_U, approx_Is)
plt.plot(approx_Is, approx_Us, '--', color="black", label="Аппроксимация")
plt.legend()


# plt.title("График зависимости U(I)")
# plt.show()
plt.savefig("1.png", bbox_inches="tight", dpi=300)
plt.show()


r, Epsilon = np.polyfit(Is, Us, 1)
r *= -1
print(f"r = {r}\tEpsilon = {Epsilon}")

P = Epsilon*Is
Pr = Us*Is
Ps = Is*Is*r

arr = (np.vstack((Us, Is, Pr, Ps, P))).T
np.set_printoptions(precision=3)
print(
    arr
)

arr[:, 1:] *= 1000
for i in range(arr.shape[0]):
    for j in range(arr.shape[1]):
        print(f"[{arr[i, j]: .3f}]", end=', ')
    print()


plt.scatter(Is, Pr, s=5, color="purple", marker="d", label="$P_R=P_R(I)$")
plt.scatter(Is, Ps, s=5, color="red", marker="s", label="$P_S=P_S(I)$")
plt.scatter(Is, P, s=5, color="blue", marker="o", label="$P=P(I)$")
plt.legend()

coeffs_Pr = np.polyfit(Is, Pr, 2)  # квадратичная аппроксимация
approx_Pr = np.polyval(coeffs_Pr, approx_Is)
plt.plot(approx_Is, approx_Pr, '--', color="purple", alpha=0.3, label="Аппроксимация $P_R(I)$")
plt.legend()


# plt.title("Графики зависимости мощностей $P, P_R, P_S$ от силы тока")
plt.xlabel("$I$")

plt.grid()
# plt.show()
plt.savefig("2.png", bbox_inches="tight", dpi=300)
plt.show()

I_star = Epsilon/(2*r)
print(f"I* = {I_star:.4f}")

i = np.polyfit(Is, Pr, 2)
approx_Is = np.linspace(min(Is), max(Is))
approx = np.polyval(i, approx_Is)


plt.scatter(Is, Pr, s=5, color="purple", marker="d", label="$P_R=P_R(I)$")
plt.plot(approx_Is, approx, color="purple", alpha=.2, linestyle='--', label="Аппроксимация $P_r(I)$")
plt.grid()


plt.axvline([I_star], color="grey", linestyle='--', linewidth=1, alpha=.5, label="$X=I^*=0.0075$")
plt.legend()

# plt.show()
plt.savefig("3.png", bbox_inches="tight", dpi=300)
plt.show()

i = np.polyfit(Is, Pr, 2)
P_Rmax = np.polyval(i, I_star)
print(f"P_Rmax = {P_Rmax:.3f}")

plt.scatter(Is, Pr)
# plt.plot(np.linspace(Is[0], Is[-1]), )

R =  P_Rmax / I_star**2
print(f"R = {R:.3f}")

plt.show()

eta = Pr / P

print(eta)

plt.scatter(Is, eta, s=5, color="blue", label="$\eta=\eta(I)$")
plt.xlabel("$I$")
plt.ylabel("$\eta (I)$")
# plt.title("Значения КПД")

plt.axhline([0.5], label="$\eta=0.5$", color="grey", linestyle="--", alpha=.5)

plt.xlim(0, 0.017)
# plt.ylim(0, 0.7)

approx_x = np.linspace(0, 0.0155)
i = np.polyfit(Is, eta, 1)
approx = np.polyval(i, approx_x)

plt.plot(approx_x, approx, color="blue", linestyle='--', alpha=.2, label="Аппроксимация $\eta=\eta (I)$")


plt.legend()
plt.grid()

# plt.show()
plt.savefig("4.png", bbox_inches="tight", dpi=300)
plt.show()

eta = np.reshape(eta, (16, 1))
eta 

arr

arr = np.hstack((arr, eta))

for i in range(arr.shape[0]):
    for j in range(arr.shape[1]):
        print(f"[{arr[i, j]: .3f}]", end=', ')
    print()
