How to find the maximum of a 2d function in specific interval?
65 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Dyuman Joshi
2023-10-27
移动:Dyuman Joshi
2023-10-27
Finding an extremum of a function in a bounded interval requires the use of numerical methods -
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
In order to maximize P, we will minimize -P
%% Convert the symbolic expression into a anonymous function
%% of a single variable
fun = matlabFunction(-P, "Vars", {[x, y]})
%Bounds on the independent variables
lb = [-1; -1];
ub = [1; 1];
%Initial point to start the numerical process of finding the minima
%via iterative methods
xy = [0 0];
As we are dealing with a constralined non-linear multivariable function, fmincon is the best option. Note that fmincon() requiers Optimization toolbox.
[xy_max, fun_min] = fmincon(fun, xy, [], [], [], [], lb, ub, [])
%The corresponding maximum value
fun_max = -fun_min
0 个评论
更多回答(2 个)
recent works
2023-10-27
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 个评论
Walter Roberson
2023-10-29
编辑:Walter Roberson
2023-10-29
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.
Walter Roberson
2023-10-27
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!