Finding largest y value with corresponding x value

5 ビュー (過去 30 日間)
Ryan Elway
Ryan Elway 2017 年 9 月 8 日
回答済み: KSSV 2017 年 9 月 8 日
I'm a beginner at this so this is making no sense. I need a code that steps through x values from xMin and xMax with step size, calculates the value of f(x) and keeps track of the largest y value and the corresponding x value. At the end, use disp() and string() to print findings as such: yMax = 1.23 at x = 4.56. There needs to be a "for loop" and maybe "if" Any help would be appreciated!
  1 件のコメント
CARLOS RIASCOS
CARLOS RIASCOS 2017 年 9 月 8 日
編集済み: CARLOS RIASCOS 2017 年 9 月 8 日
Hi friend, I'm not really sure what you need, but I did this for you, I hope it serves you.
f=@(x) x^2; %f(x)
h=0.5; %step size
domain = 0:h:4; %domain of f(x)
for i=1:length(domain) %loop
y(i)=f(domain(i));
format='current x value:%1.1f; current y value:%1.1f. \n';
%following
fprintf(format,domain(i),y(i));
end
Ymax=max(y); %find xMAX.yMAX
position = find(y==Ymax);
Xmax=domain(position);
format1='the value XMax is::%1.1f; the value YMax is:%1.1f.'; %print xMAX.yMAX
fprintf(format1,Xmax,Ymax);

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

採用された回答

KSSV
KSSV 2017 年 9 月 8 日
YOu asked the same question three times without any effort....By this time if you would have googled you would have got your work done by this time. Check the below pseudo code to get maximum value using loop.
t = linspace(0,2*pi) ;
y = sin(t) ;
figure
hold on
plot(t,y) ;
maxval = y(1);
for ii = 1:length(t)
if y(ii) > maxval
id = ii ;
maxval = y(ii);
end
end
plot(t(id),y(id),'*r')
fprintf('MAx value is:%f\n',maxval) ;
fprintf('max values index is %d:\n',id)
Note: Learn MATLAB, it is easy software. Accepth the previous questions which you asked.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by