フィルターのクリア

Simpsons rule function error

4 ビュー (過去 30 日間)
Oliver Wilson
Oliver Wilson 2021 年 3 月 29 日
編集済み: Oliver Wilson 2021 年 4 月 9 日
Ive attempted to code Simpsons Rule
and this my code
with x(1) and x(2) the lower and upper bounds. However for some reason with the following input "S = simpson_rule( @sin, [0,pi], 10 )" i get the same error saying the index exceeds the number of array elements. This problem seems to disappear when i remove f(x(2)) from my code in the final sum. Any suggestions would be great

採用された回答

the cyclist
the cyclist 2021 年 3 月 29 日
In this line of code:
x=x(1)+h*(2*k-1);
you have set x to a scalar value, so when you try to access x(2) in this line of code:
S=h*(f(x(1))+f(x(2))+4*s1+2*s2)/3;
that 2nd element does not exist.
  2 件のコメント
Oliver Wilson
Oliver Wilson 2021 年 3 月 29 日
Thank you very much but im a novice and unsure of how to fix this so any suggestions would be great
the cyclist
the cyclist 2021 年 3 月 29 日
My suggestion would be to comment your code, to state exactly what you intend it to be doing.
That will help other (and you!) understand what is supposed to be happening with the code, and perhaps understand why what you intend is not what the code is actually doing.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by