solving this maximization problem

Hi everyone,
Could anyone assist me with solving this maximization problem in MATLAB? I need to maximize the following function and determine the optimal values of r and t.
Thank you!
-(1/((1 + 2 a)^2))
t (-3 - 2 r + t - 5 a + 2 t a +
2 Sqrt[2 (2 + r - t) + (-2 + 2 r^2 + t)/(1 + a)] +
2 a Sqrt[
2 (2 + r - t) + (-2 + 2 r^2 + t)/(1 + a)]) (-4 r +
2 a (-2 + Sqrt[
2 (2 + r - t) + (-2 + 2 r^2 + t)/(1 + a)]) +
3 (-1 + Sqrt[2 (2 + r - t) + (-2 + 2 r^2 + t)/(1 + a)]))
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

7 个评论

Hi @Fatemeh,

Addressing your query about, “ assist me with solving this maximization problem in MATLAB”, I first defined the symbolic function 'f' in terms of variables 'r' and 't'. It then finds the maximum value of 'f' and the corresponding optimal values of 'r' and 't'. Finally, it converts the symbolic solutions to numeric values and displays the optimal values of 'r' and 't'. Here is updated code,

a = 1; % Assigning a value to 'a'

syms r t; % Define symbolic variables 'r' and 't'

f = -(1/((1 + 2*a)^2)) * t * (-3 - 2*r + t - 5*a + 2*t*a + 2*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a)) + 2*a*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) * (-4*r + 2*a*(-2 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) + 3*(-1 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))));

% Find the maximum value of 'f' with respect to 'r' and 't'

max_f = max(max(f));

% Find the optimal values of 'r' and 't' that maximize 'f'

[sol_r, sol_t] = solve(f == max_f, [r, t]);

% Convert symbolic solutions to numeric values

sol_r = double(sol_r);

sol_t = double(sol_t);

% Display the optimal values of 'r' and 't'

disp(['Optimal value of r: ', num2str(sol_r)]);

disp(['Optimal value of t: ', num2str(sol_t)]);

Please see attached results.

Hope, this helps resolve your problem. Please let me know if you have any further questions.

max() of symbolic does not return anything that is useful in this context.
syms a
syms r t; % Define symbolic variables 'r' and 't'
f = -(1/((1 + 2*a)^2)) * t * (-3 - 2*r + t - 5*a + 2*t*a + 2*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a)) + 2*a*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) * (-4*r + 2*a*(-2 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) + 3*(-1 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))))
f = 
% Find the maximum value of 'f' with respect to 'r' and 't'
max1 = max(f)
max1 = 
max_f = max(max1)
max_f = 
Since f is a scalar, max(f) returns f, and max(max(f)) returns f

@Walter,

@ Raghava S N suggested about function “fmincon“. I did some experimentation with it and I got the following results, what is your opinion about this.

fmincon minimizes your function so you should adjust the output so that going to minimum means you are maximizing. for instance instead of outputing y, you can output (-y) so that minimizing (-y) maximizes y.
@Aquatris, thanks for your prompt response and excellent suggestion. This is what I was expecting.
Maybe of help to get some critical points depending on a:
syms a
syms r t; % Define symbolic variables 'r' and 't'
f = -(1/((1 + 2*a)^2)) * t * (-3 - 2*r + t - 5*a + 2*t*a + 2*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a)) + 2*a*sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) * (-4*r + 2*a*(-2 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))) + 3*(-1 + sqrt(2*(2 + r - t) + (-2 + 2*r^2 + t)/(1 + a))))
f = 
dfdr = diff(f,r);
dfdt = diff(f,t);
sol = solve([dfdr,dfdt],[r,t])
Warning: Possibly spurious solutions.
sol = struct with fields:
r: [16x1 sym] t: [16x1 sym]
sol.r
ans = 
sol.t
ans = 
@Torsten & @Walter,
Thanks for your support, really appreciated.

请先登录,再进行评论。

回答(1 个)

Raghava S N
Raghava S N 2024-7-31
Hi Fatemeh,
Optimization of an equation with a two variable function can be solved using MATLAB’s optimization toolbox. Please refer to the documentation page of the Optimization Toolbox here - https://www.mathworks.com/help/optim/getting-started-with-optimization-toolbox.html.
The workflow to optimize a nonlinear multivariable function is as follows:
Firstly, the equation that has to be optimized must be parameterized to use the Optimization Toolbox routines. The variables undergoing optimization should be the parameters. Please refer to this documentation page that describes how to parameterize functions and equations - https://www.mathworks.com/help/matlab/math/parameterizing-functions.html.
In the next step, the negated value of the parameterized equation should be passed to the function “fmincon”. This function finds the minimum of a constrained nonlinear multivariable function. So, by passing the negated value of the parameterized equation, the maximum is obtained. Please refer to the documentation of “fmincon” here - https://www.mathworks.com/help/optim/ug/fmincon.html
Please refer to two other MATLAB Answers posts that discuss the optimization of a function with two variables in detail. The links for the same are attached below:
Hope this helps!
Raghava

类别

帮助中心File Exchange 中查找有关 Function Creation 的更多信息

提问:

2024-7-31

评论:

2024-7-31

Community Treasure Hunt

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

Start Hunting!

Translated by