Question: This code is python progaming code about solve Kirchoff's law about diffrent way of time scheme. I think this code will be plot a grapgh
This code is python progaming code about solve "Kirchoff's law" about diffrent way of time scheme. I think this code will be plot a grapgh seems like cosine function. But those code ara not same as cosine function, and It's converging at zero. Can you fix the code and it seems like a cosine function?
import numpy as np
import matplotlib.pyplot as plt
# Constants
V # Voltage V
LC # InductanceCapacitance product FH
deltat # Time step s
numsteps # Number of time steps
# Initialize arrays
time npzerosnumsteps
voltagevalues
'CrankNicolson': npzerosnumsteps
'RungeKutta': npzerosnumsteps
'EulerExplicit': npzerosnumsteps
# Initial condition
voltagevaluesCrankNicolson' V
voltagevaluesRungeKutta' V
voltagevaluesEulerExplicit' V
# Numerical schemes
def cranknicolsonprevv LC deltat:
return prevv V deltat LC deltat LC
def rungekuttaprevv LC deltat:
kprevv LC
kprevv k deltat LC
kprevv k deltat LC
kprevv k deltat LC
return prevv deltat kkk k
# EulerExplicit method is straightforward
for t in range numsteps:
timet t deltat
voltagevaluesCrankNicolson't cranknicolsonvoltagevaluesCrankNicolson't LC deltat
voltagevaluesRungeKutta't rungekuttavoltagevaluesRungeKutta't LC deltat
voltagevaluesEulerExplicit't voltagevaluesEulerExplicit'tvoltagevaluesEulerExplicit't LC deltat
# Plotting
pltfigurefigsize
for scheme, values in voltagevalues.items:
pltplottime values, labelscheme
pltxlabelTime s
pltylabelVoltage V
plttitleVoltageTime for Different Numerical Schemes'
pltlegend
pltgridTrue
pltshow
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
