# Numerical optimization

I want to find the numerical maximum for I and then substitute the result in UU, but I cannot obtain the numerical value of I in the following code

var("U,X,alpha,W0,W1,W2,pp,I, bb")

U= X^.5

b=1.2 # pouvoir de monopole

p=0.5 # probabilité qu'il ne se produise rien

w0=8 # richesse initiale

D=6 # coût du dommage

a= .5

W1=w0-(1-p)*b*I

W2=w0-D+(1-(1-p)*b)*I

UU= p*U(W1)+(1-p)*U(W2)

show(UU)

UU_I = UU.diff(I)

show(UU_I)

solve(UU_I==0, I)

edit retag close merge delete

Please format the code properly, after a copy+paste of the code mark this block and press that button with

101
010


on it. (This is the button after the B for "bold face", the I for italic, and that link button...)

Then please try to isolate a minimal instance of your problem, rather than giving a code with a lot of parameters, where only one parameter matters. Please try to isolate also the programming issue(s). For instance, if we have to solve an equation, then extract one particular solution as an expression in some other variable, then plug in some specific value for this variable, then please describe these steps. (It may be a "simpler way" to plug in that special value, then solve. So please explain why the "complicated way" is really needed.)

Sort by » oldest newest most voted

If you look at the documentation of solve function:

sage: solve?


you will see e few options, in particular to_poly_solve.

If you do :

sage: S = solve(UU_I==0, I, to_poly_solve=True) ; S
[I == (7/3)]


Now, to extract the 7/3 value, you should beter get a list of dictionnaries than a list of expressions:

sage: S = solve(UU_I==0, I, to_poly_solve=True, solution_dict=True) ; S
[{I: 7/3}]


Now it is easy to extract the solution:

sage: S[I]
7/3


And to evaluate UU on this value :

sage: UU(S[I])
2.14087209644419


Which looks consistent with the plot:

sage: plot(UU,I,-10,10)


By the way, instead of defining U= X^.5 you should better directly use the sqrt function.

more