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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

John D'Errico
John D'Errico el 23 de Oct. de 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;

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by