# max value of a function

16 views (last 30 days)
123456 on 8 Aug 2022
Commented: 123456 on 11 Aug 2022
Hello everyone. I'm having a problem with finding a maximum y of a function. In general I use:
x= double(solve(diff(func)));
ymax=eval(func);
But in some cases this gives me a solution that is clearly not the maximum. Let's say, that the function is:
func = exp(-(x - 1)^2) + exp(-(x + 2)^2)
if i try to use max() i get an error "Input arguments must be convertible to floating-point numbers.".
Please let me know where is the mistake in my thinking.

Matt J on 8 Aug 2022
Edited: Matt J on 8 Aug 2022
In general I use: x= double(solve(diff(func)));
This assumes there are no local min/max or saddle points.
if i try to use max() i get an error "Input arguments must be convertible to floating-point numbers.".
You cannot use max() with symbolic variables.
123456 on 11 Aug 2022
Thank you so much. I will use this solution.

### More Answers (1)

Torsten on 8 Aug 2022
Edited: Torsten on 8 Aug 2022
syms x
func = exp(-(x - 1)^2) + exp(-(x + 2)^2);
dfunc = diff(func,x);
ddfunc = diff(dfunc,x);
xc= double(vpasolve(dfunc==0,[0 2]))
xc = 0.9996
xnum = double(subs(ddfunc,x,xc));
if xnum < 0
disp('Local maximum');
elseif xnum == 0
disp('Unknown type')
else
disp('Local minimum')
end
Local maximum
funcnum = matlabFunction(func);
x = 0:0.01:2;
plot(x,funcnum(x)) 