MATLAB
10. Other Commands
maximum
You are aware that the MATLAB commands max, min find the maximum and minimum elements in an array (and their locations) while fminbnd helps to find the minimum value of a function defined in a function M-file. Seeing there is no such command as fmaxbnd, how can we print out the maximum value of a function f(x), for a ≤ x ≤ b, without using the zoom on facility? For example, find the x and y coordinates of the maximum turning point of y = xe−x cos x, 0 ≤ x ≤ π.
Using MATLAB
The value of x where f(x) has a maximum is the same as the value where −f(x) is a minimum. Hence, set up the function M-file negf.m
function y=negf(x)
y= - x*exp(-x)*cos(x);
and run the following M-file
clear all
[temp1,temp2]=fminbnd(@negf,0,pi);
xmax=temp1, ymax= -temp2 % gives xmax=0.5959, ymax=0.2718
Using “Maple”
Solve for where the derivative is zero and then substitute back into y = f(x). However, be aware that there might be other (real or complex) solutions to the equation df/dx = 0.
clear all
syms x
f=x*exp(-x)*cos(x);
xmax=double(solve(diff(f))) % gives xmax=0.5959
x=xmax;
ymax=eval(f) % gives ymax=0.2718