MATLAB: Im trying to find the x value that minimises the y value

matlab function

hi i have 7 different formulas and im trying to the find the x value (or v value in this case as its velocity) that would give the lowest y value(fuel cost);
function fuelcost=fuel_used(v)
d1 = 100000;
fuelcost_1 = (0.5 + 1.5*exp(-((d1/v(1))/20)) + 0.3*(v(1))^(2)) *(d1/v(1));
d2 = 20000;
fuelcost_2 = (0.5 + 1.5*exp(-((d2/v(2))/20)) + 0.3*(v(2))^(2)) *(d2/v(2));
d3 = 50000;
fuelcost_3 = (0.5 + 1.5*exp(-((d3/v(3))/20)) + 0.3*(v(3))^(2)) *(d3/v(3));
d4 = 75000;
fuelcost_4 = (0.5 + 1.5*exp(-((d4/v(4))/20)) + 0.3*(v(4))^(2)) *(d4/v(4));
d5 = 95000;
fuelcost_5 = (0.5 + 1.5*exp(-((d5/v(5))/20)) + 0.3*(v(5))^(2)) *(d5/v(5));
d6 = 95000;
fuelcost_6 = (0.5 + 1.5*exp(-((d6/v(6))/20)) + 0.3*(v(6))^(2)) *(d6/v(6));
d7 = 90000;
fuelcost_7 = (0.5 + 1.5*exp(-((d7/v(7))/20)) + 0.3*(v(7))^(2)) *(d7/v(7));
fuelcost=(fuelcost_1 + fuelcost_2 + fuelcost_3 + fuelcost_4 + fuelcost_5 + fuelcost_6 + fuelcost_7);
end
rng default
fun = @fuel_cost;
lb = 0;
ub = 20;
A = [];
b = [];
Aeq = [];
beq = [];
nvars = 7;
[v,fval] = ga(fun,nvars,A,b,Aeq,beq,lb,ub);

Best Answer

Using the ga function is likely overkill for such a small problem.
Try this:
iv = rand(7,1)
[v,fval] = fminsearch(@fuel_used, iv)
By the way, your function name is ’fuel_used’, not ‘fuel_cost’, at least if you are using the same function in ga that you posted here.
Related Question