フィルターのクリア

Help with trapezoidal rule while incrementing step size please help!

3 ビュー (過去 30 日間)
Thomas MacDowell
Thomas MacDowell 2018 年 7 月 16 日
I have my function for the trapezoidal rule written and works fine, but I want to add a while loop to increment the step size in order to find out how many terms it takes to find an acceptable answer. here is my code so far:
function answer = trapIntegration(a,b)
x = a;
fun = @(x) exp(x).*sin(x);
s = fun(a);
realAns = integral(fun,a,b);
errMax = 0.00001;
answer = 0;
n = 2;
while abs(realAns-answer) >= errMax
stepsize = (b-a)/n;
for i=1:n-1
x = x + stepsize;
s = s + 2*fun(x);
end
s = s + fun(b);
answer = (b-a)*s/(2*n);
n = n+1;
end
terms = n
This is the output I am getting:
>> trapIntegration(0,pi)
terms =
233
ans =
NaN
Please help!!

回答 (0 件)

カテゴリ

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