MATLAB: Should the max function and the fminbnd function produce the same result? Also, is there something wrong with the MATLAB syntax

syntax

this is my code guys. my height.m file is: function [h] = height(t)
h = @(t) ((-9.8).*(2.^(-1))).*(t.^2) + 125.*t + 500;
%h(h<0) = 0; %I DON'T UNDERSTAND WHY THIS DOESN'T WORK. if h(t) <0 h=0 end end
My actual program is: %Part A clear all; clc; t = 0:.01:30; h = height(t); %the h(h<0) is acting weird
%Part C (maxima)—I swapped the order for the plotting %max function [xpsudeomax,ymax] = max(h(t)); xrealmax = t(ymax); maxfunction = [xrealmax,ymax] %THIS IS LESS ACCURATE THAN THE BELOW VERSION-WHY?
%fminbnd function [xmax2,ymaxpsudeo]= fminbnd(@(t) (-1.*h(t)),0,30); yrealmax = -1.*ymaxpsudeo; format long g fminbndmax = [xmax2,yrealmax] %Is fminbnd and max function suppposed to give the %exact same answer? %THIS IS THE ACCURATE ONE-WHY?

Best Answer

NO. max is not an optimization tool. It just finds the maximum element of an array. VERY different.
Anyway, fminbnd is a MINIMIZER.
Finally, as you have written it, h is a function handle. You cannot then do a vectorized array operation on it, like this:
h(h<0) = 0;