November 13th, 2013, 08:26 PM

How do I optimize this in Python? help needed very much!!
I am attempting to optimize current flow in a circuit by optimizing the sets of resistors. By setting the voltages to a constant, I used the solver function to solve for the 8 currents. Now I need to create a function to optimize the currents.
from pylab import *
from scipy.optimize import *
R1 = 2; R2 = 2; R3 = 2; R4 = 2; R5 = 2; R6 = 2; R7 = 2
Va = 1.5; Vb = 1.5; Vc = 1.5
# each equation must introduce something new
leftSide = array([ [1, 0, 0, 0 , 0, 1, 1, 0], [R1, 0, R3, 0, R5, R6, 0, 0], [1, 0, 0, 1 , 1, 0, 0, 0], [0, 0, 0, R4, R5, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 1], [0, R2, R3, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, R6, R7, 0] ])
rightSide = array([0, Va, 0, Vb, 0, Vb, 0, Vc])
results1 = solve(leftSide, rightSide)
print results1
Vi = 1.5
R = 0.5
# Defines a function (to be optimized)
def R():
R = rightSide*results1
return f
# Plot the function in 2D space
vals = linspace(5, 5, 101)
xGrid, yGrid = meshgrid(vals, vals)
zGrid = f([xGrid,yGrid], x0, y0, w)
tickValues = linspace(1, 0, 50)
contourf(xGrid, yGrid, zGrid, levels=tickValues)
colorbar()
show()
# Now MINIMIZE this function...
initialVals = [0, 0]
params = [1.5, 0.5, 2]
results = minimize(f, initialVals, args=params, method='Anneal')
print "RESULTS OF MINIMIZATION:", results
Line 12 and above is correct. The rest is an example my teacher gave, but I do not know how to implement it into the assignment i am doing. Help needed very much please!
Comments on this post
December 6th, 2013, 10:27 AM

[code]
Code tags[/code] are essential for python code and Makefiles!
December 6th, 2013, 10:56 AM

1) draw the circuit.
2) define the problem.
optimize a function of no parameters. A function with no dependencies is constant.
Code:
# Defines a function (to be optimized)
def R():
# modify impedance matrix based on inputs to R
# recompute the currents
# make a scalar from some combination of the currents
# return that scalar. It is the function value that will be minimized.
R = rightSide*results1
return f # f is undefined
You probably were to optimize the values of the 7 resistances to get the maximum current somewhere in the circuit.
[code]
Code tags[/code] are essential for python code and Makefiles!