Question: explain the codes below(which are the answer to the question) and tell their role stepwise(like a report) regarding the question. format long clear all f
explain the codes below(which are the answer to the question) and tell their role stepwise(like a report)
regarding the question.
format long
clear all
f = @(x) sin (x);
integrator(f,[0 1],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)1)
fprintf('Integral is: %f ',answerS)
fprintf('Total number of iterations taken: %d ',cnt)
return
elseif(cnt>100)
fprintf('Recursion depth More than 100!!! ')
fprintf('Integral is: %f ',answerS)
return
else
time = toc;
if(time >= 5*60)
fprintf('Compplex function!!!! Execution time more than 5 minutes !!!!')
return
end
func(answerS,f,t,h/2,2*n,cnt) %Recurssion
end
end
return
end
General Integrator Develop a general purpose Matlab integrator that will integrate an arbitrary bounded function on an arbitrary interval. Your function should be called as follows: >>integrator('sin(x)', [01], 1e-6) where the third argument as an upper bound on the absolute error. The last argument should be optional (with le-6 being the default). The main challenge in this project is error control since you don't know the true answer So start the following by letting h=0.01 and using Simpson's rule. Then proceed to divide h by 2. When the answer states changing by less than 0.1*(requested error), consider that is the area of the answer. If that never starts happening, which means that the input function is too complex, display an error. Keep track of how long the previous step took and if the following step would take more than 5 minute prompt the user on whether to proceed. Save time by subdividing the segment into 2 halves and working with cach half separately. Make your function recursive (you will need to write a sub-function), but don't allow the recursion depth to exceed more than 100
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
