How to find the maximum value of a function?

104 ビュー (過去 30 日間)
Josh Belandres
Josh Belandres 2020 年 4 月 22 日
コメント済み: Walter Roberson 2022 年 11 月 13 日
The function is:
f=sin(x)+sin(x*2)
and I want to find the scalar maximum and this is my code as of now.
f = sin(x)+sin(x*2);
f2 = diff(f,x)==0;
x = solve(f2,x);
X = max(x);
However this code gives:
acos(- 33^(1/2)/8 - 1/8)
and I want a numerical value because I need to use the scalar maximum for other calculations. If I procede with this code then "acos(- 33^(1/2)/8 - 1/8)" shows in every following calculation. Please let me know how to convert this or use a different code, thanks.

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 22 日
You are chasing the wrong problem.
x = solve(f2,x);
Those are not solutions to when the function f becomes 0: those are the solutions to when the derivative becomes 0. They are the locations of the inflection points, but they are not the values of the functions. You should be using
syms x
f = sin(x)+sin(x*2);
f2 = diff(f,x)==0;
extreme_points = solve(f2,x);
extreme_values = subs(f, x, extreme_points);
[maxX, maxidx] = max(extreme_values);
best_location = extreme_points(maxidx);
best_value = simplify(maxX, 'steps', 50);
best_value =
((3*2^(1/2) + 66^(1/2))*(33^(1/2) + 15)^(1/2))/32
Chances are that you should not be working with numeric values, as the numeric values are only approximations. You might choose to display a numeric value to make it easier for the user to make sense of the result, but you should compute based upon the full result.
To convert to a numeric approximation, double(best_value)
  2 件のコメント
Josh Belandres
Josh Belandres 2020 年 4 月 22 日
Thank you so much for your response
Kareem Dawood
Kareem Dawood 2022 年 4 月 17 日
Thank you for this contribution man!

サインインしてコメントする。

その他の回答 (2 件)

KurtanWagner
KurtanWagner 2021 年 4 月 26 日
Can you graph this as well?
  2 件のコメント
KurtanWagner
KurtanWagner 2021 年 4 月 26 日
I got this max value, but I would like to see just how big my variable is
Walter Roberson
Walter Roberson 2021 年 4 月 26 日
syms x
f = sin(x)+sin(x*2);
f2 = diff(f,x)==0;
extreme_points = solve(f2,x);
extreme_values = subs(f, x, extreme_points);
[maxX, maxidx] = max(extreme_values);
best_location = extreme_points(maxidx);
best_value = simplify(maxX, 'steps', 50);
fplot(f, [-10 5])
hold on
plot(double(best_location), double(best_value), 'r*');
hold off
.. it is periodic. With a little change to the code you can show that it repeats every 2*pi

サインインしてコメントする。


MANSUR
MANSUR 2022 年 11 月 13 日
syms x
f = sin(x)+sin(x*2);
f2 = diff(f,x)=0;
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.
extreme_points = solve(f2,x);
extreme_values = subs(f, x, extreme_points);
[maxX, maxidx] = max(extreme_values);
best_location = extreme_points(maxidx);
best_value = simplify(maxX, 'steps', 50);
fplot(f, [-10 5])
hold on
plot(double(best_location), double(best_value), 'r*');
hold off
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 11 月 13 日
syms x
f = sin(x)+sin(x*2);
f2 = diff(f,x)==0;
extreme_points = solve(f2,x);
extreme_values = subs(f, x, extreme_points);
[maxX, maxidx] = max(extreme_values);
best_location = extreme_points(maxidx);
best_value = simplify(maxX, 'steps', 50);
fplot(f, [-10 5])
hold on
plot(double(best_location), double(best_value), 'r*');
hold off

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStateflow についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by