You have to assign fval to one of the outputs of the top-level function.
Or add fval as an output of the top-level function -- OptimiseEnergy
For example:
function [x,y,z] = playfunc(a,b)
% a and b are equal size vectors
x = a+b;
y = a-b;
z = conv(x,y);
end
conv.m is called inside playfunc.m and the output of conv is returned from playfunc.m