-2

I have a function in matlab called function UFamily = CalcUFamily(hh,hw). The value of the function is determined by hh and hw (working hours for husband and wife in a family). I want to get the hh and hw that maximizes the function UFamily.

How shall i proceed? I can't get the fmincon to work.

Thanks for any help! :)

KGB91
  • 630
  • 2
  • 6
  • 24
  • You need to add your code of how you tried to use `fmincon` and almost definitely need to add more information about (if not code for) `CalcUFamily` – Dan Jun 04 '17 at 23:33
  • `h = [1000 1000]; % start value lb = [0 0]; % lower bound of h ub = [3500 3500]; % upper bound of h Uoptimal = fmincon(@(h1, h2) -U(h1, h2), h, [], [], [], [], lb, ub);` `CalcUFamily` is a very long code, but it is based on the working hours of the female and male in the household. – KGB91 Jun 04 '17 at 23:34
  • Please add the code into the question which you can edit, code in comments like this is unreadable – Dan Jun 06 '17 at 02:01
  • Possible duplicate of [Find maximum of a utility function U(h1, h2)](https://stackoverflow.com/questions/43723639/find-maximum-of-a-utility-function-uh1-h2) – m7913d Jun 06 '17 at 06:19
  • Please do not duplicate your questions. This is one of the worst thing you can do. When you have a follow up question, at least provide a link to your previous question and clearly specify the difference. – m7913d Jun 06 '17 at 06:23
  • Ok, sorry! Wont do it again – KGB91 Jun 06 '17 at 10:53

1 Answers1

0

The function for fmincon to minimize should use 'h' (1x2) as its input, while your function CalcUFamily requires two input, hh and hw. You can do something like:

myFun = @(h) -CalcUFamily(h(1), h(2)); % function to minimize with one input
Uoptimal = fmincon(myFun, [1000 1000], [], [], [], [], lb, ub);
Xiangrui Li
  • 2,386
  • 2
  • 13
  • 17
  • Thanks! Will this work for testing from 1 hour to 3500 hours for male and female, separately? – KGB91 Jun 05 '17 at 12:55
  • I don't one can answer this, since this is related to how you evaluate it, i.e. how your CalcUFamily function computes the scores. – Xiangrui Li Jun 05 '17 at 15:33
  • I get: `Error in Berakningsmodeller (line 6) Uoptimal = fmincon(myFun, [1000 1000], [], [], [], [], lb, ub); Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.` Do you need the whole code in my other function? It is long, but I can post it if needs be. – KGB91 Jun 05 '17 at 23:20
  • No need for the whole code, but you need to make sure your CalcUFamily function receives two numbers (hh, hw) as input, and gives output of single number. – Xiangrui Li Jun 06 '17 at 02:42
  • Ok, I'll try it out tomorrow again! – KGB91 Jun 06 '17 at 22:42
  • Seems like the code works! Thanks a lot :) One more question, I would need to add some parameters when I call UFamily (like income) that are used in the UFamily function but that are fixed and shall not be optimized. How can I do that? – KGB91 Jun 07 '17 at 23:21