Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to display my answer with out rewriting them?

1 回表示 (過去 30 日間)
Nicholas Deosaran
Nicholas Deosaran 2020 年 10 月 18 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello all,
I have my code below that finds the min_max of my function.
I would like for it to displapy all the min_max.
When i run the code it only does the last min_max.
How would I stop it from overwiting the previous min_max?
x = 0:0.1:10;
y = x.^(101/100)+4*cos((3*pi*x)/4)-2*sin((2*pi*x)/3)-0.25;
function minmax_ind = min_max(y)
for i = 2:(length(y)-1)
if (y(i-1) > y(i)) && (y(i+1) > y(i)) % if this is true, we have found a local minimum
minmax_ind = i;
elseif (y(i-1) < y(i)) && (y(i+1) < y(i)) % if this is true, we have found a local maximum
minmax_ind = i;
end
end
end
Thank you,
Nick

回答 (1 件)

Robin Kirsch
Robin Kirsch 2020 年 10 月 18 日
編集済み: Robin Kirsch 2020 年 10 月 18 日
You are overwriting minmax_ind every time. If you find a local max, you will overwrite it if you find another one. I hope this is what you meant, I did not test the function due to it being correct by finding the min and max of the array.
You can fix this if u add another variable just like this
function minmax_ind = min_max(y)
j = 1;
for i = 2:(length(y)-1)
if (y(i-1) > y(i)) && (y(i+1) > y(i)) % if this is true, we have found a local minimum
minmax_ind(j) = i;
j = j +1;
elseif (y(i-1) < y(i)) && (y(i+1) < y(i)) % if this is true, we have found a local maximum
minmax_ind(j) = i;
j = j +1;
end
end
end
  1 件のコメント
Nicholas Deosaran
Nicholas Deosaran 2020 年 10 月 18 日
Hey Robin,
Thank you for reply!
I ran the code with your fix and it's give me differnt numbers than I had before?

Community Treasure Hunt

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

Start Hunting!

Translated by