convert into python this matlab script: clear all; close all; clc; %% Given values R= 2…
- User
convert into python this matlab script: clear all; close all; clc; %% Given values R= 2 ; % given resistance C = 1/5 ; % given capacitance f = 5/pi ; % given frequency omega = 2 * pi * f ; % omega = 10 given omega Iq = 2exp(jdeg2rad(30)) ; % Current expressed as Phasor 2 effective value 30° angle respect the real axis
%% determination of the Inductance L so tthat the supply voltage Uq %% anticipates the current Iq of 45° degrees syms L Zl_abs = j * omega * L; Zc_abs = -j /(omega * C) f = ( j*2 == 1/(1/Zl_abs + 1/Zc_abs) ) L = solve(f) L = double(L)
%% Here the calculation of the Voltage Uq Zl =j * omega * L; Zc = -j /(omega * C) Zp = 1/(1/Zl+1/Zc) Ztot = R + Zp
Uq = Ztot * Iq Ur = R * Iq Up = Zp * Iq
Il = Up/Zl Ic = Up/Zc
- Assistant
import numpy as np from sympy import *
Given values
R = 2 # given resistance C = 1 / 5 # given capacitance f = 5 / np.pi # given frequency omega = 2 * np.pi * f # omega = 10 given omega Iq = 2 * np.exp(1j * np.deg2rad(30)) # Current expressed as Phasor 2 effective value 30° angle respect the real axis
determination of the Inductance L so that the supply voltage Uq
anticipates the current Iq of 45° degrees
L = Symbol('L') Zl_abs = 1j * omega * L Zc_abs = -1j / (omega * C) f = (1j * 2 == 1 / (1 / Zl_abs + 1 / Zc_abs)) L = solve(f) L = [complex(l) for l in L] L = np.real(L[0]) # double--> just the real part of the complex number
Here the calculation of the Voltage Uq
Zl = 1j * omega * L Zc = -1j / (omega * C) Zp = 1 / (1 / Zl + 1 / Zc) Ztot = R + Zp
Uq = Ztot * Iq Ur = R * Iq Up = Zp * Iq
Il = Up / Zl Ic = Up / Zc
print('L:', L) print('Zl:', Zl) print('Zc:', Zc) print('Zp:', Zp) print('Ztot:', Ztot) print('Uq:', Uq) print('Ur:', Ur) print('Up:', Up) print('Il:', Il) print('Ic:', Ic)
- User
continue plotting the results :
% phasor diagram % Uq line([0 real(Uq)],[0 imag(Uq)],'marker','>'); text(1.05real(Uq),1.05imag(Uq),'Uq'); % Iq line([0 real(Iq)],[0 imag(Iq)],'marker','>','color','red'); text(1.05real(Iq),1.05imag(Iq),'Iq','color','red');
% Ur line([0 real(Ur)],[0 imag(Ur)],'marker','>','color','green'); text(1.025real(Ur),1.025imag(Ur),'Ur'); % Up line([real(Ur) real(Ur)+real(Up)],[imag(Ur) imag(Ur)+imag(Up)],'marker','>','color','magenta'); text(0.9*(real(Ur)+real(Up)),0.9*(imag(Ur)+imag(Up)),'Up','color','magenta'); maxi = max ([abs(Uq) abs(Iq) abs(Ur) abs(Up) ])
axis([-maxi/4 maxi -maxi/4 maxi]); title('Phasor Diagram') xlabel('Re'); ylabel('Im'); grid on; axis square;
- Assistant
import matplotlib.pyplot as plt
phasor diagram
fig, ax = plt.subplots()
Uq
ax.arrow(0, 0, np.real(Uq), np.imag(Uq), head_width=0.05, head_length=0.1, ec='blue', fc='blue') ax.text(1.05 * np.real(Uq), 1.05 * np.imag(Uq), 'Uq', color='blue')
Iq
ax.arrow(0, 0, np.real(Iq), np.imag(Iq), head_width=0.05, head_length=0.1, ec='red', fc='red') ax.text(1.05 * np.real(Iq), 1.05 * np.imag(Iq), 'Iq', color='red')
Ur
ax.arrow(0, 0, np.real(Ur), np.imag(Ur), head_width=0.05, head_length=0.1, ec='green', fc='green') ax.text(1.025 * np.real(Ur), 1.025 * np.imag(Ur), 'Ur', color='green')
Up
ax.arrow(np.real(Ur), np.imag(Ur), np.real(Up), np.imag(Up), head_width=0.05, head_length=0.1, ec='magenta', fc='magenta') ax.text(0.9 * (np.real(Ur) + np.real(Up)), 0.9 * (np.imag(Ur) + np.imag(Up)), 'Up', color='magenta')
maxi = max([abs(Uq), abs(Iq), abs(Ur), abs(Up)])
ax.axis([-maxi / 4, maxi, -maxi / 4, maxi]) ax.set_title('Phasor Diagram') ax.set_xlabel('Re') ax.set_ylabel('Im') ax.grid(True) ax.set_aspect('equal', adjustable='box')
plt.show()
- User
'Mul' object has no attribute 'hypot'
- Assistant
It seems that I've incorrectly used the
maxfunction with complex numbers. Let's fix this by finding the maximum magnitude (absvalue) of the complex numbers in the list.Replace the line:
maxi = max([abs(Uq), abs(Iq), abs(Ur), abs(Up)])with:
maxi = max([np.abs(Uq), np.abs(Iq), np.abs(Ur), np.abs(Up)])Here's the updated plotting script:
import matplotlib.pyplot as plt # phasor diagram fig, ax = plt.subplots() # Uq ax.arrow(0, 0, np.real(Uq), np.imag(Uq), head_width=0.05, head_length=0.1, ec='blue', fc='blue') ax.text(1.05 * np.real(Uq), 1.05 * np.imag(Uq), 'Uq', color='blue') # Iq ax.arrow(0, 0, np.real(Iq), np.imag(Iq), head_width=0.05, head_length=0.1, ec='red', fc='red') ax.text(1.05 * np.real(Iq), 1.05 * np.imag(Iq), 'Iq', color='red') # Ur ax.arrow(0, 0, np.real(Ur), np.imag(Ur), head_width=0.05, head_length=0.1, ec='green', fc='green') ax.text(1.025 * np.real(Ur), 1.025 * np.imag(Ur), 'Ur', color='green') # Up ax.arrow(np.real(Ur), np.imag(Ur), np.real(Up), np.imag(Up), head_width=0.05, head_length=0.1, ec='magenta', fc='magenta') ax.text(0.9 * (np.real(Ur) + np.real(Up)), 0.9 * (np.imag(Ur) + np.imag(Up)), 'Up', color='magenta') maxi = max([np.abs(Uq), np.abs(Iq), np.abs(Ur), np.abs(Up)]) ax.axis([-maxi / 4, maxi, -maxi / 4, maxi]) ax.set_title('Phasor Diagram') ax.set_xlabel('Re') ax.set_ylabel('Im') ax.grid(True) ax.set_aspect('equal', adjustable='box') plt.show()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.