Message Boards Message Boards

Search for maximum of user defined function

Posted 1 year ago

I am very new to Mathematica, so please bear with me. I have looked up all I can without posting but now I am stuck.

The following is for context, so the code (inexpertly written) is easier to understand). In addition, I ended up writing very "long hand" so I could work out what is happening, I'd like to automate more if I can find the answer to my basic problem.

I am trying to set up a toy problem where someone is making a path of choices over 3 periods (think of them as consumption) to maximize a "welfare from consumption" function. Something you can think of as "price" can take different paths (very simply from the current price: stay the same, go lower, go higher) subject to a budget constraint. In the first period, the choice is a, in the second, it could be b1,b2, or b3, depending on price; if b1 happens, then in period 3, it can be c1,c2, or c3, or if b2 happens in can be c4,c5 or c6, etc.

I substitute in the budget constraint to eliminate the "c" variables, so I am left with choice variables a,b1,b2, and b3.

I have tried FindRoot and Nsolve (see my code below), but it really doesn't seem to work - possibly because the function is too nonlinear. For the equivalent 2-period problem, NSolve or FindRoot finds the solution very easily. So next, I tried to do the following: Given "a" find the optimal choices for b1 to b3, c1 to c9, and plug them into the welfare function. Then search for the "a" that maximizes the welfare function overall. But, although writing a user function for the first part is simple enough (fun[ai_] in the code), I am stuck on how to search for the maximizing "a". As you can see below, I tried something like FindMaximum, but this doesn't seem right (as I am unable to use my user-defined function in it).

Any help is greatly appreciated.

POSTED BY: Emily O
3 Replies
Posted 1 year ago

Thank you very much for replying. I forgot that I had turned the message off (Off[Dot::dotsh} since, despite the message, it does calculate the list I want - viz

ln[1374]:= BBig

Out[1374]= {2 a + 2.4 b1 + 2.88 c1, 2 a + 2.4 b1 + 2.4 c2, 
 2 a + 2.4 b1 + 2. c3, 2 a + 2. b2 + 2.4 c4, 2 a + 2. b2 + 2 c5, 
 2 a + 2. b2 + 1.66667 c6, 2 a + 1.66667 b3 + 2. c7, 
 2 a + 1.66667 b3 + 1.66667 c8, 2 a + 1.66667 b3 + 1.38889 c9}

Although, I'd also be interested to know how I should program it correctly. I tried to search how to do "element by element" multiplication (i.e. not the complete dot product) but couldn't find out how to do this.

POSTED BY: Emily O

Crossposted here.

POSTED BY: Rohit Namjoshi

Hi Emily,

When I evaluate the code I get

Dot::dotsh: Tensors {{2,2.4,2.88},{2,2.4,2.4},{2,2.4,2.},{2,2.,2.4},{2,2.,2},{2,2.,1.66667},{2,1.66667,2.},{2,1.66667,1.66667},{2,1.66667,1.38889}} and {{a,b1,c1},{a,b1,c2},{a,b1,c3},{a,b2,c4},{a,b2,c5},{a,b2,c6},{a,b3,c7},{a,b3,c8},{a,b3,c9}} have incompatible shapes.

Thread does not Hold its arguments so Dot gets evaluated before Thread in the following

BBig = Thread[Dot[B1t, B2t]];

You do not get the same error, which is odd. Are you sure the code shared is exactly what you evaluated?

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract