For loop within a function f
    8 views (last 30 days)
  
       Show older comments
    
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 Comments
  dpb
      
      
 on 9 May 2018
				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 "()"
Answers (2)
  dpb
      
      
 on 9 May 2018
        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 Comments
  dpb
      
      
 on 9 May 2018
				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...
  Stephen23
      
      
 on 9 May 2018
        
      Edited: Stephen23
      
      
 on 9 May 2018
  
      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 Comments
  Stephen23
      
      
 on 9 May 2018
				
      Edited: Stephen23
      
      
 on 9 May 2018
  
			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.
See Also
Categories
				Find more on Loops and Conditional Statements 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!


