"diana van dijk" wrote in message <igheu1$kdd$1@fred.mathworks.com>...
> Hi,
>
> I'm optimizing based on 2 state variables (x,y). As for x, I want to iterate over all values in the state space. As for y, I want to iterate over all values below x/(1a), where 'a' is a parameter.
> So far, I've coded this with an ifloop. But then I still iterate over all y's, with the consequence that I need to specify and 'else'. What I would like, is to never iterate over y > x/(1a).
> If I didn't need to coordinates of x and y in the 2D matrix M, I could have done 'for y=Y(Y<=x/(1a))'. Is there an equivalent for my case where I do need to refer to the coordinates?
> I hope you can help me!
>
> X = 0.1:0.1:1
> Y = 0.1:0.1:2
>
> for x = 1:length(X)
> for y = 1:length(Y)
> if (y <= x/(1a))
> LB=...
> UB=...
> objfcn = @......name(x,y)
> [result,fval,exitflag,output] = fminbnd( objfcn,LB,UB)
> M(x,y) = result
> else M(x,y) = 0
> end
> end
>
>
First, I think there's a problem with your existing code. The vector X contains 0.1, 0.2, 0.3 ..., but in the loop x gets successive values 1, 2, 3 ... . Do you actually want x to take on successive values from X? Likewise for Y and y?
Assuming that you want to use the values from X and Y, the code would go something like this (needs checking):
M = zeros(length(X), length(Y); % avoid having to assign zeros inside loop
for xindex = 1:length(X)
x = X(xindex);
Yvalid = Y(Y <= x/(1a));
for yindex = 1:length(Yvalid)
y = Yvalid(yindex);
... % compute function of x and y
M(xindex, yindex) = ... whatever
end
end
