nested function problem solving

2 views (last 30 days)
sensation
sensation on 29 May 2018
Commented: Stephen23 on 29 May 2018
Hi, I have created a nested function where many functions that call each other, and they all contribute to main fun R. However, it doesnt work (Not enough input arguments). Do you have any clue why? Thanks!
function R=revenue_nested(price,ro,g,eff,x,N,k1)
totaloutflow
storage1
depth1
function T=totaloutflow(x,N)
T=x(1:N)+x(N+1:2*N);
end
function S=storage1(init_s,inFlow,x,N)
S(1) = init_s(1) + inFlow(1)-x(1)-x(N+1);
for ii = 2:N
S(ii) = S(ii-1) + inFlow(ii)-x(ii)-x(N+ii);
end
end
function HH=depth1(S,alpha_par,b_par)
HH= (S/alpha_par).^(1/b_par);
end
R= -sum(price.*((HH*ro*g*eff).*x(1:N))/k1);
end

Answers (1)

Stephen23
Stephen23 on 29 May 2018
Edited: Stephen23 on 29 May 2018
This is how you define the function totaloutflow:
T=totaloutflow(x,N)
It has two input arguments, both of which are required.
This is how you call the function totaloutflow:
totaloutflow
Where are the input arguments? There are none, thus the error. The same for the other functions.
With nested functions for each variable you have a choice between:
  1. to pass it as an input/output argument, or
  2. to access it directly from the main function's workspace (which means that it has to exist in the parent workspace, and is not defined as an input/output variable).
E.g. to define a nested function that accesses x, passes N, and writes T:
x = ...
T = [];
...
totaloutflow(1)
...
totaloutflow(2)
...
totaloutflow(3)
...
function totaloutflow(N)
T = x(1:N) + x(N + 1:2 * N);
end
...
So far you have not defined init_s, inFlow, alpha_bar, or b_bar anywhere, so I cannot suggest how you should fix the rest of your code.
  4 Comments
sensation
sensation on 29 May 2018
Hi Stephen. Thanks for your tips. Beforehand I created many local functions (saved as separate files) as you suggested. It run but the HH was constant (linear) in R and it should not be like that. I wanted to have HH depending on x values so when I run:
[x(:,i),fval(:,i)] = fmincon(@(x0)revenue(price(:,1),HH,ro,g,eff,x0,N,k1),x0(1:2*N,i),A,b(:,i),Aeq,beq(:,i),LB(:,i),UB(:,i),[],options);
I want to get optimized x values. These are the same values that act first in T, then in S, then in HH and finally in R. That was the reason why I think that here I should go for a nested function. I could also maybe create many local functions but do you know how I can call then depth and storage within the revenue,R (so I optimize x in each of those functions)? Thanks!
Stephen23
Stephen23 on 29 May 2018
"Beforehand I created many local functions (saved as separate files) as you suggested."
I suggested using local functions. Local functions are saved in one file. I did not suggest that you should put all of the functions into separate files (although it may be possible, depending on your algorithm (which I know nothing about)). You can learn about the different kinds of functions here:
"I want to get optimized x values."
"HH ... should depend on x that we optimize."
Aha, and you are calling fmincon to perform some optimization on x. If you want HH to depend on a variable that changes with the optimization then you could use nested functions to define that variable, but you would have to remove it from the anonymous function definition input arguments (otherwise it will just be a constant).
To be honest I have trouble keeping track of your examples, which ones you are now using, or what is not working. It would help if you put them into one file (as they should be) and uploaded that in a new comment.

Sign in to comment.

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!