Trapezoidal Rule Approximation of Integral and Function Use
Show older comments
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!
Answers (1)
Categories
Find more on Calculus in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!