Maximum in For Loop

I need to find the maximum value of the last equation in my for loop as well as the iterative value. How would I do this? For example I need to find the maximum value of z and for what value of x.
for x = 0:10
y = 3*x;
z = sqrt(y/2);
end

回答 (1 件)

Image Analyst
Image Analyst 2016 年 10 月 30 日

0 投票

Try this:
zMax = -inf;
xAtZMax = 0;
for x = 0:10
y = 3*x;
z = sqrt(y/2);
if z > zMax
% New max found.
zMax = z;
xAtZMax = x;
end
end
fprintf('The max z is %f that occurs at x = %d\n', zMax, xAtZMax);

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

m13
2016 年 10 月 30 日

回答済み:

2016 年 10 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by