import numpy as np import matplotlib.pyplot as plt import random import math plt.style.use('ggplot') plt.figure(figsize=(20,10)) N=50000 # number of steps T=0.5 # simulation time in seconds c = 2200 * 10**(-6) # v = 5 i= 5 * 10 ** (-3) r = 0.08 * 10**3 t = np.empty(N) Vc = np.empty(N) Vi = np.empty(N) Ic = np.empty(N) dt=T/N; # initial values Vc[0]=0 Ic[0]=i t[0]=0 Is=10**(-12) Vi[0]=0 for k in range(0,N-1): t[k+1]=t[k]+dt #Vi[k+1]=20*np.sin(2*math.pi*50*t[k]) # sine Vi[k+1]=20*np.sin(2*math.pi*50*t[k])*(1+0.5*np.sin(10*t[k])) #sine with variable amplitude Vc[k+1]=Vc[k]-dt*Vc[k]/(r*c)+Is*dt/c*(np.exp(40*(Vi[k]-Vc[k]))-1) plt.plot(t,Vc,label='Capacitor Voltage') plt.plot(t,Vi,label='Input Voltage') plt.xlabel('Time (s)') plt.title('Peak detector circuit') plt.legend() plt.show()