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 168436

Submitted on 26 Nov 2012 by James

Correct

73Size
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
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))
increment =
     6
x_values =
    -2     4
I =
        5280
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))
increment =
     3
x_values =
    -2     1     4
I =
        2634
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))
increment =
    1.5000
x_values =
   -2.0000   -0.5000    1.0000    2.5000    4.0000
I =
   1.5169e+03
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)
n =
   100
increment =
    0.0600
x_values =
  Columns 1 through 10
   -2.0000   -1.9400   -1.8800   -1.8200   -1.7600   -1.7000   -1.6400   -1.5800   -1.5200   -1.4600
  Columns 11 through 20
   -1.4000   -1.3400   -1.2800   -1.2200   -1.1600   -1.1000   -1.0400   -0.9800   -0.9200   -0.8600
  Columns 21 through 30
   -0.8000   -0.7400   -0.6800   -0.6200   -0.5600   -0.5000   -0.4400   -0.3800   -0.3200   -0.2600
  Columns 31 through 40
   -0.2000   -0.1400   -0.0800   -0.0200    0.0400    0.1000    0.1600    0.2200    0.2800    0.3400
  Columns 41 through 50
    0.4000    0.4600    0.5200    0.5800    0.6400    0.7000    0.7600    0.8200    0.8800    0.9400
  Columns 51 through 60
    1.0000    1.0600    1.1200    1.1800    1.2400    1.3000    1.3600    1.4200    1.4800    1.5400
  Columns 61 through 70
    1.6000    1.6600    1.7200    1.7800    1.8400    1.9000    1.9600    2.0200    2.0800    2.1400
  Columns 71 through 80
    2.2000    2.2600    2.3200    2.3800    2.4400    2.5000    2.5600    2.6200    2.6800    2.7400
  Columns 81 through 90
    2.8000    2.8600    2.9200    2.9800    3.0400    3.1000    3.1600    3.2200    3.2800    3.3400
  Columns 91 through 100
    3.4000    3.4600    3.5200    3.5800    3.6400    3.7000    3.7600    3.8200    3.8800    3.9400
  Column 101
    4.0000
I =
   1.1047e+03
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)
increment =
    0.0060
x_values =
  Columns 1 through 10
   -2.0000   -1.9940   -1.9880   -1.9820   -1.9760   -1.9700   -1.9640   -1.9580   -1.9520   -1.9460
  Columns 11 through 20
   -1.9400   -1.9340   -1.9280   -1.9220   -1.9160   -1.9100   -1.9040   -1.8980   -1.8920   -1.8860
  Columns 21 through 30
   -1.8800   -1.8740   -1.8680   -1.8620   -1.8560   -1.8500   -1.8440   -1.8380   -1.8320   -1.8260
  Columns 31 through 40
   -1.8200   -1.8140   -1.8080   -1.8020   -1.7960   -1.7900   -1.7840   -1.7780   -1.7720   -1.7660
  Columns 41 through 50
   -1.7600   -1.7540   -1.7480   -1.7420   -1.7360   -1.7300   -1.7240   -1.7180   -1.7120   -1.7060
  Columns 51 through 60
   -1.7000   -1.6940   -1.6880   -1.6820   -1.6760   -1.6700   -1.6640   -1.6580   -1.6520   -1.6460
  Columns 61 through 70
   -1.6400   -1.6340   -1.6280   -1.6220   -1.6160   -1.6100   -1.6040   -1.5980   -1.5920   -1.5860
  Columns 71 through 80
   -1.5800   -1.5740   -1.5680   -1.5620   -1.5560   -1.5500   -1.5440   -1.5380   -1.5320   -1.5260
  Columns 81 through 90
   -1.5200   -1.5140   -1.5080   -1.5020   -1.4960   -1.4900   -1.4840   -1.4780   -1.4720   -1.4660
  Columns 91 through 100
   -1.4600   -1.4540   -1.4480   -1.4420   -1.4360   -1.4300   -1.4240   -1.4180   -1.4120   -1.4060
  Columns 101 through 110
   -1.4000   -1.3940   -1.3880   -1.3820   -1.3760   -1.3700   -1.3640   -1.3580   -1.3520   -1.3460
  Columns 111 through 120
   -1.3400   -1.3340   -1.3280   -1.3220   -1.3160   -1.3100   -1.3040   -1.2980   -1.2920   -1.2860
  Columns 121 through 130
   -1.2800   -1.2740   -1.2680   -1.2620   -1.2560   -1.2500   -1.2440   -1.2380   -1.2320   -1.2260
  Columns 131 through 140
   -1.2200   -1.2140   -1.2080   -1.2020   -1.1960   -1.1900   -1.1840   -1.1780   -1.1720   -1.1660
  Columns 141 through 150
   -1.1600   -1.1540   -1.1480   -1.1420   -1.1360   -1.1300   -1.1240   -1.1180   -1.1120   -1.1060
  Columns 151 through 160
   -1.1000   -1.0940   -1.0880   -1.0820   -1.0760   -1.0700   -1.0640   -1.0580   -1.0520   -1.0460
  Columns 161 through 170
   -1.0400   -1.0340   -1.0280   -1.0220   -1.0160   -1.0100   -1.0040   -0.9980   -0.9920   -0.9860
  Columns 171 through 180
   -0.9800   -0.9740   -0.9680   -0.9620   -0.9560   -0.9500   -0.9440   -0.9380   -0.9320   -0.9260
  Columns 181 through 190
   -0.9200   -0.9140   -0.9080   -0.9020   -0.8960   -0.8900   -0.8840   -0.8780   -0.8720   -0.8660
  Columns 191 through 200
   -0.8600   -0.8540   -0.8480   -0.8420   -0.8360   -0.8300   -0.8240   -0.8180   -0.8120   -0.8060
  Columns 201 through 210
   -0.8000   -0.7940   -0.7880   -0.7820   -0.7760   -0.7700   -0.7640   -0.7580   -0.7520   -0.7460
  Columns 211 through 220
   -0.7400   -0.7340   -0.7280   -0.7220   -0.7160   -0.7100   -0.7040   -0.6980   -0.6920   -0.6860
  Columns 221 through 230
   -0.6800   -0.6740   -0.6680   -0.6620   -0.6560   -0.6500   -0.6440   -0.6380   -0.6320   -0.6260
  Columns 231 through 240
   -0.6200   -0.6140   -0.6080   -0.6020   -0.5960   -0.5900   -0.5840   -0.5780   -0.5720   -0.5660
  Columns 241 through 250
   -0.5600   -0.5540   -0.5480   -0.5420   -0.5360   -0.5300   -0.5240   -0.5180   -0.5120   -0.5060
  Columns 251 through 260
   -0.5000   -0.4940   -0.4880   -0.4820   -0.4760   -0.4700   -0.4640   -0.4580   -0.4520   -0.4460
  Columns 261 through 270
   -0.4400   -0.4340   -0.4280   -0.4220   -0.4160   -0.4100   -0.4040   -0.3980   -0.3920   -0.3860
  Columns 271 through 280
   -0.3800   -0.3740   -0.3680   -0.3620   -0.3560   -0.3500   -0.3440   -0.3380   -0.3320   -0.3260
  Columns 281 through 290
   -0.3200   -0.3140   -0.3080   -0.3020   -0.2960   -0.2900   -0.2840   -0.2780   -0.2720   -0.2660
  Columns 291 through 300
   -0.2600   -0.2540   -0.2480   -0.2420   -0.2360   -0.2300   -0.2240   -0.2180   -0.2120   -0.2060
  Columns 301 through 310
   -0.2000   -0.1940   -0.1880   -0.1820   -0.1760   -0.1700   -0.1640   -0.1580   -0.1520   -0.1460
  Columns 311 through 320
   -0.1400   -0.1340   -0.1280   -0.1220   -0.1160   -0.1100   -0.1040   -0.0980   -0.0920   -0.0860
  Columns 321 through 330
   -0.0800   -0.0740   -0.0680   -0.0620   -0.0560   -0.0500   -0.0440   -0.0380   -0.0320   -0.0260
  Columns 331 through 340
   -0.0200   -0.0140   -0.0080   -0.0020    0.0040    0.0100    0.0160    0.0220    0.0280    0.0340
  Columns 341 through 350
    0.0400    0.0460    0.0520    0.0580    0.0640    0.0700    0.0760    0.0820    0.0880    0.0940
  Columns 351 through 360
    0.1000    0.1060    0.1120    0.1180    0.1240    0.1300    0.1360    0.1420    0.1480    0.1540
  Columns 361 through 370
    0.1600    0.1660    0.1720    0.1780    0.1840    0.1900    0.1960    0.2020    0.2080    0.2140
  Columns 371 through 380
    0.2200    0.2260    0.2320    0.2380    0.2440    0.2500    0.2560    0.2620    0.2680    0.2740
  Columns 381 through 390
    0.2800    0.2860    0.2920    0.2980    0.3040    0.3100    0.3160    0.3220    0.3280    0.3340
  Columns 391 through 400
    0.3400    0.3460    0.3520    0.3580    0.3640    0.3700    0.3760    0.3820    0.3880    0.3940
  Columns 401 through 410
    0.4000    0.4060    0.4120    0.4180    0.4240    0.4300    0.4360    0.4420    0.4480    0.4540
  Columns 411 through 420
    0.4600    0.4660    0.4720    0.4780    0.4840    0.4900    0.4960    0.5020    0.5080    0.5140
  Columns 421 through 430
    0.5200    0.5260    0.5320    0.5380    0.5440    0.5500    0.5560    0.5620    0.5680    0.5740
  Columns 431 through 440
    0.5800    0.5860    0.5920    0.5980    0.6040    0.6100    0.6160    0.6220    0.6280    0.6340
  Columns 441 through 450
    0.6400    0.6460    0.6520    0.6580    0.6640    0.6700    0.6760    0.6820    0.6880    0.6940
  Columns 451 through 460
    0.7000    0.7060    0.7120    0.7180    0.7240    0.7300    0.7360    0.7420    0.7480    0.7540
  Columns 461 through 470
    0.7600    0.7660    0.7720    0.7780    0.7840    0.7900    0.7960    0.8020    0.8080    0.8140
  Columns 471 through 480
    0.8200    0.8260    0.8320    0.8380    0.8440    0.8500    0.8560    0.8620    0.8680    0.8740
  Columns 481 through 490
    0.8800    0.8860    0.8920    0.8980    0.9040    0.9100    0.9160    0.9220    0.9280    0.9340
  Columns 491 through 500
    0.9400    0.9460    0.9520    0.9580    0.9640    0.9700    0.9760    0.9820    0.9880    0.9940
  Columns 501 through 510
    1.0000    1.0060    1.0120    1.0180    1.0240    1.0300    1.0360    1.0420    1.0480    1.0540
  Columns 511 through 520
    1.0600    1.0660    1.0720    1.0780    1.0840    1.0900    1.0960    1.1020    1.1080    1.1140
  Columns 521 through 530
    1.1200    1.1260    1.1320    1.1380    1.1440    1.1500    1.1560    1.1620    1.1680    1.1740
  Columns 531 through 540
    1.1800    1.1860    1.1920    1.1980    1.2040    1.2100    1.2160    1.2220    1.2280    1.2340
  Columns 541 through 550
    1.2400    1.2460    1.2520    1.2580    1.2640    1.2700    1.2760    1.2820    1.2880    1.2940
  Columns 551 through 560
    1.3000    1.3060    1.3120    1.3180    1.3240    1.3300    1.3360    1.3420    1.3480    1.3540
  Columns 561 through 570
    1.3600    1.3660    1.3720    1.3780    1.3840    1.3900    1.3960    1.4020    1.4080    1.4140
  Columns 571 through 580
    1.4200    1.4260    1.4320    1.4380    1.4440    1.4500    1.4560    1.4620    1.4680    1.4740
  Columns 581 through 590
    1.4800    1.4860    1.4920    1.4980    1.5040    1.5100    1.5160    1.5220    1.5280    1.5340
  Columns 591 through 600
    1.5400    1.5460    1.5520    1.5580    1.5640    1.5700    1.5760    1.5820    1.5880    1.5940
  Columns 601 through 610
    1.6000    1.6060    1.6120    1.6180    1.6240    1.6300    1.6360    1.6420    1.6480    1.6540
  Columns 611 through 620
    1.6600    1.6660    1.6720    1.6780    1.6840    1.6900    1.6960    1.7020    1.7080    1.7140
  Columns 621 through 630
    1.7200    1.7260    1.7320    1.7380    1.7440    1.7500    1.7560    1.7620    1.7680    1.7740
  Columns 631 through 640
    1.7800    1.7860    1.7920    1.7980    1.8040    1.8100    1.8160    1.8220    1.8280    1.8340
  Columns 641 through 650
    1.8400    1.8460    1.8520    1.8580    1.8640    1.8700    1.8760    1.8820    1.8880    1.8940
  Columns 651 through 660
    1.9000    1.9060    1.9120    1.9180    1.9240    1.9300    1.9360    1.9420    1.9480    1.9540
  Columns 661 through 670
    1.9600    1.9660    1.9720    1.9780    1.9840    1.9900    1.9960    2.0020    2.0080    2.0140
  Columns 671 through 680
    2.0200    2.0260    2.0320    2.0380    2.0440    2.0500    2.0560    2.0620    2.0680    2.0740
  Columns 681 through 690
    2.0800    2.0860    2.0920    2.0980    2.1040    2.1100    2.1160    2.1220    2.1280    2.1340
  Columns 691 through 700
    2.1400    2.1460    2.1520    2.1580    2.1640    2.1700    2.1760    2.1820    2.1880    2.1940
  Columns 701 through 710
    2.2000    2.2060    2.2120    2.2180    2.2240    2.2300    2.2360    2.2420    2.2480    2.2540
  Columns 711 through 720
    2.2600    2.2660    2.2720    2.2780    2.2840    2.2900    2.2960    2.3020    2.3080    2.3140
  Columns 721 through 730
    2.3200    2.3260    2.3320    2.3380    2.3440    2.3500    2.3560    2.3620    2.3680    2.3740
  Columns 731 through 740
    2.3800    2.3860    2.3920    2.3980    2.4040    2.4100    2.4160    2.4220    2.4280    2.4340
  Columns 741 through 750
    2.4400    2.4460    2.4520    2.4580    2.4640    2.4700    2.4760    2.4820    2.4880    2.4940
  Columns 751 through 760
    2.5000    2.5060    2.5120    2.5180    2.5240    2.5300    2.5360    2.5420    2.5480    2.5540
  Columns 761 through 770
    2.5600    2.5660    2.5720    2.5780    2.5840    2.5900    2.5960    2.6020    2.6080    2.6140
  Columns 771 through 780
    2.6200    2.6260    2.6320    2.6380    2.6440    2.6500    2.6560    2.6620    2.6680    2.6740
  Columns 781 through 790
    2.6800    2.6860    2.6920    2.6980    2.7040    ...