max value of a function

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.

 Risposta accettata

Matt J
Matt J il 8 Ago 2022
Modificato: Matt J il 8 Ago 2022

1 voto

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.

8 Commenti

123456
123456 il 8 Ago 2022
This assumes there are no local min/max or saddle points.
Do you have any suggestions how can change it to work even in such cases?
You cannot use max() with symbolic variables.
Do you have any suggestions what can I use with symbolic variables?
Matt J
Matt J il 8 Ago 2022
Modificato: Matt J il 8 Ago 2022
It depends on what you can assume about the function. Some functions have an infinite number of local minima and maxima, e.g., sinc(x). You can't write code that will check them all.
123456
123456 il 8 Ago 2022
This function is a sum of several functions (each of them has one peak), so I know how many peaks it can have at most. Does it allow me to calculate the max?
Matt J
Matt J il 8 Ago 2022
Modificato: Matt J il 8 Ago 2022
so I know how many peaks it can have at most.
I don't see how you could. Consider these two functions, each having one peak:
f=@(x) (abs(x)<=1).*(cos(pi*x)+1);
g=@(x) f(x-1);
fplot(f,[-3,3]);hold on
fplot(g,[-3,3]); hold off; axis padded
When we add them together though, we see there are infinite points where the derivative is zero:
fplot(@(x) f(x)+g(x), [-3,3]); axis padded
Matt J
Matt J il 8 Ago 2022
Modificato: Matt J il 8 Ago 2022
Perhaps the last example wasn't persuasive because all maxima were global maxima. Very well. Here are two single-peak functions that sum to give 8 local max, only one of them global:
clear; close all
m=@(x) (1-x.^2).*sin(8*pi*x).^2/40.*(abs(x-0.5)<=0.5);
f0=@(x) (abs(x)<=1).*(cos(pi*x)+1);
f=@(x) f0(x)+m(x);
g=@(x) f0(x-1);
fplot(f,[-3,3]);hold on
fplot(g,[-3,3]); hold off; axis padded
figure;
fplot(@(x) f(x)+g(x), [-3,3]); axis padded
123456
123456 il 9 Ago 2022
I don't see how you could.
I am not a mathematician, forgive me if I'm not able to explain myself clearly. Let me try to explain what I am trying to do here.
This is a script calculating orthographic similarity between two words. For each letter I have a function that has one peak (d is a difference between the position of a specific letter in first word vs its position in the second one):
(exp(-(d-x)^2/3)
Then I sum the functions for each letter, so I get the function, that (as far as I understand) can have maximum as many peaks as the amount of letters. The func that I gave in my question is an example of the sum of functions for 2 specific words, and because of the letter combination it has two peaks. For further calculations I need only the maximum value of y (I don’t need the corresponding x, I don’t need to know how many peaks there is). Is that possible?
Matt J
Matt J il 9 Ago 2022
Modificato: Matt J il 9 Ago 2022
If you can identify a finite search interval [a,b] where the solution lies, you can do something like this:
a=-10; b=+10;
func = @(x) exp(-(x - 1).^2) + exp(-(x + 2).^2); %Note the element-wise .^
X=linspace(a,b,1e4);
Y=func(X);
[~,i0]=max(Y);
[xmax,ymax] = fminbnd(@(z) -func(z), X(i0-1), X(i0+1));
ymax=-ymax;
fplot(func,[a,b]); hold on
plot(xmax,ymax,'or','MarkerSize',10); hold off; axis padded
123456
123456 il 11 Ago 2022
Thank you so much. I will use this solution.

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 8 Ago 2022
Modificato: Torsten il 8 Ago 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))

Richiesto:

il 8 Ago 2022

Commentato:

il 11 Ago 2022

Community Treasure Hunt

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

Start Hunting!

Translated by