Trapezoidal Rule Approximation of Integral and Function Use

1 view (last 30 days)
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)

Jan
Jan on 6 Mar 2013
How do you start the function? What are a and b?
The actual call to the local function f() seems to be ok.
Did you save the function before running it?
  2 Comments
Paul
Paul on 7 Mar 2013
Edited: Paul on 7 Mar 2013
Hi Jan, Thanks for the response. I start the function by typing "Trapezoidal" (my function file name) in the Command Window. I also tried setting a and b from the window like this: "Trapezoidal; a = 0; b = 1;" It unfortunately gave the same error. Do I need to create script that calls the function? I'm not sure how to do this. Also, my function is saved as a .m file, would that be correct? Sorry, I'm new to MatLab programming.
Yes, I believe the file was properly saved before running the code.
Thanks for any more advice, Jan (or others)!!
EDIT: Okay, I did some research and made progress (whoo!). In the command window I entered in the "Trapezoidal()" with arguemnts, duh! Seems obvious now, but in any case, if I enter "Trapezoidal(0,1,2,1)" it approximates my function for case 1, accurately!
Paul
Paul on 8 Mar 2013
if true
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;
case 3
f_value = sqrt(1 + (cos(x)).^2);
case 4
sigma = 0;
mu = 0;
f_value = (1/(sigma*(sqrt(2*pi)))*exp((-0.5)*((x-mu)/sigma)^2));
end
end
end
end
When I run the following command in the command window: Trapezoidal(-1,1,2,4); It returns: Error in Trapezoidal>f (line 32) switch index
Output argument "f_value" (and maybe others) not assigned during call to "C:\Users\Paul\Documents\MATLAB\Trapezoidal.m>Trapezoidal/f".
Error in Trapezoidal (line 7) sumend = (f(a,index_f) +f(b,index_f))./2;
Any advice would be greatly appreciated.

Sign in to comment.

Categories

Find more on Function Creation 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!