The code is provided below, I need to derive expressions for Q1 and Q2 based on
ID: 2984289 • Letter: T
Question
The code is provided below, I need to derive expressions for Q1 and Q2 based on midpoint and trapezoid rules, Please Help
CODE:
function [Q, fcount] = quad_adaptive(F,a,b,tol)
% Evaluate definite integral numerically using adaptive quadrature.
% The first argument, F, is a function handle or an anonymous function
% that defines F(x).
% a and b specify the range of integration
% tol gives the tolerance to compute integral to
% The output fcount gives the number of function evaluations
% Initialization
c = (a + b)/2;
fa = F(a);
fc = F(c);
fb = F(b);
% nodes is a vector that stores the values at which the function is evaluated
% The size of nodes must be equal to the value of fcount
nodes = [a b c];
% Recursive call
[Q, k, nodes] = quad_adaptive_step(F, a, b, tol, fa, fc, fb, nodes);
fcount = k + 3;
nodes = sort(nodes);
% Uncomment the lines below for the abscissa plot
% plot(nodes, F(nodes), 'o', 'Markersize', 2, a:(b-a)/(length(nodes)*10):b, F(a:(b-a)/(length(nodes)*10):b))
% ---------------------------------------------------------
function [Q,fcount,nodes] = quad_adaptive_step(F,a,b,tol,fa,fc,fb,nodes)
h = b - a;
c = (a + b)/2;
fd = F((a+c)/2);
fe = F((c+b)/2);
% Simpson's rule
% For the HW problem, you need to modify Q1 and Q2
Q1 = h/6 * (fa + 4*fc + fb);
Q2 = h/12 * (fa + 4*fd + 2*fc + 4*fe + fb);
% Append the new points at which function is evaluated to nodes
nodes = [nodes (a+c)/2 (c+b)/2];
if abs(Q2 - Q1) <= tol
% This approximate Q will be different in case of the HW problem
Q = Q2 + (Q2 - Q1)/15;
fcount = 2;
else
[Qa,ka,nodes] = quad_adaptive_step(F, a, c, tol, fa, fd, fc, nodes);
[Qb,kb,nodes] = quad_adaptive_step(F, c, b, tol, fc, fe, fb, nodes);
Q = Qa + Qb;
fcount = ka + kb + 2;
end
Explanation / Answer
trepezoidal rule = 1/2*((a+b)+2(a1+a2+a3.............)
simpson rule = 3/8*((a+b)+2(sum of odd terms)+3(sum of even terms)
use above formulas to fix your code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.