0

I have a for loop testing the max of a function:

function Start
max_i = 0;
max_j = 0;
max_value = 0;
for i =1:3500
   for j = 1:3500
      new_value = CalcUFamily(i,j);
      if new_value > max_value;
         max_value = new_value;
         max_i = i;
         max_j = j;
      end
  end
end
max_i
max_j
end

function uFamily = CalcUFamily(hh,hw) %h = male, w = wife
(code)
end

The basic is that it is testing a function that I have been trying to optimize (with some help from here) but which I have so far failed to do. I therefore want to test to make a loop that tests all possible values, work hours for a husband and a wife, from 1 h to 3500 h (yearly). I then want to get the highest utility value from CalcUFamily and its corresponding input variables, hh and hw (called i and j in the function above).

My code works well, apart from the fact that it takes too long to run since it runs 12 250 000 times. I therefore want to rase the test interval from 1 to 10 or maybe even 100. Is this possible with the for code, or do I have to rewrite it somehow?

Thanks a lot for your help!

2
  • 1
    This is an awful lot of code to ask a volunteer to slog through. Are you sure this is a minimal example? (See minimal reproducible example.) Commented Aug 4, 2017 at 15:43
  • I know. The only thing I want help with is the first part of the code, in order to loop it through. That is why I first only posted that part. Commented Aug 4, 2017 at 18:29

2 Answers 2

1

Reshape CalcUFamily into a vector using colon (or reshape) and use max to find the maximum value (max_value) and its linear index. Now use ind2sub to convert this linear index into the equivalent row (hh or max_i) and column (hw or max_j) subscripts.

[max_value, max_Ind] = max(CalcUFamily(:));
[hh, hw] = ind2sub(size(CalcUFamily), max_Ind);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! The problem is just that CalcUFamily depends of the income, which depends on the tax (another function) which depends on hh and hw, how long you work. So I just get the error: Error: File: Berakningsmodeller.m Line: 16 Column: 28 "CalcUFamily" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.
If you have CalcUFamily as a function then you should have shared its code
Sorry! Now I have
0

I found a solution myself! :)

max_hh = 0;
max_hw = 0;
max_value_hhhw = -50000;
max_value_u = -50000;

hh = 1;
hw = 1;
runs = 0;
interval = 10;
datestr(clock)

while hw < 3501
    while hh  < 3501 %runs hh to all posible values
        new_value = CalcUFamily(hh,hw);
        if new_value > max_value_u
            max_value_hhhw = [hh hw]; %working hour husband, working hour wife, and its utility value
            max_value_u = new_value;
        end 
        hh = hh + interval;
        runs = runs + 1; % number of runs
    end
    hh = 1;
    hw = hw + interval;
end
runs
datestr(clock)
max_value_hhhw

1 Comment

Instead of 12 250 000 runs, this code just runs 122 500 times. Your solution was much more elegant and more professional, the problem is just the tax code that messes it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.