This function keeps returning the error"ttempted to access wf(2); index out of bounds because numel(wf)=1. "

1 次查看(过去 30 天)
This function keeps returning this error and I cannot figure out how to fix it. Can anyone help?
function Powerval=Analyse_Grid(wf)
global wind_farm;
global N;
global velocity_farm;
N = 30;
wind_farm = zeros(N,2);
velocity_farm = zeros(N,1);
wf = wf * 1000;
wf = round(wf);
for a = 1:1:N
b = (2 * a) - 1;
wind_farm(a,1) = wf(b);
wind_farm(a,2) = wf(b + 1);
end
power = 0;
total_power = 0;
wind_farm = sortrows(wind_farm,[2 1]);
for i = 1:1:N
x = wind_farm(i,1);
y = wind_farm(i,2);
velocity = check_wake(x,y,i);
velocity_farm(i) = velocity;
power = 0.3 * (velocity ^ 3);
total_power = total_power + power;
end

回答(2 个)

Roger Stafford
Roger Stafford 2015-3-15
Matlab states that the number of elements it finds in 'wf' is 1. It must mean that you passed a scalar 'wf' to 'Powerval' when you called on it. You had better check on how you called on it, because your code within the first for-loop calls for it to have at least 60 elements in it.

Star Strider
Star Strider 2015-3-15
You have defined ‘wf’ as an input argument. What are you supplying to it in your call to ‘Analyse_Grid’?
It wants a vector of length (2*N-1), that since you defined ‘N=30’, would be 59.
At least that’s how I interpret your code.
  4 个评论
Lorcan O'Toole
Lorcan O'Toole 2015-3-15
Thanks dude ! It works without the function but I was trying to use this function along with 2 constraint functions in a G.A . The purpose is to optimize the layout of a Wind farm. Do you think Ill have to start again? Thanks Again man !
Star Strider
Star Strider 2015-3-15
My pleasure!
I don’t know how you’re using it in GA. If ‘wf’ is a ‘chromosome’ (or ‘individual’), then it needs to be a vector, and possibly a vector of parameters. The GA algorithm then optimises the parameters according to your designated ‘fitness function’.
I don’t have significant recent experience with the MATLAB GA functions (I wrote a number of my own in FORTRAN ‘way back when), so I would be reluctant to advise you specifically on the rest of your code, but consider that if ‘Analyse_Grid’ is your fitness function (or called by it), ‘wf’ nevertheless needs to be a vector.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by