MatLab is needed Here is the simpson code function val = simpson(func,a,b,n) % V
ID: 663136 • Letter: M
Question
MatLab is needed
Here is the simpson code
function val = simpson(func,a,b,n)
% VAL = simpson(FUNC,A,B,N)
%
% VAL is the approximation of int_a^b func(x).dx using Simpson's rule
% FUNC -- function to integrate
% A -- lower limit of integration interval
% B -- upper limit of integration interval
% N -- number of sub-intervals (= (number of evaluation points)/2)
%
% FUNC must be able to compute a vector of values given a vector of
% inputs (for efficiency)
% val = (b-a)/6*(feval(func,a)+4*feval(func,(a+b)/2)+feval(func,b));
val = feval(func,a)+feval(func,b);
h = (b-a)/n;
xs = ((0:(n-1))+0.5)*h + a;
for i = 1:length(xs)
val = val + 4*sum(feval(func,xs(i)));
end
xs = (1:(n-1))*h + a;
for i = 1:length(xs)
val = val + 2*sum(feval(func,xs(i)));
end
val = val*h/6;
Explanation / Answer
note:
your code is not work that's why I am implementing the code.
function [y,err_est,if_lg,nofun] = adpsim(a,b,tol,fun)
y=0;if_lg=0;jif_lg=0;err_est=0;lev_max=10;
fsave=zeros(lev_max,3);xsave=zeros(lev_max,3);simpr=zeros(lev_max);
tol2=tol+10*eps;
tol1=tol2*15/(b-a);
x=a:(b-a)/4:b;
for j=1:5
f(j)=feval(fun,x(j));
end
nofun=5;
level=1;
while level>0
for k=1:3
fsave(level,k)=f(k+2);
xsave(level,k)=x(k+2);
end
h=(x(5)-x(1))/4;
simpr(level)=(h/3)*(f(3)+4*f(4)+f(5));
if jif_lg<=0
simp1=2*(h/3)*(f(1)+4*f(3)+f(5));
end
simpl=(h/3)*(f(1)+4*f(2)+f(3));
simp2=simpl+simpr(level);
diff=abs(simp1-simp2);
if diff<=tol1*4*h
level=level-1;
jif_lg=0;
y=y+simp1.
y=y+simp2;
7
err_est=err_est+diff/15;
if level<=0
fprintf(