필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by