- to pass it as an input/output argument, or
- 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).
nested function problem solving
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
Stephen23
el 29 de Mayo de 2018
Editada: Stephen23
el 29 de Mayo de 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.
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 comentarios
Stephen23
el 29 de Mayo de 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.
Ver también
Categorías
Más información sobre Linear Programming and Mixed-Integer Linear Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!