Trapezoidal Rule Approximation of Integral and Function Use
이전 댓글 표시
function [integral,difference,ratio]=Trapezoidal(a,b,n0,index_f)
integral = zeros(9,1);
difference = zeros(9,1);
ratio = zeros(9,1);
sumend = (f(a,index_f) +f(b,index_f))/2;
sum = 0;
if(n0 > 2)
h = (b-a)/n0;
for i=2 : 2 : n0-2
sum = sum + f(a+i*h, index_f);
end
end
for i=1:9
n = n0*2^(i-1);
h = (b-a)/n;
for k = 1 : 2 : n-1
sum = sum + f(a+k*h, index_f);
end
integral(i) = h*(sumend + sum);
end
difference(2:9) = integral(2:9) - integral(1:8);
ratio(3:9) = difference(2:8)./difference(3:9);
function f_value = f(x,index)
switch index
case 1
f_value = x.^4;
case 2
f_value = x.^2;
end
end
end
I am receiving the error: Error using Trapezoidal (line 7) Not enough input arguments. This is line 7: %sumend = (f(a,index_f) +f(b,index_f))/2;
Any advice would be useful. thanks!
답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!