Reputation: 1
% Power output
power_output = MF_t.*difference.*e;
% POWER OUTPUT OPTIMIZATION USING - GOLDEN SEARCH METHOD
x_lower = 0.2;
x_upper = 1.4;
h = (x_upper - x_lower);
r = (sqrt(5) - 1)*0.5;
x2 = x_lower + (1+r)*h;
x3 = x_upper - (1+r)*h;
tol = 0.05;
count = 1;
while h>-tol
f2 = -power_output(x2)
f3 = -power_output(x3)
if f3<f2
x_lower = x2;
x_upper = x_upper;
h = (x_upper - x_lower);
x2 = x3;
x3 = x_upper - (1-r)*h;
elseif f2<f3
x_lower = x_lower;
x_upper = x3;
h = (x_upper - x_lower);
x3 = x2;
x2 = x_lower + (1-r)*h;
else
x_lower = x2;
x_upper = x3;
h = (x_upper - x_lower);
x2 = x_lower + (1+r)*h;
x3 = x_upper - (1+r)*h;
end
interval = x_upper - x_lower;
count = count + 1
if count > 100
break
end
end
answer = (x_upper + x_lower)/2
value = fanswer
Im trying to run this code, it's telling me that there is an error as my arrays f2 and f3 as "Array indices must be positive integers or logical values". I'm wondering would anyone know if taking the absolute values of my arrays will make my optimisation incorrect
Upvotes: 0
Views: 1015
Reputation: 1769
The error message is quite informative in this case. You are trying to access the x2
th element of f2
. A quick calculation shows that on the first iteration x2 = 2.14
. It is not possible to take the 2.14th element of f2
because this is is not an integer, and taking the absolute value will not help.
x2
is not an index, it is a value. On each iteration, the Golden Ratio search requires you to actually evaluate power_output
with whatever variable set to x2
. So, it looks like you need to do this calculation power_output = MF_t.*difference.*e
with x=x2
.
Upvotes: 1