import random from matplotlib import pyplot as plot import numpy as np def calc_p(U0, Ri, Rl): I = U0 / (Ri + Rl) Ul = I * Rl P = Ul * I return P U0 = 10 Ri = 3.14 Rl_min = 0.1 Rl_max = 10. Rl_step_start = 1 Rl_step_stop = 0.01 Rl = np.linspace(Rl_min, Rl_max, 100, dtype=float) # -------------------------------------------------- # MPPT search Rl_s = np.random.uniform()*(Rl_max - Rl_min) + Rl_min Rl_step = Rl_step_start dir = "inc" dir_last = dir while Rl_step > Rl_step_stop: p0 = calc_p(U0, Ri, Rl_s) p1 = p0 if "inc" in dir: Rl_s = min(Rl_max, Rl_s + Rl_step) p1 = calc_p(U0, Ri, Rl_s) if p0 > p1: if 'inc' in dir_last: Rl_step /= 2 dir = 'dec' elif "dec" in dir: Rl_s = max(Rl_min, Rl_s - Rl_step) p1 = calc_p(U0, Ri, Rl_s) if p0 > p1: if 'dec' in dir_last: Rl_step /= 2 dir = 'inc' dir_last = dir print(f"p1 = {p1}, Rl_s = {Rl_s}, Rl_step {Rl_step}") # -------------------------------------------------- P = calc_p(U0, Ri, Rl) plot.plot(Rl, P) plot.ylabel("Power [W]") plot.xlabel("RL [Ohms]") plot.title("Power vs RL") plot.legend("RL [Ohms]") plot.grid() plot.show()