import%20marimo%0A%0A__generated_with%20%3D%20%220.9.14%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20__(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%23%20State%20estimation%20example%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20__()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20%23%20Simple%20Linear%20State%20Estimation%20Example%0A%20%20%20%20%23%203-Bus%20System%0A%20%20%20%20%23%20Interactive%20Notebook%20using%20Marimo%20with%20Adjustable%20Noise%20Variance%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%0A%20%20%20%20return%20mo%2C%20np%2C%20plt%2C%20sns%0A%0A%0A%40app.cell%0Adef%20__(mo)%3A%0A%20%20%20%20noise%20%3D%20mo.ui.slider(0%2C%2020%2C%20label%3Df%22Amount%20of%20noise%20%F0%9F%94%8A%3A%20%22)%0A%20%20%20%20noise%0A%20%20%20%20return%20(noise%2C)%0A%0A%0A%40app.cell%0Adef%20__(mo%2C%20noise)%3A%0A%20%20%20%20mo.md(%22%23%20%22%20%2B%20%22%F0%9F%94%8A%22%20*%20noise.value)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20__(noise)%3A%0A%20%20%20%20var%20%3D%20noise.value*0.0005%0A%20%20%20%20return%20(var%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20__(np%2C%20plt%2C%20var)%3A%0A%0A%20%20%20%20%23%20Measurement%20matrix%20H%20for%203%20measurements%3A%0A%20%20%20%20%23%20%201)%20P_12%20%3D%20theta1%20-%20theta2%0A%20%20%20%20%23%20%202)%20P_23%20%3D%20theta2%20-%20theta3%0A%20%20%20%20%23%20%203)%20P_inj2%20%3D%20(theta2%20-%20theta1)%20%2B%20(theta2%20-%20theta3)%0A%20%20%20%20H%20%3D%20np.array(%5B%0A%20%20%20%20%20%20%20%20%5B%201%2C%20-1%2C%20%200%5D%2C%20%20%20%23%20P_%7B12%7D%0A%20%20%20%20%20%20%20%20%5B%200%2C%20%201%2C%20-1%5D%2C%20%20%20%23%20P_%7B23%7D%0A%20%20%20%20%20%20%20%20%5B%200%2C%20%201%2C%20%200%5D%20%20%20%20%23%20Direct%20measurement%3A%20theta_2%0A%20%20%20%20%5D)%0A%0A%0A%0A%20%20%20%20%23%20True%20state%20vector%20(for%20demonstration%20purposes)%0A%20%20%20%20true_x%20%3D%20np.array(%5B0.1%2C%200.05%2C%200.0%5D)%20%20%23%20Voltage%20angles%20in%20radians%0A%0A%20%20%20%20%23%20Function%20to%20perform%20state%20estimation%0A%20%20%20%20def%20perform_estimation(noise_variance)%3A%0A%20%20%20%20%20%20%20%20np.random.seed(2025)%20%20%23%20For%20reproducibility%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Generate%20measurements%20with%20noise%0A%20%20%20%20%20%20%20%20P_12%20%3D%20true_x%5B0%5D%20-%20true_x%5B1%5D%20%2B%20np.random.normal(0%2C%20noise_variance)%0A%20%20%20%20%20%20%20%20P_23%20%3D%20true_x%5B1%5D%20-%20true_x%5B2%5D%20%2B%20np.random.normal(0%2C%20noise_variance)%0A%20%20%20%20%20%20%20%20theta2%20%3D%20true_x%5B1%5D%20%2B%20np.random.normal(0%2Cnoise_variance)%0A%0A%20%20%20%20%20%20%20%20z%20%3D%20np.array(%5BP_12%2C%20P_23%2C%20theta2%5D)%0A%0A%20%20%20%20%20%20%20%20%23%20Perform%20state%20estimation%0A%20%20%20%20%20%20%20%20H_T_H_inv%20%3D%20np.linalg.inv(H.T%20%40%20H)%0A%20%20%20%20%20%20%20%20estimated_x%20%3D%20H_T_H_inv%20%40%20(H.T%20%40%20z)%0A%0A%20%20%20%20%20%20%20%20%23%20Compute%20residuals%0A%20%20%20%20%20%20%20%20residuals%20%3D%20z%20-%20H%20%40%20estimated_x%0A%0A%20%20%20%20%20%20%20%20%23%20Plotting%0A%20%20%20%20%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%0A%20%20%20%20%20%20%20%20%23%20Plot%20measurements%0A%20%20%20%20%20%20%20%20plt.subplot(1%2C%202%2C%201)%0A%20%20%20%20%20%20%20%20plt.bar(%5B'P_12'%2C%20'P_23'%2C%20'theta2'%5D%2C%20z%2Clabel%3D'noisy')%0A%20%20%20%20%20%20%20%20plt.bar(%5B'P_12'%2C%20'P_23'%2C%20'theta2'%5D%2C%20H%20%40%20true_x%2C%20alpha%3D0.5%2Clabel%3D'noisy')%0A%20%20%20%20%20%20%20%20plt.title('Measurement')%0A%20%20%20%20%20%20%20%20plt.ylabel('(p.u.)')%0A%0A%20%20%20%20%20%20%20%20%23%20Plot%20residuals%0A%20%20%20%20%20%20%20%20plt.subplot(1%2C%202%2C%202)%0A%20%20%20%20%20%20%20%20plt.bar(%5B'theta1'%2C'theta2'%2C'theta3'%5D%2Ctrue_x)%0A%20%20%20%20%20%20%20%20plt.bar(%5B'theta1'%2C'theta2'%2C'theta3'%5D%2Cestimated_x%2Calpha%3D0.5%2Clabel%3D'estimated')%0A%20%20%20%20%20%20%20%20plt.title('States')%0A%20%20%20%20%20%20%20%20plt.ylabel('States%20(p.u.)')%0A%0A%20%20%20%20%20%20%20%20plt.tight_layout()%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20%20%20%20%20%23%20Display%20results%0A%20%20%20%20%20%20%20%20print(f%22True%20state%20vector%3A%20%7Btrue_x%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Estimated%20state%20vector%3A%20%7Bestimated_x%7D%22)%0A%20%20%20%20%20%20%20%20print(f%22Estimation%20Error%3A%20%7Btrue_x%20-%20estimated_x%7D%5Cn%22)%0A%0A%0A%20%20%20%20%23%20Interactive%20display%0A%20%20%20%20perform_estimation(var)%0A%0A%20%20%20%20return%20H%2C%20perform_estimation%2C%20true_x%0A%0A%0A%40app.cell%0Adef%20__(plt)%3A%0A%20%20%20%20def%20generate_3bus_pdf(filename%3D'three_bus_system.pdf')%3A%0A%20%20%20%20%20%20%20%20plt.figure(figsize%3D(4%2C2))%0A%0A%20%20%20%20%20%20%20%20%23%20Bus%201%0A%20%20%20%20%20%20%20%20plt.plot(0%2C%200%2C%20'ks'%2C%20ms%3D10)%0A%20%20%20%20%20%20%20%20plt.text(0%2C%200.3%2C%20'Bus%201'%2C%20ha%3D'center'%2C%20va%3D'bottom')%0A%0A%20%20%20%20%20%20%20%20%23%20Bus%202%0A%20%20%20%20%20%20%20%20plt.plot(2%2C%200%2C%20'ks'%2C%20ms%3D10)%0A%20%20%20%20%20%20%20%20plt.text(2%2C%200.3%2C%20'Bus%202'%2C%20ha%3D'center'%2C%20va%3D'bottom')%0A%0A%20%20%20%20%20%20%20%20%23%20Bus%203%0A%20%20%20%20%20%20%20%20plt.plot(4%2C%200%2C%20'ks'%2C%20ms%3D10)%0A%20%20%20%20%20%20%20%20plt.text(4%2C%200.3%2C%20'Bus%203'%2C%20ha%3D'center'%2C%20va%3D'bottom')%0A%0A%20%20%20%20%20%20%20%20%23%20Lines%0A%20%20%20%20%20%20%20%20plt.plot(%5B0%2C2%5D%2C%5B0%2C0%5D%2C%20'k-'%2C%20lw%3D2)%0A%20%20%20%20%20%20%20%20plt.text(1%2C%200.03%2C%20'Line%2012'%2C%20ha%3D'center'%2C%20va%3D'bottom')%0A%20%20%20%20%20%20%20%20plt.plot(%5B2%2C4%5D%2C%5B0%2C0%5D%2C%20'k-'%2C%20lw%3D2)%0A%20%20%20%20%20%20%20%20plt.text(3%2C%200.03%2C%20'Line%2023'%2C%20ha%3D'center'%2C%20va%3D'bottom')%0A%0A%20%20%20%20%20%20%20%20%23%20Adjust%20figure%0A%20%20%20%20%20%20%20%20plt.axis('equal')%0A%20%20%20%20%20%20%20%20plt.axis('off')%0A%20%20%20%20%20%20%20%20plt.xlim(-1%2C5)%0A%20%20%20%20%20%20%20%20plt.ylim(-1%2C1)%0A%0A%20%20%20%20%20%20%20%20%23%20Save%20to%20PDF%0A%20%20%20%20%20%20%20%20plt.savefig(filename%2C%20bbox_inches%3D'tight')%0A%20%20%20%20%20%20%20%20plt.show()%0A%20%20%20%20%20%20%20%20plt.close()%0A%20%20%20%20%20%20%20%20%23%20print(f%5C%22Saved%203-bus%20system%20diagram%20to%20%7Bfilename%7D%5C%22)%0A%0A%20%20%20%20%23%20Usage%20example%3A%0A%20%20%20%20generate_3bus_pdf()%0A%0A%20%20%20%20return%20(generate_3bus_pdf%2C)%0A%0A%0A%40app.cell%0Adef%20__()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
44154b3a1eee3ba4f55d3dbde21268830735b8578677bd9ef85da71b299de68e