what's my error in this code? Unrecognized function or variable 'integrator'.

Unrecognized function or variable 'integrator'.
can anyone guide me to solve this error!?
format long
clear all
f = @(x) x^5*exp(-x)*cos(sqrt(x)); %Sample function
integrator(f,[1.008 10.04],1e-6)
%Main function
function integrator(f,t,er)
h=0.01;
n=abs(t(2)-t(1))/h;
cnt = 0;
prev = 0;
func(prev,f,t,h,n,cnt)
function func(prev,f,t,h,n,cnt ) %sub function
tic
cnt = cnt + 1;
%Simpson integration rule
I=(f(t(2))+f(t(1)));
for j = 1:2:n-1
I=I+4*f(t(1)+j*h);
end
for k = 2:2:n-2
I=I+ 2*f(t(1)+k*h);
end
answerS = I*(h/3) ;
if(abs(prev - answerS)<0.1*er && cnt>1)
fprintf('Integral is: %f\n',answerS)
fprintf('Total number of iterations taken: %d\n',cnt)
return
elseif(cnt>100)
fprintf('Recursion depth More than 100!!!\n')
fprintf('Integral is: %f\n',answerS)
return
else
time = toc;
if(time >= 5*60*60)
fprintf('Compplex function!!!! Execution time more than 5 minutes\n!!!!')
return
end
func(answerS,f,t,h/2,2*n,cnt) %Recurssion
end
end
return
end

1 Comment

The code works when I test it (or at least it does not fail in the way you describe)
If you are using R2015a or earlier, then you need to ensure that your function integrator is in a file named integrator.m . If you are using R2015b or later then function integrator can be in the same file as your script that calls it, as long as the script is not named integrator

Sign in to comment.

Answers (0)

Categories

Asked:

on 11 Jan 2022

Commented:

on 11 Jan 2022

Community Treasure Hunt

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

Start Hunting!