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

Umar
Umar 2024년 7월 31일

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
Umar
Umar 2024년 7월 31일

@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.

Aquatris
Aquatris 2024년 7월 31일
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.
Umar
Umar 2024년 7월 31일
@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 = 
Umar
Umar 2024년 7월 31일
@Torsten & @Walter,
Thanks for your support, really appreciated.

댓글을 달려면 로그인하십시오.

답변 (1개)

Raghava S N
Raghava S N 2024년 7월 31일

0 개 추천

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