Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 161746

Submitted on 13 Nov 2012

Incorrect

9Size
Leading solution size is 30.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Fail
 
%% 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))
Error: Output argument "I" (and maybe others) not assigned during call to "/users/msssystem3/trapezoidal_rule.m>trapezoidal_rule".
2
Fail
 
%% 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))
Error: Output argument "I" (and maybe others) not assigned during call to "/users/msssystem3/trapezoidal_rule.m>trapezoidal_rule".
3
Fail
 
%% 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))
Error: Output argument "I" (and maybe others) not assigned during call to "/users/msssystem3/trapezoidal_rule.m>trapezoidal_rule".
4
Fail
 
%% 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)
Error: Output argument "I" (and maybe others) not assigned during call to "/users/msssystem3/trapezoidal_rule.m>trapezoidal_rule".
5
Fail
 
%% 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)
Error: Output argument "I" (and maybe others) not assigned during call to "/users/msssystem3/trapezoidal_rule.m>trapezoidal_rule".