For loop within a function f

15 次查看(过去 30 天)
Hello all,
I'm trying to get the following function by using a for loop:
f=@(x) ((0.0011*(x(2:1*N)-x(1:N-1)))'*exp(-0.0078*x(Battery_num*N+1:Battery_num*N+N-1)))*Battery_cost/20 + ((0.0011*(x((N+2):2*N)-x((N+1):(2*N-1))))'*exp(-0.0078*x(Battery_num*N+N-1+1:Battery_num*N+2*(N-1))))*Battery_cost/20+...
((0.0011*(x((2*N+2):(3*N))-x((2*N+1):(3*N-1))))'*exp(-0.0078*x(Battery_num*N+2*(N-1)+1:Battery_num*N+3*(N-1))))*Battery_cost/20 + ((0.0011*(x((3*N+2):(4*N))-x((3*N+1):(4*N-1))))'*exp(-0.0078*x(Battery_num*N+3*(N-1)+1:Battery_num*N+4*(N-1))))*Battery_cost/20;
I have found the pattern with i and j however I don;t know how to get the sum of it, as I get an error because of the x presence.
here is my code:
j=1;
for i=0:Battery_num-1
f=@(x)((0.0011*(x(2+i*N:j*N)-x(2-1+i*N:j*N-1)))' *exp(-0.0078*x(Battery_num*N+1+i*(N-1):Battery_num*N+j*(N-1))))*Battery_cost/20;
j=j+1;
So finally the question is how to add the values for each i. I hope my question is clear
thank you in advance!
  5 个评论
Nikolas Spiliopoulos
thanks again for the answer! Actually I'm not getting numeric answers because I have the variable x inside. I tried what you said with f{i}. It puts the funtion in a cell array however, it doesn't put the i and j for each iteration. I would expect to see something like that:
(0.0011*(x(2:1*N)-x(1:N-1)))'*exp(-0.0078*x(Battery_num*N+1:Battery_num*N+N-1)))*Battery_cost/20
But i don't. Thanks again
dpb
dpb 2018-5-9
If you try to create a function array, then you have the same issue as in the original of how to execute it; that syntax is to dereference the particular function out of the array with curlies "{}" followed by the function arguments in parens "()"

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2018-5-9
f=@(x)((0.0011*(x(2+i*N:j*N)-x(2-1+i*N:j*N-1)))' *exp(-0.0078*x(Battery_num*N+1+i*(N-1):Battery_num*N+j*(N-1))))*Battery_cost/20;
The above defines the function, it doesn't evaluate it. The function should be written as
fnf=@(x,i,j) ((0.0011*(x(2+i*N:j*N)-x(2-1+i*N:j*N-1)))' *exp(-0.0078*x(Battery_num*N+1+i*(N-1):Battery_num*N+j*(N-1))))*Battery_cost/20;
and then used as
f(i,j)=fnf(x,i,j);
for whatever combinations of i,j it is that you want.
Judicious rewriting to use "dot" operators could probably let you rewrite the function in a vectorized form to eliminate the loop; just should be to get the desired result is indeterminate for lack of complete code from which to try to decipher the intent.
  9 个评论
dpb
dpb 2018-5-9
I've not tried to compute what the subscripts in terms (x(2:1*N)-x(1:N-1)), x(N+2):2*N)-x((N+1):(2*N-1)) actually turn into numerically (we've no idea what N is here, for starters), but I can't help but believe that storing the array x as either a 2D maybe or even a cell array to simplify the lookup of the proper sequence wouldn't go a long ways towards removing much of the complexity and then be able to write legible (and more importantly working) code.
As is, I still can't fathom what it is your end result is actually supposed to be, sorry...
Nikolas Spiliopoulos
thanks again for all the comments! f is an objective function in an optimization problem, so I'm trying to find a way to update it when some parameters change. Thanks anyway for your effort!

请先登录,再进行评论。


Stephen23
Stephen23 2018-5-9
编辑:Stephen23 2018-5-9
The answer to your question is already in the title to your question: "For loop within a function f" The attmempts that you showed are the other way around: you define a function inside a loop, but you need a loop inside a function. It is not possible to put a loop inside an anonymous function, so you will need to define a function in a file, e.g. as a nested function:
function out = myfun(x)
out = 0;
for k = 1:Battery_num
out = out + (0.0011*(x(2+(k-1)*N:k*N)-x(2-1+(k-1)*N:k*N-1)))' * ...
exp(-0.0078*x(Battery_num*N+1+(k-1)*(N-1):Battery_num*N+k*(N-1)))) * ...
Battery_cost/20;
end
end
Alternatively it may be possible to vectorize your code, in which case using an anonymous function will still be possible. If you told use the sizes of all of the variables then we might be able to help you with vectorizing the code.
  4 个评论
Stephen23
Stephen23 2018-5-9
编辑:Stephen23 2018-5-9
myfun has one input argument x. When you call it from the command line, you will need to supply a value for this argument, e.g.
myfun(1) % or whatever value of |x| you want to use
If you do not supply x then MATLAB has no idea what value to use for the x inside the function. Thus the error.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by