Looping until threshold is met

6 ビュー (過去 30 日間)
AD
AD 2021 年 1 月 20 日
編集済み: AD 2021 年 5 月 18 日
I

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 1 月 20 日
hello
yes - error is not initialized , and BTW, you are shadowing a native matlab function error , which is not good pratice (could led to conflicts or wrong output)
also , your while loop will never stop because the error is constant !
the output of your function is always 24
you can put a breakpoint where you compute I = h/3*(y(1) +even_sum + odd_sum + y(end));
even though each individual term on the RHS is evolving , the result remains exactly the same.
I checked the code of your 1/3 simpson rule and it's correct IMHO
in other words , you have demonstrated that doing this integral computation with always refined x vector does not change the integral value
  1 件のコメント
AD
AD 2021 年 1 月 20 日
Thanks Mathieu!

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

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 20 日
You have to initialize "error" before the loop. The initial value must be less than a threshold value as defined. Also make sure that the error value is continuously increasing as the iteration progress, so that it definitely break loop condition.
  2 件のコメント
AD
AD 2021 年 1 月 20 日
Thanks Kalyan! I've initalized the error as you've suggested. How would you go about making sure that the error value is continuously increasing as the iteration progress?
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 20 日
Just make sure it will, otherwise, while loop runs continuously. You may check the code with sample 2/3 iteration manually.
error=abs(av_temp_simp13(iter)-av_temp_matlab);

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by