How to find the maximum of a 2d function in specific interval?
Ältere Kommentare anzeigen
I have a 2d function generated from loops, now I want to find the maximum value of that function in specific interval ( -1<(x,y)<1). How it is possible in Matlab?
syms x y
P=0;
for i=1:7
for j=1:7
P= legendreP(i-1,x)*legendreP(j-1,y)+P;
end
end
Akzeptierte Antwort
Weitere Antworten (2)
You can find the maximum value of your 2D function within a specific interval in MATLAB by using the following steps:
- Define the symbolic variables and the function.
- Define the interval of interest.
- Use the fmincon function to find the maximum value within the specified interval.
how you can modify your code to find the maximum value in the interval (-1 < x, y < 1):
syms x y
P = 0;
for i = 1:7
for j = 1:7
P = legendreP(i-1, x) * legendreP(j-1, y) + P;
end
end
% Define the function you want to maximize
f = -P; % Use negative sign to find the maximum
% Define the constraint for the interval (-1 < x, y < 1)
lb = [-1; -1]; % Lower bound for x and y
ub = [1; 1]; % Upper bound for x and y
% Initial guess (optional)
x0 = [0; 0]; % You can choose a different initial guess if needed
% Solve for the maximum within the interval
options = optimset('Display', 'off'); % Turn off display for fmincon
[x_max, f_max] = fmincon(f, x0, [], [], [], [], lb, ub, [], options);
% Display the result
fprintf('Maximum value: %f\n', -f_max); % Use negative sign to get the actual maximum
fprintf('Coordinates of the maximum: x = %f, y = %f\n', x_max(1), x_max(2));
- We define the function you want to maximize as f and use the negative sign to find the maximum (since fmincon minimizes by default).
- We define the constraints for the interval (-1 < x, y < 1) by specifying lower and upper bounds lb and ub.
- We use fmincon to find the maximum value within the specified interval.
- Finally, we display the maximum value and the coordinates where it occurs.
1 Kommentar
Walter Roberson
am 29 Okt. 2023
Bearbeitet: Walter Roberson
am 29 Okt. 2023
Your f is a symbolic expression. fmincon requires the handle to a function (or, more obscurely, a character vector or string scalar that names a public function -- not a local or nested function.)
Also, fmincon never promises to find a global minima, not even when there are not constraints. fmincon finds a local minima that the initial point is in the "basin of attraction" of.
syms x y
P=0;
for i=1:7
for j=1:7
P= legendreP(i-1,x)*legendreP(j-1,y)+P;
end
end
Pe = collect(expand(P), [x y])
partialx = solve(diff(Pe,x),x)
Pe2 = collect(expand(subs(Pe, x, partialx)),y)
partialy = arrayfun(@(EXPR) double(solve(diff(EXPR))), Pe2, 'uniform', 0)
You now have a situation where for each of the 5 symbolic partialx values, you have 5 specific double precision partialy values that are critical points for that particular partial x. You would have to substitute the values back into the specific partialx to get the full x value, and then you would evaluate the function at those x and y values; you would put everything together and determine the maximum.
Your P is a multinomial of degree 6 in each of x and y, so there are 5 x 5 = 25 critical points to examine to determine if they are maxima or minima. But you can skip the ones involving imaginary coefficients.
You can seldom find symbolic solutions for the roots of equations of degree 5 or higher, so you are not likely to be able to get to closed form solutions.
Kategorien
Mehr zu Optimization Toolbox finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


