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

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

 Accepted 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.

1 Comment

Thank you Jan
I have achieved the problem with using my fzero algorithm created in 2017. Probably, i didn't add some statements because of changing of indices. By using same indices (2017 indices), problem has been solved.
Thanks a lot again.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!