This function keeps returning the error"ttempted to access wf(2); index out of bounds because numel(wf)=1. "
Show older comments
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
Answers (2)
Roger Stafford
on 15 Mar 2015
0 votes
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
on 15 Mar 2015
0 votes
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 Comments
Lorcan O'Toole
on 15 Mar 2015
Star Strider
on 15 Mar 2015
My pleasure!
Supplying a scalar value of 60 and a 60-element vector (that the function wants) are two different situations. The scalar value will quite simply not work.
When I strip the code out of the function (and delete the global calls), I get:
wf = linspace(100, 1, 60);
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
This works until it throws the error:
Undefined function 'check_wake' for input arguments of type 'double'.
Error in AnswersTestScript (line 20263)
velocity = check_wake(x,y,i);
So give it a 60-element vector and see how it goes.
Lorcan O'Toole
on 15 Mar 2015
Star Strider
on 15 Mar 2015
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.
Categories
Find more on Genetic Algorithm 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!