Advertisement
Guest User

Simple Circuit Analysis

a guest
Jun 29th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pyswarms as ps
  4.  
  5.  
  6. # Define voltage functions
  7. def resistance_voltage(current, resistance):
  8.     u_R = current/resistance
  9.     return u_R
  10.  
  11. def diode_voltage(current):
  12.     temp_volt = 25.6*10**(-3)
  13.     rev_current = 9.4*10**(-15)
  14.     u_D = temp_volt * np.log(current/rev_current)
  15.     return u_D
  16.  
  17.  
  18. # Define optimization parameters and cost function
  19. options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
  20. bounds = (np.array([0]), np.array([50]))
  21.  
  22. def cost_function(X):
  23.     n_particles = X.shape[0]
  24.     target_voltage = 5
  25.     resistance = 10
  26.     cost = np.array([
  27.             target_voltage
  28.             - resistance_voltage(X[i], resistance)
  29.             - diode_voltage(X[i])
  30.             for i in range(n_particles)])
  31.     print(cost)
  32.     return cost
  33.  
  34. optimizer = ps.single.GlobalBestPSO(n_particles=8, dimensions=1, options=options, bounds=bounds)
  35.  
  36. cost, curr = optimizer.optimize(cost_function, print_step=100, iters=1000, verbose=3)
  37.  
  38. print(cost, curr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement