Cody

Problem 1031. Composite Trapezoidal Rule for Numeric Integration

Solution 3971643

Submitted on 3 Dec 2020
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   Fail
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))

Unrecognized function or variable 'r'. Error in trapezoidal_rule (line 11) h = (b-a)/r; % fill this variable Error in Test1 (line 5) assert(isequal(trapezoidal_rule(f,a,b,1),5280))

2   Fail
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))

Unrecognized function or variable 'r'. Error in trapezoidal_rule (line 11) h = (b-a)/r; % fill this variable Error in Test2 (line 5) assert(isequal(trapezoidal_rule(f,a,b,2),2634))

3   Fail
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))

Unrecognized function or variable 'r'. Error in trapezoidal_rule (line 11) h = (b-a)/r; % fill this variable Error in Test3 (line 5) assert(isequal(trapezoidal_rule(f,a,b,4),1516.875))

4   Fail
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)

Unrecognized function or variable 'r'. Error in trapezoidal_rule (line 11) h = (b-a)/r; % fill this variable Error in Test4 (line 7) I=trapezoidal_rule(f,a,b);

5   Fail
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)

Unrecognized function or variable 'r'. Error in trapezoidal_rule (line 11) h = (b-a)/r; % fill this variable Error in Test5 (line 5) I = trapezoidal_rule(f,a,b,1000);

Community Treasure Hunt

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

Start Hunting!