The maximum value assumed by the function on the interval

回答 (2 件)

James Browne
James Browne 2019 年 6 月 12 日

1 投票

Greetings,
I wrote an example for you which finds the maximum value of f(x) over the specified interval and plots the resuts. Here you go, hope this helps!
%Define the interval over which f(x) will be evaluated
xInterval = [0 1];
%specify resolution of the evaluation
dx = 0.0001;
%Create a vector of x values based on the interval of evaluateion and
%resolution specifications
x = xInterval(1): dx : xInterval(2);
%Calculate f(x) values
for i = 1:length(x)
fx(i) = sin(5*cos(x(i)))*cos(5*sin(x(i)));
end
%Find the magnitude and index of the maximum f(x) value and print the
%maximum magnitude to the command window
[M,idx] = max(fx);
fprintf('The maximum value in the given range of x is: %5.4f\n',M)
%Use the index of the maximum f(x) value to find the x value which produced
%the maximum f(x) value
xForMaxFx = x(idx);
%Determine title based on interval of evaluation parameters
titleName = strcat('Maximum f(x) Value over the interval [',num2str(xInterval(1)),',',num2str(xInterval(2)),']');
%Plot f(x) and highlight the maximum value
plot(x,fx,xForMaxFx,M,'kd')
title(titleName)
xlabel('x (units)')
ylabel('f(x) (units)')
ylim([-1,1])
legend('f(x)','Maximum f(x) value','location','southeast')

5 件のコメント

Aidan Welch
Aidan Welch 2021 年 4 月 7 日
Just FYI this code does not work generally. It only worked because the original function had no powers. When you attempt this with a polynomial, it reads as incorreect implementation of matrix powers since x is a vector. Not sure how to fix this without fundementally re-approaching how this code is structured.
Walter Roberson
Walter Roberson 2021 年 4 月 8 日
Which part of the code would fail with polynomials? The user posted
for i = 1:length(x)
fx(i) = sin(5*cos(x(i)))*cos(5*sin(x(i)));
end
which shows evaluating the function scalar by scalar, and scalar^power is the same as scalar.^power
There would be a potential issue if the function evaluation was vectorized, such as
fx = sin(5*cos(x))*cos(5*sin(x)); %WOULD FAIL
and would need to be written
fx = sin(5*cos(x)).*cos(5*sin(x)); %WOULD FAIL
but the issue for ^ needing to be .^ in vector calculations would not be different than the issue for * needing to be .* in vector calculations.
Aidan Welch
Aidan Welch 2021 年 4 月 8 日
The line which defines the function fails under power conditions and under dot power conditions. It fails on 2020a with a polynomial via the aforementioned error with the default power function. When the dot power function is applied, I get an error concerning "incompatible Matrix sizes"
Walter Roberson
Walter Roberson 2021 年 4 月 8 日
The line that defines the function? You mean
fx(i) = sin(5*cos(x(i)))*cos(5*sin(x(i)));
??
But that line only uses scalar values.
Example of the function changed to use ^ without using .^
No failure.
%Define the interval over which f(x) will be evaluated
xInterval = [0 1];
%specify resolution of the evaluation
dx = 0.0001;
%Create a vector of x values based on the interval of evaluateion and
%resolution specifications
x = xInterval(1): dx : xInterval(2);
%Calculate f(x) values
for i = 1:length(x)
fx(i) = sin(5*cos(x(i)))^2*cos(5*sin(x(i)))^3;
end
%Find the magnitude and index of the maximum f(x) value and print the
%maximum magnitude to the command window
[M,idx] = max(fx);
fprintf('The maximum value in the given range of x is: %5.4f\n',M)
The maximum value in the given range of x is: 0.9195
%Use the index of the maximum f(x) value to find the x value which produced
%the maximum f(x) value
xForMaxFx = x(idx);
%Determine title based on interval of evaluation parameters
titleName = strcat('Maximum f(x) Value over the interval [',num2str(xInterval(1)),',',num2str(xInterval(2)),']');
%Plot f(x) and highlight the maximum value
plot(x,fx,xForMaxFx,M,'kd')
title(titleName)
xlabel('x (units)')
ylabel('f(x) (units)')
ylim([-1,1])
legend('f(x)','Maximum f(x) value','location','southeast')

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

Walter Roberson
Walter Roberson 2019 年 6 月 12 日

0 投票

Step 1: Plot the function to see visually the approximate peak. Find an approximate interval for the peak; it does not have to be precise at all.
Step 2: Differentiate the function. Solve for a zero of that over the approximate interval that you identified. That gives the location of the peak.
Step 3: substitute the location of the peak into the formula to get the value of the peak.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2019 年 6 月 12 日

コメント済み:

2021 年 4 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by