MATLAB code to find the maximum of a function over an interval

182 ビュー (過去 30 日間)
Liam Bouchereau
Liam Bouchereau 2020 年 10 月 27 日
編集済み: Salman Zaheer 2023 年 3 月 23 日
Could someone please help with writing a code to solve the following?
Write a MATLAB code that will find the maximum of the following function over the interval x1 < x < x2 :
f(x) = cos(4x) sin(10x) e^-2x
Find the value of f(x) which corresponds to the maximum over the given interval and print it out
using: fprintf('%.4f',fmax).
For example:
TestResult
x1 = 0.7;
x2 = 0.9;
Result
-0.0611

回答 (2 件)

Rohit Pappu
Rohit Pappu 2020 年 10 月 29 日
編集済み: Rohit Pappu 2020 年 10 月 29 日
MATLAB doesn't have any functions which can explicit calculate the local maxima. A possible workaround would be to find the minima of -f(x)in the same interval.
Code for calculating maxima
x1 = 0.7;
x2 = 0.9;
[x_max,val] = fminbnd(@(x) -cos(4*x)*sin(10*x)*exp(-2*x),x1,x2); %%x_max is the maxima of f(x) and val is the value of f(x_max)
val = -val; %% Negate the value because -f(x) was the function worked upon
Additional documentation can be found here

Salman Zaheer
Salman Zaheer 2023 年 3 月 23 日
編集済み: Salman Zaheer 2023 年 3 月 23 日
How to get max|| Tx-Sx|| of two functions in interval [-1,1] where Tx= x if x belongs to [-1,0) and Tx= -x if x belongs to [0,1]. Similarly for S any function.

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by