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

1 view (last 30 days)
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?

Accepted Answer

John D'Errico
John D'Errico on 23 Oct 2015
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;

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!