I'm using the following function to calculate Simpson's Rule, but for the input: recur_simpson(Fun,a,b,TOL,level,level_max) is where I'm confused on how to input a function. The comments say to input a function m-file name in ' '. The function I would like to input is 4/(1+x^2), but I can't just input that as an argument. What would I write in a seperate m-file for this function? Thanks for any help!
Here is program I'm trying to use:
function simpson_result=recur_simpson(Fun,a,b,TOL,level,level_max);%%function simpson_result=recur_simpson(Fun,a,b,TOL,level,level_max);
% Adaptive Simpson's Rule for the integral of Fun on [a,b]
% INPUT
% Fun - a function m-file name in ' '
% a,b - lower and upper limits of integration respectively
% TOL - desired accuracy of the integral
% level - level of recursion (initially start with level=0)
% level_max - maximum depth of recursion
% OUTPUT
% simpson_result - value of the integral
%echo on
level=level+1;h=b-a;c=(b+a)/2;one_simpson=h*(feval(Fun,a)+4*feval(Fun,c)+feval(Fun,b))/6;d=(a+c)/2;e=(c+b)/2;two_simpson=h*(feval(Fun,a)+4*feval(Fun,d)+2*feval(Fun,c)+4*feval(Fun,e)+feval(Fun,b))/12;if level >= level_maxsimpson_result=two_simpson;message='Level Limit Exceeded'elseif abs(two_simpson-one_simpson)<15*TOL;simpson_result=two_simpson+(two_simpson-one_simpson)/15;elseleft_simpson=recur_simpson(Fun,a,c,TOL/2,level,level_max);right_simpson=recur_simpson(Fun,c,b,TOL/2,level,level_max);simpson_result=left_simpson+right_simpson;end;
Best Answer