MATLAB: What is missing at fzero command? (Data.xlsx at the attachment)

for loopfunctionfunctionsfzeroif statementmatlab function

I should calculate each "hiz" values with for loop. If i accept the starting value as "hiz":
hiz(inclined_road(1))=15;
the code can not work.
Error using fzero (line 306)
FZERO cannot continue because user-supplied function_handle ==>
@(hiz) (1/2)*1750*hiz(inclined_road (h-1))^2-total_loss(hiz)-(1/2)*1750*hiz.^2;failed with the error below. Index exceeds matrix dimensions.
I don't understand where the wrong is.
dgs=[1 204 332 396 428 499 578 630 661];
for z=2:1:numel(dgs);
if (ele(dgs(z))-ele(dgs(z-1)))<0;
inclined_road=dgs(z-1):1:dgs(z);
for h=2:1:numel(inclined_road);
WR= 0.015.*(1+hiz(inclined_road(h-1))^2/1500)*1750*9.81*cos(slope(inclined_road(h-1)));
WS(h-1)=1750*9.81*sin(slope(inclined_road(h-1)));
WB(h-1)=0;
WL=@(hiz) (1/2)*0.99.*((0.5)*(hiz+hiz(inclined_road(h-1))))^2;
total= @(hiz) WR(h-1)+WL(hiz)+WS(h-1);
total_loss= @(hiz) total(hiz)*5;
hiz_fun=@(hiz) (1/2)*1750*hiz(inclined_road (h-1))^2-total_loss(hiz)-(1/2)*1750*hiz.^2;
x1 = [-100 100];
for k1 = 1:numel(x1);
hiz_along_route(h,k1) = fzero(hiz_fun, x1(k1));
hiz_along_route(h,2)=hiz(inclined_road(h));
end
end
end
end
I am waiting your helps,
Thx

Best Answer

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.