7
equals
8
equals
9
equals
10
sage code
11
a = 2 b = 3 def objFunc(p): g(x) = sum(p[j]*x**(j+1) for j in range(len(p))) f(x) = g(x)-g(2)+b x1 = 99 try: x1 = find_root(f, a, 99) except: pass return numerical_integral(sqrt(1+f.derivative()**2), 0, x1)[0] startP = (3.3, 0.7, -1.4) sol = minimize_constrained(objFunc, [(None, None), (None, None), (None, 0)], startP) print (sol) print (objFunc(sol))
12
#Let's solve for even higher degree a = 2 b = 3 def makePoly(p): g(x) = sum(p[j]*x**(j+1) for j in range(len(p))) return g(x)-g(2)+b def objFunc(p): f = makePoly(p) x1 = 99 try: x1 = find_root(f, a, 99) except: pass return numerical_integral(sqrt(1+f.derivative()**2), 0, x1)[0] def solveRand(n): startP = [10*random()-5 for _ in range(n)] sol = minimize_constrained(objFunc, [(None, None)]*(len(startP)-1) +[(None, 0)], startP) return (sol, objFunc(sol)) n = 4 simuN=10 bestSol = None bestVal = 999999999 for j in range(simuN): sol, val = solveRand(n) if sol==None or val<bestVal: bestSol = sol bestVal = val print (bestSol) print (objFunc(bestSol)) g = Graphics() solPoly = makePoly(bestSol) g += plot(solPoly, 0, find_root(solPoly, a, 10)) g += points([(a,b)], color='red') g += line([(0, b), (a,b)], color='red') g += line([(a, 0), (a,b)], color='red') g.show()
13
for n=4: (0.4262590448096395, -1.4604762488028293, 1.3673636412046153, -0.37672432166692) 5.194897146463533
14
15
powered by
powered by
New Blank Graph
Examples