Cody

# Problem 43003. Simpsons's rule (but not Homer Simpson)

Solution 2259122

Submitted on 6 May 2020 by Martin C.
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
deltax = pi/12; Fx = cos(linspace(0,pi/2,7)); y_correct = 1.00002631217059; tol = 1e-14; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 7 m1 = 4 0 4 0 4 m2 = 0 2 0 2 0 m = 4 2 4 2 4 v = 1 4 2 4 2 4 1 d = 11.4595 y = 1.0000

2   Pass
deltax = 0.125; Fx = exp((0:8)/8); y_correct = 1.7182841546999; tol = 1e-14; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 9 m1 = 4 0 4 0 4 0 4 m2 = 0 2 0 2 0 2 0 m = 4 2 4 2 4 2 4 v = 1 4 2 4 2 4 2 4 1 d = 41.2388 y = 1.7183

3   Pass
deltax = 1; Fx = (0:10).^2; y_correct = 1000/3; tol = 1e-11; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 11 m1 = 4 0 4 0 4 0 4 0 4 m2 = 0 2 0 2 0 2 0 2 0 m = 4 2 4 2 4 2 4 2 4 v = 1 4 2 4 2 4 2 4 2 4 1 d = 1000 y = 333.3333

4   Pass
deltax = 1; Fx = (0:10).^4; y_correct = 20001.3333333333333; tol = 1e-9; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 11 m1 = 4 0 4 0 4 0 4 0 4 m2 = 0 2 0 2 0 2 0 2 0 m = 4 2 4 2 4 2 4 2 4 v = 1 4 2 4 2 4 2 4 2 4 1 d = 60004 y = 2.0001e+04

5   Pass
deltax = 1; Fx = sin(-5:5); y_correct = 0; tol = 1e-15; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 11 m1 = 4 0 4 0 4 0 4 0 4 m2 = 0 2 0 2 0 2 0 2 0 m = 4 2 4 2 4 2 4 2 4 v = 1 4 2 4 2 4 2 4 2 4 1 d = 4.4409e-16 y = 1.4803e-16

6   Pass
deltax = 0.25; Fx = sin(0:.25:100); y_correct = 0.13768413796203; tol = 1e-15; assert(abs(simpsInt(Fx,deltax) - y_correct) < tol)

le = 401 m1 = Columns 1 through 30 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 31 through 60 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 61 through 90 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 91 through 120 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 121 through 150 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 151 through 180 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 181 through 210 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 211 through 240 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 241 through 270 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 271 through 300 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 301 through 330 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 331 through 360 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 361 through 390 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 4 0 Columns 391 through 399 4 0 4 0 4 0 4 0 4 m2 = Columns 1 through 30 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 31 through 60 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 61 through 90 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 91 through 120 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 121 through 150 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 151 through 180 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 181 through 210 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 211 through 240 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 241 through 270 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 271 through 300 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 301 through 330 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 331 through 360 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 361 through 390 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 0 2 Columns 391 through 399 0 2 0 2 0 2 0 2 0 m = Columns 1 through 30 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 31 through 60 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 61 through 90 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 91 through 120 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 121 through 150 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 151 through 180 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 181 through 210 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 211 through 240 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 241 through 270 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 271 through 300 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 301 through 330 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 331 through 360 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 361 through 390 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 Columns 391 through 399 4 2 4 2 4 2 4 2 4 v = Columns 1 through 30 1 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 31 through 60 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 61 through 90 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 91 through 120 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 121 through 150 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 151 through 180 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 181 through 210 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 Columns 211 through 240 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2 4 2...

7   Pass
deltax = 1; Fx = double((0:10) >= 5); y_correct = 5.66666666666667; tol = 1e-14; abs(simpsInt(Fx,deltax) - y_correct) < tol

le = 11 m1 = 4 0 4 0 4 0 4 0 4 m2 = 0 2 0 2 0 2 0 2 0 m = 4 2 4 2 4 2 4 2 4 v = 1 4 2 4 2 4 2 4 2 4 1 d = 17 y = 5.6667 ans = logical 1

### Community Treasure Hunt

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

Start Hunting!