Cody

Solution 161861

Submitted on 14 Nov 2012 by @bmtran
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% Simple Trapezoidal Rule p=[2 0 -4 0 -1 1]; a=-2; b=4; f = @(x) polyval(p,x); assert(isequal(trapezoidal_rule(f,a,b,1),5280))
[Warning: File: trapezoidal_rule.m Line: 12 Column: 5 This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB. See Release Notes for MATLAB Version 7.4, "Warning Generated by try-catch" for details.] [> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14] n = 1
2   Pass
%% Composite Trapezoidal Rule for 2 intervals p=[2 0 -4 0 -1 1]; a=-2; b=4; f = @(x) polyval(p,x); assert(isequal(trapezoidal_rule(f,a,b,2),2634))
n = 2
3   Pass
%% Composite Trapezoidal Rule for 4 intervals p=[2 0 -4 0 -1 1]; a=-2; b=4; f = @(x) polyval(p,x); assert(isequal(trapezoidal_rule(f,a,b,4),1516.875))
n = 4
4   Pass
%% Exact analytical comparison p=[2 0 -4 0 -1 1]; a=-2; b=4; f = @(x) polyval(p,x); P=polyint(p); I_correct=polyval(P,b)-polyval(P,a); I=trapezoidal_rule(f,a,b); assert(abs(I-I_correct)<1)
5   Pass
%% Exact analytical comparison--higher tolerance p=[2 0 -4 0 -1 1]; a=-2; b=4; f = @(x) polyval(p,x); I = trapezoidal_rule(f,a,b,1000); P=polyint(p); I_correct=polyval(P,b)-polyval(P,a); assert(abs(trapezoidal_rule(f,a,b,1000)-I_correct)<1e-1)
n = 1000 n = 1000