Use a function instead of an anonymous function, such that you can use the debugger for further investigations:
function x = hiz_fun_Func(hiz, inclined_road, total_loss, h)
x = (1/2) * 1750 * hiz(inclined_road(h-1))^2 - total_loss(hiz) - (1/2)*1750*hiz.^2;
end
And provide only the parameters by an anonymous function:
hiz_fun = @(hiz) hiz_fun_Func(hiz, inclined_road, total_loss, h)
Now set a breakpoint in hiz_fun_Func to check, if inclined_road or total_loss is affected by the exceeding index.
Sorry, this will not help directly. Do this for the other anonymous functions also. I guess, the core of the problem is:
hiz + hiz(inclined_road(h-1))
in the definition of WL. The nested anonymous functions impede the debugging tremendously. It is a good idea to avoid such troubles by using them only to provide the parameters, while the actual code is written as functions, which can be examined using the debugger.
Best Answer