# Optimal Control - Calculus of Variations

I'm trying to find the profit function. All the specific info should be in the link but if not I attached the short article im looking at with the specific example in more detail.

Xtyrantix

12

## Answer

**Answers can only be viewed under the following conditions:**

- The questioner was satisfied with and accepted the answer, or
- The answer was evaluated as being 100% correct by the judge.

Alessandro Iraci

1.7K

The answer is accepted.

Join Matchmaticians Affiliate Marketing
Program to earn up to a 50% commission on every question that your affiliated users ask or answer.

- answered
- 1064 views
- $200.00

### Related Questions

- Exercise 4.33 from Spivak's Calculus on Manifolds.
- Velocity of a rock
- Use Green’s theorem to compute $\int_C x^2 ydx − xy^2 dy$ where $C$ is the circle $x^2 + y ^2 = 4$ oriented counter-clockwise.
- Proving f is continuous
- Convergence of $\int_{1}^{\infty} e^{\sin(x)}\cdot\frac{\sin(x)}{x^2} $
- The volume of a spherical tank with radius = r is expanding in such a way that r is increasing at 1 cm/min. At what rate is the volume expanding when r = 100 cm?
- Find all values of x... (Infinite Sums)
- Derivative of $\int_{\sin x}^{x^2} \cos (t)dt$

I think page 1121 states the whole problem I'm trying to solve.

Are you trying to maximize the function pd? What are the fixed variables and what are the variables that you would like to maximize with respect to?

I am trying to maximize it. I'm not sure which variables are fixed, as it is a dynamic model. I'm trying to recreate the table on page 1120 but with an initial quantity of 5,000 instead of 10,000. The page also explains where the maximum values come from (which equations it used) if that helps, sorry I'm not much help I only understand the paper on a purely theoretical level, not really the specific example. I can offer more bounty for help recreating this table for my specific case aswell.

So you expect someone to kind of read the paper and explain it to you. That would require at least a couple of hours of work. Yes, your bounty seem too low for that much work.

Did you check the link I provided? I'm not sure the paper really needs to be read, I just reference it as it specifically mentions the equations it uses and it's easier than typing it. I think with the variables defined the paper doesn't really need to be read at all, but I might be wrong.

But if it does, I'd be happy to change it to whatever price seems fair.

I understand the paper, but I just don't know how to get values for the "optimum strategy" part of the table given. That is basically what I'm trying to find from all of this.

Yes, but the information provided in the link is incomplete. Some of the variables do not even appear in the function you want to maximize, and one needs to know what are the independent variables.

What do you think would be a fair amount for someone to read the article, find the maximum function, and replicate a table similar to the one in the paper? Like $200 or should it be higher.

I guess 200 would be good.

Ok, so just to clarify from my original question. I'm looking to maximize the profit function in the link I provided and make a table similar to the one on page 1120 but with an initial quantity of 5,000 instead of 10,000. I don't need work for the entirety of the table just the last row and maybe the first entry of each of the other rows.

I see. I am busy today, I will take a closer look later and will try to answer if no one else accepts to answer before then.

Hey, I wrote some code that gives the table you want. I don't get exactly their values for Q(0)=10000, and I think it's because that paper is very old and computer approximation wasn't as accurate. In that test case, I get the optimum unit price for year 5 to be ~3.53 rather than ~3.55, and this changes the values quite a bit. In particular, you get Q(20) = 7881 rather than 8508. Is that satisfying? I can do the same for Q(0) = 5000 if that works for you.

Actually I can just upload the code (you need to install Sagemath) and you can pick whatever Q(0) you like.

Ah, uhm, the discrepancy might also be due to the fact that I'm keeping the unit cost constant for each quarter. I'll have to fix that.

Ok sounds neat, what language is the code in?

It's Sagemath, which is a Python-based language. If you're proficient with Python you should be able to read it. I still have to fix it though; I took an extra simplification that was not permitted. I think they suggest to deal with it numerically; I'll try to find out how.

I've used python a bit before, if you could include some basic comments in it so its easy to follow that would be great! thanks!

Also, if you see this before you finish fixing it can you still upload the code anyways so I can get an idea of what it looks like?

Well, I can't upload anything without submitting an answer. Anyway, I think I understood how to fix that but I still don't get the table listed there, and honestly the difference in values between the "C constant" and "C variable" case is so minimum I don't think it's worth the extra (significant) computational cost.

I'm also very confused about what happens in the paper. In the formula at page 1121, second paragraph, they say Q(19) = Q(20) - V(20)/4, which only makes sense if V(20) is constant during the last quarter, and so Q changes linearly. I implemented the "C variable" case this way and still don't get their table.

If you think the answer would still be acceptable it should be fine, would it be possible to try to show the calculus of variations method the paper references if it wouldn’t be too much work?

I wish I could say something relevant in regards to the table, I’ve tried many things to replicate the table and always found their numbers confusing.

A program that atleast makes a table with a similar final finding(that Initial optimum strategy starts at a loss and then ends in profits) and maybe the Euler equation that optimizes equation 7 with work should be sufficient!

I have a textbook that gives a generic case of this if it would help to look at, I can add it as a file it's only one page.

That might help, thank you. I took the "naive" approach of optimizing each "Delta_pi_D" separately, and I got the numbers I showed you before (they're very similar regardless of the fact that we keep C constant or not during a quarter), and the total profit is also not much less.

But the pricing is somewhat off, it ends indeed very close to what they have (3.53 for C constant, 3.54 for C varabile, they have 3.55), but it increases going back in time rather than going up and then down. You still a table that starts at loss and ends at profit, even though I didn't compute that explicitly (but I can, it's not difficult).

The Euler equation for (7) is not computed by them, and our functions are not smooth, so I don't think it's possible to get that (look at end of page 1120 / beginning of page 1121).

Ok I see. And yea I’d you can include that in the table that would be great, I’m trying to replicate the table as exact as possible

I'll code a function for that, np.

I think I understand what's wrong but not how to get it right. The problem is that I'm maximizing Delta \pi_D(n) individually for each n instead of the sum from n onwards. I asked an expert in dynamic programming and they're also confused. I'll think about it a bit more.

Ok, if you can figure it out that’s great but if not it’s fine. This is part of a project I’m working on for my undergrad in economics/math. it’s probably fine if I simplify it a bit aslong as the results are still similar to the paper and the problem still is dynamic even if we hold the cost constant (only because the paper talks a bit about the experience learning curve is my worry)

Do you specifically have to deal with dynamic programming in your project?

Well, my math advisor mentioned it but our offered courses don’t really cover it so if it’s pretty advanced it might be fine to not in include. The main point of this is to look at a dynamic pricing model instead of a classical marginal one. I obviously can’t see the program you made yet but if you think it would be suitable for an undergrad math project i think it might be ok to exclude the dynamic programming.

I think what I have so far might work, after some polishing. I'll try to figure it out for a bit more, then I'll submit something, if you're happy with it we're cool otherwise I'll try to get something better. I thought the problem was easier, but then the algorithm at page 1121 is poorly explained and the correct implementation is a lot more difficult. I think I understand the strategy now, still not sure about the implementation though.

Yea … when I first took on the project it seemed simpler until I realized the paper was written pretty poorly and I realized I took on more than I could handle haha. The help is greatly appreciated though!

The more I think I understand the paper, the less the code makes sense. At the moment I have a draft, I'll submit the code I have now so you can start looking at it. If that is enough for you, I'm good with only a partial payment (I can't say I've 100% answered the question), but honestly for an undergrad I think that going farther is more difficult than what reasonably expected (given that I asked a PhD who does dynamic programming as a job and he still can't get there). Just let me know.

The strategy I got doesn't start at loss though. It still does better than marginal and optimum constant return, but not as well as the optimal one. Note that the problem is computational, not mathematical: I know what I have to implement, but without dynamic programming is too computationally expensive to actually find, and their algorithm is really not explained at all.

Yea if you wanna send the code I’ll look at it and see. I agree the more I look over the paper the harder this seems (especially at my level) and I’m sure my professors would understand me simplifying a bit considering two people much smarter than me are struggling with it.

Apparently I can't upload formats that are not images?

Download it from here: https://www.mediafire.com/file/zudblk2gb2jayxu/dynamic_pricing.ipynb/file

You know what, check the file, and if that is vaguely satisfying, tell me how much you think it's fair for that work and we can close the question for that price, as long as it's reasonable. I'm not sure I can do better than this, and even if I could it's a huge amount of work. Just let me know.

Sounds good, can you help me debug it to get it to run? (It's on sage math 9.4). I attached screen shots of the entire code, I'm not sure if a line might have gotten lost that defines price or something.

Yeah sure, you have to run the line that generates the table first. There are two, one is independent from C and fast, the other is a little more precise but a lot slower.

Aaaaah, my bad, I forgot to update a thing. On lines 18 and 19, replace "price" with "Pr[20-n]". Sorry about that.

I ran the whole thing, I think its erroring at this part of the equation int(V(price, Q[20-n])/4) . I'm not sure if it's because the only price we defined is the dict to store values (pr) ? Also I have no idea what's wrong with the DAP part, the error is not very helpful lol

oo ok just saw your comment haha

I'm very sorry I couldn't provide anything better, but they really don't explain how to implement D) with dynamic programming.

I tried to get the table for optimal constant price as well, but it's still wrong somehow. I might be missing something with the methodology.

Try running this (// denotes a new line): price = 3.25 // Q[20] = 8915 // for n in range(20): // Q[19-n] = Q[20-n] - int(V(price, Q[20-n])/4) // print(Q[19-n])

This gives a table that's very close to table C), but not exactly the same. I'm starting suspecting they solve the equation for pi_D exactly (like, solving the ODE dQ/dt = V) and use that Q, and admittedly you can do that explicitly (Wolfram gives an exact solution), but then I don't know how to give that to Sage in a way that's computationally feasible.

Ok, sounds good. Just a few questions. Would it be a lot of work to add the marginal pricing table/constant return? Is there an easy way to get it to print the unit cost along with the prices and quantities for the tables? With these changes (if they don't add a lot of work) I'd be fine with the whole bounty being given. Even without it it's considerably more than what I was able to do so I see no problem giving most of the bounty, as the complete answer is unreasonable to ask.

I don't know, probably not but I have to think about it. About printing the cost, you can just run "for n in range(20): // print(C(Q[n])), the C functions deals with that automatically.

I just tried running the code you gave, it tells me price = RealNumber('3.25') // Q[Integer(20)] = Integer(8915) ^ SyntaxError: cannot assign to operator

The ^ points to the real number part

Nevermind I realized my mistake haha

I think I can get the tables for those two cases but still there is something wrong. They're not too far from the originals, but not especially close either.

Ah okay, got it. For marginal pricing and optimum constant return there is no need to work backwards, I can work forward and get the right thing. I'll post an updated notebook soon.

Thanks! If you could clearly distinguish between them thay would be great!

https://www.mediafire.com/file/kc9zlpzyq8stcvu/dynamic_pricing.ipynb/file

There you go, everything is commented. The assumption I'm using (that they don't have in the paper) is that V is treated as constant in every quarter, and so Q changes linearly rather than according to the proper ODE. This changes the value a bit, but in the marginal and optimum constant return cases, not too much. The optimum constant price changes significantly somehow, and the optimum strategy requries dynamic programming to implement properly, the one I gave you is not very refined.

Wait a sec, I think it didn't save the file. I'll reupload.

Try this: https://www.mediafire.com/file/056gdz4vz2acdg6/dynamic_pricing_updated.ipynb/file

Hey, I don't want to pressure you or anything, but another user asked me to answer a few high-priced questions later in the afternoon and booking this one pretty much drained my account. If you are satisfied with the answer, can you tell me so that I can submit it and have it accepted? I'm totally fine with giving you a discount and I'll remain available for further questions/debugging, but I want to avoid having to recharge through PayPal because of the fees. Feel free to say no. Thanks!

Sorry, I have t gotten the chance to look at it but if you’re still available after for any debugging that might come up then I’m ok with you submitting it as the answer!

Also, my question was very involved, I have no problem with you taking the full amount as I’m sure it took quite a bit of time away from you.

Yeah, I'll be available for sure, but if you don't feel okay with having it accepted before even looking at it I completely understand, no worries. I also got a small recharge from the admin in the meantime, which should be enough to reserve a question or two. Worst case, the fees are going to be less anyway. Take your time, get back to me after looking at it!

Well indeed it took me a lot of time, so I won't say no to your offer if that is not a concern for you, but I also realize that it's not a full answer to the orignal question, so I'm also okay with a discount.

It’s completely fine, you can close it I don’t know if I’ll be able to look at it before much later today. If there is a problem I encounter should I just comment on here again or will I not be able to after you post an answer? But yea you can definitely take the full amount no problem.

We will both be able to comment, for sure, even after it's accepted. Well, thank you very much for your understanding! I'll submit the answer then.

Hey, just checking if you had the chance to check the notebook and if you're okay with it or you need further help.