Ask Your Question
0

Numerical optimization

asked 2020-04-12 03:10:35 -0500

Cyrille gravatar image

updated 2020-04-14 07:00:02 -0500

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 flag offensive close merge delete

Comments

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.)

dan_fulea gravatar imagedan_fulea ( 2020-04-13 12:23:33 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2020-04-14 05:52:45 -0500

tmonteil gravatar image

updated 2020-04-14 05:53:57 -0500

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[0][I]
7/3

And to evaluate UU on this value :

sage: UU(S[0][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.

edit flag offensive delete link more

Comments

One more time I fell stupid

Cyrille gravatar imageCyrille ( 2020-04-14 12:17:03 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-12 03:10:35 -0500

Seen: 66 times

Last updated: Apr 14