Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OK, the third and last time ;-)

L_roots = [-1,4]
L_max = [(-3,1)]
L_min = [(1,1)]
L_other = [] 

distinguish_min_max = False  # set to True or False

N = len(L_roots)+2*len(L_min)+2*len(L_max)+len(L_other)-1

param_list = [var("a%d" % k) for k in [0..N]]

f(x) = sum([ param_list[k]*x^k for k in [0..N]])
df = diff(f,x)
ddf = diff(df,x)
show(f)

eqns = [ f(x0)==0 for x0 in L_roots ]
eqns += [ f(P[0])==P[1] for P in L_min ]
eqns += [ df(P[0])==0 for P in L_min]
eqns += [ f(P[0])==P[1] for P in L_max ]
eqns += [ df(P[0])==0 for P in L_max ]
eqns += [ f(P[0])==P[1] for P in L_other ]

if distinguish_min_max:
    eqns += [ ddf(P[0])> 0 for P in L_min]
    eqns += [ ddf(P[0])<0 for P in L_max]

#for eq in eqns:
#    print eq

sol = solve(eqns,param_list,solution_dict=True)
show(sol)

try:
    g = f.substitute(sol[0])
    show(g)
    G = Graphics()
    G += g.plot(xmin=-10,xmax=10,ymin=-10,ymax=10)
    G += points(L_min,color='red')
    G += points(L_max,color='red')
    G += points(L_other,color='black')
    G += points([(x0,0) for x0 in L_roots],color='green')
    G.show()    
except:
    print 'no solution'