Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SciProg2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cprutean
SciProg2024
Commits
0d3dacfd
Commit
0d3dacfd
authored
9 months ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Update file ImpedancePlot.ipynb
parent
55224910
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/ImpedancePlot.ipynb
+40
-44
40 additions, 44 deletions
CodeExamples/ImpedancePlot.ipynb
with
40 additions
and
44 deletions
CodeExamples/ImpedancePlot.ipynb
+
40
−
44
View file @
0d3dacfd
...
...
@@ -18,7 +18,7 @@
"\"\"\"\n",
" Example program to plot out the current and phase for a LRC circuit in two plots\n",
" \n",
" Physics student will see the theory in Physics of Fields next semester.\n",
" Physics student
s
will see the theory in Physics of Fields next semester.\n",
" \n",
" Try values of:\n",
" Resistor = 0.1\n",
...
...
@@ -31,61 +31,57 @@
"import math\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Define calculation of impedance\n",
"def impedance(resistor,capacitor,inductor,omega):\n",
" \"\"\"\n",
" Calculate the complex impedance of a LRC circuit using complex numbers and return\n",
" as a complex\n",
" \"\"\"\n",
" return resistor + complex(0,-1.0/(capacitor*omega)) + complex(0,inductor*omega)\n",
" \n",
"def main():\n",
" \n",
"# Ask for the three values\n",
"resistor = float(input(\"Resistor : \"))\n",
"capacitor = float(input(\"Capicitor : \"))\n",
"inductor = float(input(\"Inductor : \"))\n",
" \n",
"
# Ask for the three values
\n",
"
resistor = float(input(\"Resistor : \")
)\n",
"
capacitor = float(input(\"C
ap
i
citor
: \"))
\n",
"
inductor = float(input(\"Inductor : \"
))\n",
"
# Calculate resonance and quality factors for the circuit and print them out.
\n",
"
resonance = 1.0/math.sqrt(capacitor*inductor
)\n",
"
quality = math.sqrt(inductor/c
ap
a
citor
)/resistor
\n",
"
print(\"Resonance \" + str(resonance) + \" Quality : \" + str(quality
))\n",
" \n",
" # Calcuate resonance and quality factors for the circuit and print them out.\n",
" resonance = 1.0/math.sqrt(capacitor*inductor)\n",
" quality = math.sqrt(inductor/capacitor)/resistor\n",
" print(\"Resonance \" + str(resonance) + \" Quality : \" + str(quality))\n",
" \n",
" # Calcualte the range of the plot with resonance freq at the centre.\n",
" npoints = 300\n",
" omegaMin = 0.5*resonance \n",
" omegaMax = 1.5*resonance\n",
" domega = (omegaMax - omegaMin)/npoints # Separations betewen points \n",
"# Calculate the range of the plot with resonance freq at the centre.\n",
"npoints = 300\n",
"omegaMin = 0.5*resonance \n",
"omegaMax = 1.5*resonance\n",
"domega = (omegaMax - omegaMin)/npoints # Separations betewen points \n",
"\n",
"
#
Thee empty lists to hold the data to be plotted\n",
"
omegaData = [] \n",
"
modData = []\n",
"
phaseData = []\n",
"
#
Th
r
ee empty lists to hold the data to be plotted\n",
"omegaData = [] \n",
"modData = []\n",
"phaseData = []\n",
"\n",
"
#
Fill the three data lists\n",
"
for i in range(0,npoints + 1):\n",
"
omega = omegaMin + i*domega # calcuate omega\n",
"
omegaData.append(omega) # store in list\n",
"
#
Get complex impedance\n",
"
z = impedance(resistor,capacitor,inductor,omega)\n",
"
modData.append(1/abs(z)) # Store Current = 1/abs(z)\n",
"
phaseData.append(cmath.phase(z)) # Store Phase\n",
"
#
Fill the three data lists\n",
"for i in range(0,npoints + 1):\n",
" omega = omegaMin + i*domega # calcu
l
ate omega\n",
" omegaData.append(omega) # store in list\n",
"
#
Get complex impedance\n",
" z = impedance(resistor,capacitor,inductor,omega)\n",
" modData.append(1/abs(z)) # Store Current = 1/abs(z)\n",
" phaseData.append(cmath.phase(z)) # Store Phase\n",
" \n",
" \n",
" # To the plotting\n",
" plt.subplot(2,1,1) # Make upper subplot\n",
" plt.plot(omegaData,modData) # Plot in upper graph\n",
" plt.title(\"LRC with resonance: {0:5.3f} quality: {1:5.3f}\".format(resonance,quality))\n",
" plt.ylabel(\"Current\")\n",
" plt.grid()\n",
" plt.subplot(2,1,2) # Make lower subplot\n",
" plt.plot(omegaData,phaseData) # Plot in lower graph\n",
" plt.xlabel(\"Angular Frequency\")\n",
" plt.ylabel(\"Phase\")\n",
" plt.grid()\n",
" plt.show() # Show the two plots\n",
" \n",
" \n",
"main()"
"# To the plotting\n",
"plt.subplot(2,1,1) # Make upper subplot\n",
"plt.plot(omegaData,modData) # Plot in upper graph\n",
"plt.title(\"LRC with resonance: {0:5.3f} quality: {1:5.3f}\".format(resonance,quality))\n",
"plt.ylabel(\"Current\")\n",
"plt.grid()\n",
"plt.subplot(2,1,2) # Make lower subplot\n",
"plt.plot(omegaData,phaseData) # Plot in lower graph\n",
"plt.xlabel(\"Angular Frequency\")\n",
"plt.ylabel(\"Phase\")\n",
"plt.grid()\n",
"plt.show() # Show the two plots"
]
}
],
...
...
%% Cell type:markdown id:6838b2de tags:
# Hello
%% Cell type:code id:4200d243 tags:
```
python
"""
Example program to plot out the current and phase for a LRC circuit in two plots
Physics student will see the theory in Physics of Fields next semester.
Physics student
s
will see the theory in Physics of Fields next semester.
Try values of:
Resistor = 0.1
Capacitor = 0.001
Inductor = 0.02
"""
import
cmath
import
math
import
matplotlib.pyplot
as
plt
# Define calculation of impedance
def
impedance
(
resistor
,
capacitor
,
inductor
,
omega
):
"""
Calculate the complex impedance of a LRC circuit using complex numbers and return
as a complex
"""
return
resistor
+
complex
(
0
,
-
1.0
/
(
capacitor
*
omega
))
+
complex
(
0
,
inductor
*
omega
)
def
main
():
# Ask for the three values
resistor
=
float
(
input
(
"
Resistor :
"
))
capacitor
=
float
(
input
(
"
Capicitor :
"
))
inductor
=
float
(
input
(
"
Inductor :
"
))
# Calcuate resonance and quality factors for the circuit and print them out.
resonance
=
1.0
/
math
.
sqrt
(
capacitor
*
inductor
)
quality
=
math
.
sqrt
(
inductor
/
capacitor
)
/
resistor
print
(
"
Resonance
"
+
str
(
resonance
)
+
"
Quality :
"
+
str
(
quality
))
# Calcualte the range of the plot with resonance freq at the centre.
npoints
=
300
omegaMin
=
0.5
*
resonance
omegaMax
=
1.5
*
resonance
domega
=
(
omegaMax
-
omegaMin
)
/
npoints
# Separations betewen points
# Thee empty lists to hold the data to be plotted
omegaData
=
[]
modData
=
[]
phaseData
=
[]
# Fill the three data lists
for
i
in
range
(
0
,
npoints
+
1
):
omega
=
omegaMin
+
i
*
domega
# calcuate omega
omegaData
.
append
(
omega
)
# store in list
# Get complex impedance
z
=
impedance
(
resistor
,
capacitor
,
inductor
,
omega
)
modData
.
append
(
1
/
abs
(
z
))
# Store Current = 1/abs(z)
phaseData
.
append
(
cmath
.
phase
(
z
))
# Store Phase
# To the plotting
plt
.
subplot
(
2
,
1
,
1
)
# Make upper subplot
plt
.
plot
(
omegaData
,
modData
)
# Plot in upper graph
plt
.
title
(
"
LRC with resonance: {0:5.3f} quality: {1:5.3f}
"
.
format
(
resonance
,
quality
))
plt
.
ylabel
(
"
Current
"
)
plt
.
grid
()
plt
.
subplot
(
2
,
1
,
2
)
# Make lower subplot
plt
.
plot
(
omegaData
,
phaseData
)
# Plot in lower graph
plt
.
xlabel
(
"
Angular Frequency
"
)
plt
.
ylabel
(
"
Phase
"
)
plt
.
grid
()
plt
.
show
()
# Show the two plots
main
()
# Ask for the three values
resistor
=
float
(
input
(
"
Resistor :
"
))
capacitor
=
float
(
input
(
"
Capicitor :
"
))
inductor
=
float
(
input
(
"
Inductor :
"
))
# Calculate resonance and quality factors for the circuit and print them out.
resonance
=
1.0
/
math
.
sqrt
(
capacitor
*
inductor
)
quality
=
math
.
sqrt
(
inductor
/
capacitor
)
/
resistor
print
(
"
Resonance
"
+
str
(
resonance
)
+
"
Quality :
"
+
str
(
quality
))
# Calculate the range of the plot with resonance freq at the centre.
npoints
=
300
omegaMin
=
0.5
*
resonance
omegaMax
=
1.5
*
resonance
domega
=
(
omegaMax
-
omegaMin
)
/
npoints
# Separations betewen points
# Three empty lists to hold the data to be plotted
omegaData
=
[]
modData
=
[]
phaseData
=
[]
# Fill the three data lists
for
i
in
range
(
0
,
npoints
+
1
):
omega
=
omegaMin
+
i
*
domega
# calculate omega
omegaData
.
append
(
omega
)
# store in list
# Get complex impedance
z
=
impedance
(
resistor
,
capacitor
,
inductor
,
omega
)
modData
.
append
(
1
/
abs
(
z
))
# Store Current = 1/abs(z)
phaseData
.
append
(
cmath
.
phase
(
z
))
# Store Phase
# To the plotting
plt
.
subplot
(
2
,
1
,
1
)
# Make upper subplot
plt
.
plot
(
omegaData
,
modData
)
# Plot in upper graph
plt
.
title
(
"
LRC with resonance: {0:5.3f} quality: {1:5.3f}
"
.
format
(
resonance
,
quality
))
plt
.
ylabel
(
"
Current
"
)
plt
.
grid
()
plt
.
subplot
(
2
,
1
,
2
)
# Make lower subplot
plt
.
plot
(
omegaData
,
phaseData
)
# Plot in lower graph
plt
.
xlabel
(
"
Angular Frequency
"
)
plt
.
ylabel
(
"
Phase
"
)
plt
.
grid
()
plt
.
show
()
# Show the two plots
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment