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

2 次查看(过去 30 天)
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?

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by