Cody

# Problem 1031. Composite Trapezoidal Rule for Numeric Integration

Solution 2250703

Submitted on 2 May 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   Pass
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))

h = 6 I = 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))

h = 3 I = 2640

Assertion failed.

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

h = 1.5000 I = 1320 I = 1.3665e+03 I = 1.9305e+03

Assertion failed.

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)

Not enough input arguments. Error in trapezoidal_rule (line 2) h=(b-a)/n 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)

h = 0.0060 I = 5.2800 I = 5.4660 I = 7.7220 I = 18.4560 I = 52.9320 I = 141.0300 I = 334.4460 I = 715.3320 I = 1.4064e+03 I = 2.5823e+03 I = 4.4829e+03 I = 7.4274e+03 I = 1.1830e+04 I = 1.8218e+04 I = 2.7249e+04 I = 3.9734e+04 I = 5.6654e+04 I = 7.9189e+04 I = 1.0874e+05 I = 1.4695e+05 I = 1.9573e+05 I = 2.5732e+05 I = 3.3426e+05 I = 4.2948e+05 I = 5.4630e+05 I = 6.8845e+05 I = 8.6016e+05 I = 1.0662e+06 I = 1.3117e+06 I = 1.6027e+06 I = 1.9455e+06 I = 2.3474e+06 I = 2.8161e+06 I = 3.3604e+06 I = 3.9896e+06 I = 4.7141e+06 I = 5.5450e+06 I = 6.4945e+06 I = 7.5758e+06 I = 8.8031e+06 I = 1.0192e+07 I = 1.1758e+07 I = 1.3520e+07 I = 1.5497e+07 I = 1.7709e+07 I = 2.0179e+07 I = 2.2928e+07 I = 2.5983e+07 I = 2.9370e+07 I = 3.3117e+07 I = 3.7254e+07 I = 4.1813e+07 I = 4.6828e+07 I = 5.2334e+07 I = 5.8370e+07 I = 6.4974e+07 I = 7.2190e+07 I = 8.0062e+07 I = 8.8636e+07 I = 9.7962e+07 I = 1.0809e+08 I = 1.1908e+08 I = 1.3098e+08 I = 1.4386e+08 I = 1.5778e+08 I = 1.7280e+08 I = 1.8899e+08 I = 2.0643e+08 I = 2.2519e+08 I = 2.4535e+08 I = 2.6700e+08 I = 2.9021e+08 I = 3.1507e+08 I = 3.4169e+08 I = 3.7016e+08 I = 4.0057e+08 I = 4.3304e+08 I = 4.6768e+08 I = 5.0459e+08 I = 5.4390e+08 I = 5.8573e+08 I = 6.3021e+08 I = 6.7746e+08 I = 7.2763e+08 I = 7.8086e+08 I = 8.3730e+08 I = 8.9709e+08 I = 9.6040e+08 I = 1.0274e+09 I = 1.0982e+09 I = 1.1731e+09 I = 1.2522e+09 I = 1.3356e+09 I = 1.4237e+09 I = 1.5165e+09 I = 1.6143e+09 I = 1.7174e+09 I = 1.8258e+09 I = 1.9399e+09 I = 2.0599e+09 I = 2.1860e+09 I = 2.3184e+09 I = 2.4575e+09 I = 2.6035e+09 I = 2.7566e+09 I = 2.9172e+09 I = 3.0855e+09 I = 3.2618e+09 I = 3.4464e+09 I = 3.6396e+09 I = 3.8418e+09 I = 4.0532e+09 I = 4.2743e+09 I = 4.5053e+09 I = 4.7466e+09 I = 4.9986e+09 I = 5.2617e+09 I = 5.5362e+09 I = 5.8225e+09 I = 6.1210e+09 I = 6.4322e+09 I = 6.7565e+09 I = 7.0943e+09 I = 7.4461e+09 I = 7.8122e+09 I = 8.1933e+09 I = 8.5897e+09 I = 9.0020e+09 I = 9.4306e+09 I = 9.8761e+09 I = 1.0339e+10 I = 1.0820e+10 I = 1.1319e+10 I = 1.1838e+10 I = 1.2376e+10 I = 1.2934e+10 I = 1.3513e+10 I = 1.4113e+10 I = 1.4736e+10 I = 1.5381e+10 I = 1.6050e+10 I = 1.6743e+10 I = 1.7460e+10 I = 1.8203e+10 I = 1.8972e+10 I = 1.9768e+10 I = 2.0592e+10 I = 2.1444e+10 I = 2.2325e+10 I = 2.3236e+10 I = 2.4178e+10 I = 2.5152e+10 I = 2.6158e+10 I = 2.7197e+10 I = 2.8271e+10 I = 2.9379e+10 I = 3.0524e+10 I = 3.1705e+10 I = 3.2925e+10 I = 3.4183e+10 I = 3.5481e+10 I = 3.6820e+10 I = 3.8200e+10 I = 3.9624e+10 I = 4.1091e+10 I = 4.2604e+10 I = 4.4162e+10 I = 4.5768e+10 I = 4.7422e+10 I = 4.9126e+10 I = 5.0881e+10 I = 5.2687e+10 I = 5.4546e+10 I = 5.6460e+10 I = 5.8430e+10 I = 6.0456e+10 I = 6.2541e+10 I = 6.4685e+10 I = 6.6890e+10 I = 6.9157e+10 I = 7.1488e+10 I = 7.3884e+10 I = 7.6347e+10 I = 7.8878e+10 I = 8.1478e+10 I = 8.4149e+10 I = 8.6893e+10 I = 8.9711e+10 I = 9.2605e+10 I = 9.5576e+10 I = 9.8626e+10 I = 1.0176e+11 I = 1.0497e+11 I = 1.0827e+11 I = 1.1165e+11 I = 1.1512e+11 I = 1.1868e+11 I = 1.2233e+11 I = 1.2608e+11 I = 1.2992e+11 I = 1.3386e+11 I = 1.3789e+11 I = 1.4203e+11 I = 1.4627e+11 I = 1.5061e+11 I = 1.5506e+11 I = 1.5962e+11 I = 1.6429e+11 I = 1.6908e+11 I = 1.7398e+11 I = 1.7900e+11 I = 1.8414e+11 I = 1.8940e+11 I = 1.9478e+11 I = 2.0030e+11 I = 2.0594e+11 I = 2.1171e+11 I = 2.1762e+11 I = 2.2367e+11 I = 2.2985e+11 I = 2.3618e+11 I = 2.4265e+11 I = 2.4926e+11 I = 2.5603e+11 I = 2.6295e+11 I = 2.7002e+11 I = 2.7726e+11 I = 2.8465e+11 I = 2.9221e+11 I = 2.9993e+11 I = 3.0782e+11 I = 3.1589e+11 I = 3.2413e+11 I = 3.3255e+11 I = 3.4115e+11 I = 3.4993e+11 I = 3.5890e+11 I = 3.6807e+11 I = 3.7743e+11 I = 3.8698e+11 I = 3.9674e+11 I = 4.0670e+11 I = 4.1686e+11 I = 4.2724e+11 I = 4.3783e+11 I = 4.4864e+11 I = 4.5968e+11 I = 4.7093e+11 I = 4.8242e+11 I = 4.9414e+11 I = 5.0609e+11 I = 5.1829e+11 I = 5.3072e+11 I = 5.4341e+11 I = 5.5635e+11 I = 5.6954e+11 I = 5.8300e+11 I = 5.9671e+11 I = 6.1070e+11 I = 6.2496e+11 I = 6.3949e+11 I = 6.5430e+11 I = 6.6940e+11 I = 6.8479e+11 I = 7.0047e+11 I = 7.1645e+11 I = 7.3273e+11 I = 7.4932e+11 I = 7.6623e+11 I = 7.8344e+11 I = 8.0098e+11 I = 8.1885e+11 I = 8.3705e+11 I = 8.5558e+11 I = 8.7445e+11 I = 8.9367e+11 I = 9.1324e+11 I = 9.3316e+11 I = 9.5345e+11 I = 9.7410e+11 I = 9.9512e+11 I = 1.0165e+12 I = 1.0383e+12 I = 1.0605e+12 I = 1.0830e+12 I = 1.1060e+12 I = 1.1294e+12 I = 1.1531e+12 I = 1.1773e+12 I = 1.2019e+12 I = 1.2270e+12 I = 1.2525e+12 I = 1.2784e+12 I = 1.3047e+12 I = 1.3315e+12 I = 1.3588e+12 I = 1.3865e+12 I = 1.4147e+12 I = 1.4434e+12 I = 1.4726e+12 I = 1.5022e+12 I = 1.5324e+12 I = 1.5630e+12 I = 1.5942e+12 I = 1.6258e+12 I = 1.6580e+12 I = 1.6908e+12 I = 1.7240e+12 I = 1.7578e+12 I = 1.7922e+12 I = 1.8271e+12 I = 1.8626e+12 I = 1.8986e+12 I = 1.9352e+12 I = 1.9725e+12 I = 2.0103e+12 I = 2.0487e+12 I = 2.0877e+12 I = 2.1273e+12 I = 2.1676e+12 I = 2.2085e+12 I = 2.2500e+12 I = 2.2922e+12 I = 2.3351e+12 I = 2.3786e+12 I = 2.4228e+12 I = 2.4676e+12 I = 2.5132e+12 I = 2.5594e+12 I = 2.6064e+12 I = 2.6541e+12 I = 2.7025e+12 I = 2.7516e+12 I = 2.8015e+12 I = 2.8521e+12 I = 2.9035e+12 I = 2.9557e+12 I = 3.0086e+12 I = 3.0623e+12 I = 3.1169e+12 I = 3.1722e+12 I = 3.2283e+12 I = 3.2853e+12 I = 3.3431e+12 I = 3.4018e+12 I = 3.4613e+12 I = 3.5216e+12 I = 3.5829e+12 I = 3.6450e+12 I = 3.7080e+12 I = 3.7720e+12 I = 3.8368e+12 I = 3.9026e+12 I = 3.9693e+12 I = 4.0369e+12 I = 4.1056e+12 I = 4.1751e+12 I = 4.2457e+12 I = 4.3173e+12 I = 4.3898e+12 I = 4.4634e+12 I = 4.5380e+12 I = 4.6136e+12 I = 4.6903e+12 I = 4.7680e+12 I = 4.8469e+12 I = 4.9267e+12 I = 5.0077e+12 I = 5.0898e+12 I = 5.1730e+12 I = 5.2574e+12 I = 5.3429e+12 I = 5.4295e+12 I = 5.5173e+12 I = 5.6063e+12 I = 5.6965e+12 I = 5.7879e+12 I = 5.8805e+12 I = 5.9743e+12 I = 6.0694e+12 I = 6.1657e+12 I = 6.2633e+12 I = 6.3622e+12 I = 6.4624e+12 I = 6.5639e+12 I = 6.6668e+12 I = 6.7709e+12 I = 6.8764e+12 I = 6.9833e+12 I = 7.0916e+12 I = 7.2013e+12 I = 7.3123e+12 I = 7.4248e+12 I = 7.5388e+12 I = 7.6542e+12 I = 7.7710e+12 I = 7.8893e+12 I = 8.0092e+12 I = 8.1305e+12 I = 8.2534e+12 I = 8.3778e+12 I = 8.5038e+12 I = 8.6314e+12 I = 8.7605e+12 I = 8.8913e+12 I = 9.0236e+12 I = 9.1577e+12 I = 9.2933e+12 I = 9.4307e+12 I = 9.5697e+12 I = 9.7104e+12 I = 9.8529e+12 I = 9.9971e+12 I = 1.0143e+13 I = 1.0291e+13 I = 1.0440e+13 I = 1.0592e+13 I = 1.0745e+13 I = 1.0900e+13 I = 1.1056e+13 I = 1.1215e+13 I = 1.1376e+13 I = 1.1538e+13 I = 1.1703e+13 I = 1.1869e+13 I = 1.2037e+13 I = 1.2208e+13 I = 1.2380e+13 I = 1.2554e+13 I = 1.2731e+13 I = 1.2909e+13 I = 1.3090e+13 I = 1.3273e+13 I = 1.3457e+13 I = 1.3644e+13 I = 1.3833e+13 I = 1.4025e+13 I = 1.4218e+13 I = 1.4414e+13 I = 1.4612e+13 I = 1.4812e+13 I = 1.5014e+13 I = 1.5219e+13 I = 1.5426e+13 I = 1.5635e+13 I = 1.5847e+13 I = 1.6061e+13 I = 1.6278e+13 I = 1.6497e+13 I = 1.6718e+13 I = 1.6942e+13 I = 1.7169e+13 I = 1.7397e+13 I = 1.7629e+13 I = 1.7863e+13 I = 1.8099e+13 I = 1.8339e+13 I = 1.8580e+13 I = 1.8825e+13 I = 1.9072e+13 I = 1.9322e+13 I = 1.9575e+13 I = 1.9830e+13 I = 2.0088e+13 I = 2.0349e+13 I = 2.0613e+13 I = 2.0879e+13 I = 2.1148e+13 I = 2.1421e+13 I = 2.1696e+13 I = 2.1974e+13 I = 2.2255e+13 I = 2.2539e+13 I = 2.2826e+13 I = 2.3117e+13 I = 2.3410e+13 I = 2....

Assertion failed.

### Community Treasure Hunt

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

Start Hunting!