-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimpson-integration-julia.html
466 lines (440 loc) · 21.9 KB
/
simpson-integration-julia.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Simpson's rule in Julia</title>
<meta charset="utf-8">
<meta property="og:title" content="Simpson's rule in Julia">
<meta property="og:site_name" content="Modesto Mas | Blog">
<meta property="og:image" content="https://mmas.github.io/images/profile.jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<meta property="og:url" content="https://mmas.github.io/simpson-integration-julia">
<meta property="og:locale" content="en_GB">
<meta name="twitter:image" content="https://mmas.github.io/images/profile.jpg">
<meta name="twitter:url" content="https://mmas.github.io/simpson-integration-julia">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Simpson's rule in Julia">
<meta name="description" content="An approximation to the integral of a function f(x) over an interval [a,b] can be approximated by the Simpson's rule as follows: ∫ a b f ( x ) d x ≈...">
<meta name="twitter:description" content="An approximation to the integral of a function f(x) over an interval [a,b] can be approximated by the Simpson's rule as follows: ∫ a b f ( x ) d x ≈...">
<meta property="og:description" content="An approximation to the integral of a function f(x) over an interval [a,b] can be approximated by the Simpson's rule as follows: ∫ a b f ( x ) d x ≈...">
<meta name="keywords" content="julia,numerical-analysis,numerical-integration">
<meta property="og:type" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:type" content="article">
<meta property="article:author" content="https://github.com/mmas">
<meta property="article:section" content="julia">
<meta property="article:tag" content="julia,numerical-analysis,numerical-integration">
<meta property="article:published_time" content="2016-04-15">
<meta property="article:modified_time" content="2016-04-15">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: {
scale: 93,
showMathMenu: false
},
tex2jax: {
"inlineMath": [["$","$"], ["\\(","\\)"]]
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body class="entry-detail">
<header>
<div>
<img src="https://mmas.github.io/images/profile.jpg">
<a class="brand" href="/">Modesto Mas</a>
<span>Data/Python/DevOps Engineer</span>
<nav>
<ul>
<li><a href="/tags">Tags</a></li>
<li><a href="https://github.com/mmas/mmas.github.io/issues" target="_blank">Issues</a></li>
</ul>
</nav>
</div>
</header>
<section id="content" role="main">
<article>
<header>
<h1><a href="/simpson-integration-julia">Simpson's rule in Julia</a></h1>
<time datetime="2016-04-15">Apr 15, 2016</time>
<a class="tag" href="/tags?tag=julia">julia</a>
<a class="tag" href="/tags?tag=numerical-analysis">numerical-analysis</a>
<a class="tag" href="/tags?tag=numerical-integration">numerical-integration</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p>An approximation to the integral of a function <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>f</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow></math> over an interval <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>a</mi><mo>,</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math> can be approximated by the <a target="_blank" href="https://en.wikipedia.org/wiki/Simpson's_rule">Simpson's rule</a> as follows:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mi>a</mi>
<mi>b</mi>
</msubsup>
<mi>f</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mtext> </mtext>
<mi>d</mi>
<mi>x</mi>
<mo>≈<!-- ≈ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mrow>
<mi>b</mi>
<mo>−<!-- − --></mo>
<mi>a</mi>
</mrow>
<mn>6</mn>
</mfrac>
</mrow>
<mtext> </mtext>
<mrow class="MJX-TeXAtom-ORD">
<mo maxsize="1.623em" minsize="1.623em">(</mo>
</mrow>
<mi>f</mi>
<mo stretchy="false">(</mo>
<mi>a</mi>
<mo stretchy="false">)</mo>
<mo>+</mo>
<mn>4</mn>
<mi>f</mi>
<mo stretchy="false">(</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mrow>
<mi>a</mi>
<mo>+</mo>
<mi>b</mi>
</mrow>
<mn>2</mn>
</mfrac>
</mrow>
<mo stretchy="false">)</mo>
<mo>+</mo>
<mi>f</mi>
<mo stretchy="false">(</mo>
<mi>b</mi>
<mo stretchy="false">)</mo>
<mrow class="MJX-TeXAtom-ORD">
<mo maxsize="1.623em" minsize="1.623em">)</mo>
</mrow>
<mo>.</mo>
</math>
<p>Using the composite Simpson's rule, the interval <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo stretchy="false">[</mo><mi>a</mi><mo>,</mo><mi>b</mi><mo stretchy="false">]</mo></mrow></math> is divided in subintervals and Simpson's rule is applied to each interval and the results are summed. The composite Simpson's rule is given by</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mi>a</mi>
<mi>b</mi>
</msubsup>
<mi>f</mi>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mtext> </mtext>
<mi>d</mi>
<mi>x</mi>
<mo>≈<!-- ≈ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>h</mi>
<mn>3</mn>
</mfrac>
</mrow>
<mtext> </mtext>
<mrow class="MJX-TeXAtom-ORD">
<mo maxsize="1.623em" minsize="1.623em">(</mo>
</mrow>
<mi>f</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>x</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">)</mo>
<mo>+</mo>
<mn>2</mn>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>j</mi>
<mo>=</mo>
<mn>1</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<mn>2</mn>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</munderover>
<mi>f</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>j</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
<mo>+</mo>
<mn>4</mn>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>j</mi>
<mo>=</mo>
<mn>1</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<mn>2</mn>
</mrow>
</munderover>
<mi>f</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>x</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>j</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msub>
<mo>+</mo>
<mi>f</mi>
<mo stretchy="false">(</mo>
<msub>
<mi>x</mi>
<mi>n</mi>
</msub>
<mo stretchy="false">)</mo>
<mrow class="MJX-TeXAtom-ORD">
<mo maxsize="1.623em" minsize="1.623em">)</mo>
</mrow>
<mo>,</mo>
</math>
<p>where <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>n</mi></mrow></math> is the number (even) of subintervals and <math xmlns="http://www.w3.org/1998/Math/MathML">
<mi>h</mi>
<mo>=</mo>
<mo stretchy="false">(</mo>
<mi>b</mi>
<mo>−<!-- − --></mo>
<mi>a</mi>
<mo stretchy="false">)</mo>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<mi>n</mi>
</math>.</p>
<p>The composite Simpson's rule of a function <code>f</code> over the interval [<code>a</code>,<code>b</code>] taking <code>n</code> intervals can be implemented in Julia (0.4.2) as</p>
<pre class="sourceCode julia"><code class="sourceCode julia"><span class="kw">function</span> simps(f::<span class="dt">Function</span>, a::<span class="dt">Number</span>, b::<span class="dt">Number</span>, n::<span class="dt">Number</span>)
n % <span class="fl">2</span> == <span class="fl">0</span> || error(<span class="st">"`n` must be even"</span>)
h = (b-a)/n
s = f(a) + f(b)
s += <span class="fl">4</span>sum(f(a + collect(<span class="fl">1</span>:<span class="fl">2</span>:n) * h))
s += <span class="fl">2</span>sum(f(a + collect(<span class="fl">2</span>:<span class="fl">2</span>:n-<span class="fl">1</span>) * h))
<span class="kw">return</span> h/<span class="fl">3</span> * s
<span class="kw">end</span></code></pre>
<p>As an example, <math xmlns="http://www.w3.org/1998/Math/MathML">
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mn>0</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>π<!-- π --></mi>
</mrow>
</msubsup>
<mn>3</mn>
<mi>sin</mi>
<mo>⁡<!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<msup>
<mo stretchy="false">)</mo>
<mn>3</mn>
</msup>
<mtext> </mtext>
<mi>d</mi>
<mi>x</mi>
<mo>=</mo>
<mn>4</mn>
</math>, in Julia:</p>
<pre class="sourceCode julia"><code class="sourceCode julia">julia> simps(x -> <span class="fl">3</span>sin(x).^<span class="fl">3</span>,<span class="fl">0</span>,pi,<span class="fl">4</span>)
<span class="fl">3.792237795874079</span>
julia> simps(x -> <span class="fl">3</span>sin(x).^<span class="fl">3</span>,<span class="fl">0</span>,pi,<span class="fl">6</span>)
<span class="fl">3.9783434011671583</span>
julia> simps(x -> <span class="fl">3</span>sin(x).^<span class="fl">3</span>,<span class="fl">0</span>,pi,<span class="fl">12</span>)
<span class="fl">3.998978466021562</span></code></pre>
<p><a target="_blank" href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simps.html">Scipy provides a Simpson's rule</a> for Python. It takes an array to integrate and an array with the sampling points or the spacing of the integrating points (the parameter <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>h</mi></mrow></math>). Let's implement the function following a similar structure.</p>
<p>The number of points of the array corresponds with the number of intervals and must be odd, being the number of subdivisions even. Although Scipy deals with an even number of intervals, here we are going to keep it simple and just implement the mathematical definition. Some code can be simplified, for example using auxiliar functions, but it is kept to be more explicit.</p>
<p>Writing the composite Simpson's rule as</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<msub>
<mi>S</mi>
<mi>n</mi>
</msub>
<mo>=</mo>
<mrow class="MJX-TeXAtom-ORD">
<mfrac>
<mi>h</mi>
<mn>3</mn>
</mfrac>
</mrow>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>j</mi>
<mo>=</mo>
<mn>1</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>n</mi>
<mrow class="MJX-TeXAtom-ORD">
<mo>/</mo>
</mrow>
<mn>2</mn>
</mrow>
</munderover>
<mo stretchy="false">(</mo>
<msub>
<mi>y</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>j</mi>
<mo>−<!-- − --></mo>
<mn>2</mn>
</mrow>
</msub>
<mo>+</mo>
<mn>4</mn>
<msub>
<mi>y</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>j</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msub>
<mo>+</mo>
<msub>
<mi>y</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>2</mn>
<mi>j</mi>
</mrow>
</msub>
<mo stretchy="false">)</mo>
<mo>,</mo>
</math>
<p>can be implemented in Julia for samples <code>y</code> and sampling spacing <code>h</code> as</p>
<pre class="sourceCode julia"><code class="sourceCode julia"><span class="kw">function</span> simps(y::<span class="dt">Vector</span>, h::<span class="dt">Number</span>)
n = length(y)-<span class="fl">1</span>
n % <span class="fl">2</span> == <span class="fl">0</span> || error(<span class="st">"`y` length (number of intervals) must be odd"</span>)
s = sum(slice(y,<span class="fl">1</span>:<span class="fl">2</span>:n) + <span class="fl">4</span>slice(y,<span class="fl">2</span>:<span class="fl">2</span>:n) + slice(y,<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>))
<span class="kw">return</span> h/<span class="fl">3</span> * s
<span class="kw">end</span></code></pre>
<p>And using the same example as before for a vector of 201 elements:</p>
<pre class="sourceCode julia"><code class="sourceCode julia">julia> (y=<span class="fl">3</span>sin(linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>)).^<span class="fl">3</span>; h=pi/<span class="fl">200</span>; simps(y,h))
<span class="fl">3.9999999878202863</span></code></pre>
<p>Similarly, knowing the samples array <code>y</code> and the sampling points array <code>x</code>:</p>
<pre class="sourceCode julia"><code class="sourceCode julia"><span class="kw">function</span> simps(y::<span class="dt">Vector</span>, x::<span class="dt">Union</span>{<span class="dt">Vector</span>,<span class="dt">Range</span>})
n = length(y)-<span class="fl">1</span>
n % <span class="fl">2</span> == <span class="fl">0</span> || error(<span class="st">"`y` length (number of intervals) must be odd"</span>)
length(x)-<span class="fl">1</span> == n || error(<span class="st">"`x` and `y` length must be equal"</span>)
h = (x[<span class="kw">end</span>]-x[<span class="fl">1</span>])/n
s = sum(slice(y,<span class="fl">1</span>:<span class="fl">2</span>:n) + <span class="fl">4</span>slice(y,<span class="fl">2</span>:<span class="fl">2</span>:n) + slice(y,<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>))
<span class="kw">return</span> h/<span class="fl">3</span> * s
<span class="kw">end</span></code></pre>
<pre class="sourceCode julia"><code class="sourceCode julia">julia> (x=linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>); y=<span class="fl">3</span>sin(x).^<span class="fl">3</span>; simps(y,x))
<span class="fl">3.9999999878202863</span></code></pre>
<p>Remark that the vectors are sliced using the function <code>slice</code> and not the direct indexing (<code>array[start:step:stop]</code>), avoiding the creation of new vectors. Also, <code>x</code> can be a <code>Vector</code> or a <code>Range</code>, like a <code>LinSpace</code>.</p>
<p>With the Scipy implementation in mind, let's add support for matrices by adding the parameter <code>axis</code> to integrate the matrix <code>y</code> by row (<code>axis=2</code>, default) or column (<code>axis=1</code>).</p>
<p>The implementation of the Simpson's rule integration for the samples <code>y</code>, knowing the sampling spacing <code>h</code>:</p>
<pre class="sourceCode julia"><code class="sourceCode julia"><span class="kw">function</span> simps(y::<span class="dt">Matrix</span>, h::<span class="dt">Number</span>, axis::<span class="dt">Integer</span>=<span class="fl">2</span>)
n = size(y)[axis]-<span class="fl">1</span>
n % <span class="fl">2</span> == <span class="fl">0</span> || error(<span class="st">"`y` length (number of intervals) must be odd"</span>)
axis <span class="kw">in</span> [<span class="fl">1</span>,<span class="fl">2</span>] || error(<span class="st">"`axis` must be 1 or 2"</span>)
<span class="kw">if</span> axis == <span class="fl">2</span>
inds = [(:,<span class="fl">1</span>:<span class="fl">2</span>:n),(:,<span class="fl">2</span>:<span class="fl">2</span>:n),(:,<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>)]
<span class="kw">else</span>
inds = [(<span class="fl">1</span>:<span class="fl">2</span>:n,:),(<span class="fl">2</span>:<span class="fl">2</span>:n,:),(<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>,:)]
<span class="kw">end</span>
s = sum(slice(y,inds[<span class="fl">1</span>]...) + <span class="fl">4</span>slice(y,inds[<span class="fl">2</span>]...) + slice(y,inds[<span class="fl">3</span>]...),axis)
<span class="kw">return</span> h/<span class="fl">3</span> * s
<span class="kw">end</span></code></pre>
<p>The implementation of the Simpson's rule integration for the samples <code>y</code>, knowing the sampling points array <code>x</code>:</p>
<pre class="sourceCode julia"><code class="sourceCode julia"><span class="kw">function</span> simps(y::<span class="dt">Matrix</span>, x::<span class="dt">Union</span>{<span class="dt">Matrix</span>,<span class="dt">Vector</span>,<span class="dt">Range</span>}, axis::<span class="dt">Integer</span>=<span class="fl">2</span>)
n = size(y)[axis]-<span class="fl">1</span>
n % <span class="fl">2</span> == <span class="fl">0</span> || error(<span class="st">"`y` length (number of intervals) must be odd"</span>)
axis <span class="kw">in</span> [<span class="fl">1</span>,<span class="fl">2</span>] || error(<span class="st">"`axis` must be 1 or 2"</span>)
<span class="kw">if</span> length(size(x)) == <span class="fl">1</span>
length(x)-<span class="fl">1</span> == n || error(<span class="st">"`x` and `y` length must be equal in axis `axis`"</span>)
h = (x[<span class="kw">end</span>]-x[<span class="fl">1</span>])/n
<span class="kw">else</span>
size(x)[axis]-<span class="fl">1</span> == n || error(<span class="st">"`x` and `y` length must be equal in axis `axis`"</span>)
h = nothing
<span class="kw">end</span>
<span class="kw">if</span> axis == <span class="fl">2</span>
h != nothing || (h = (x[<span class="fl">1</span>,<span class="kw">end</span>]-x[<span class="fl">1</span>,<span class="fl">1</span>])/n)
inds = [(:,<span class="fl">1</span>:<span class="fl">2</span>:n),(:,<span class="fl">2</span>:<span class="fl">2</span>:n),(:,<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>)]
<span class="kw">else</span>
h != nothing || (h = (x[<span class="kw">end</span>,<span class="fl">1</span>]-x[<span class="fl">1</span>,<span class="fl">1</span>])/n)
inds = [(<span class="fl">1</span>:<span class="fl">2</span>:n,:),(<span class="fl">2</span>:<span class="fl">2</span>:n,:),(<span class="fl">3</span>:<span class="fl">2</span>:n+<span class="fl">1</span>,:)]
<span class="kw">end</span>
s = sum(slice(y,inds[<span class="fl">1</span>]...) + <span class="fl">4</span>slice(y,inds[<span class="fl">2</span>]...) + slice(y,inds[<span class="fl">3</span>]...),axis)
<span class="kw">return</span> h/<span class="fl">3</span> * s
<span class="kw">end</span></code></pre>
<p>As we can see, <code>x</code> might be a <code>Matrix</code> but also a <code>Vector</code> or a <code>Range</code>, like in the previous case. The requirement is that it needs to have the same length as <code>y</code> in the axis <code>axis</code>. In both cases, the axis defines the order of the indices to slice and the axis of the summation.</p>
<p>Adding to the previous example that <math xmlns="http://www.w3.org/1998/Math/MathML">
<msubsup>
<mo>∫<!-- ∫ --></mo>
<mn>0</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>π<!-- π --></mi>
</mrow>
</msubsup>
<mi>sin</mi>
<mo>⁡<!-- --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo stretchy="false">)</mo>
<mtext> </mtext>
<mi>d</mi>
<mi>x</mi>
<mo>=</mo>
<mn>2</mn>
</math> and using it for testing the last two functions gives:</p>
<pre class="sourceCode julia"><code class="sourceCode julia">julia> (y=[<span class="fl">3</span>sin(linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>)).^<span class="fl">3</span> sin(linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>))]'; h=pi/<span class="fl">200</span>; simps(y,h))
<span class="fl">2</span>x1 <span class="dt">Array</span>{<span class="dt">Float64</span>,<span class="fl">2</span>}:
<span class="fl">4.0</span>
<span class="fl">2.0</span>
julia> (y=[<span class="fl">3</span>sin(linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>)).^<span class="fl">3</span> sin(linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>))]; h=pi/<span class="fl">200</span>; simps(y,h,<span class="fl">1</span>))
<span class="fl">1</span>x2 <span class="dt">Array</span>{<span class="dt">Float64</span>,<span class="fl">2</span>}:
<span class="fl">4.0</span> <span class="fl">2.0</span>
julia> (x=linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>); y=[<span class="fl">3</span>sin(x).^<span class="fl">3</span> sin(x)]'; simps(y,x,<span class="fl">2</span>))
<span class="fl">2</span>x1 <span class="dt">Array</span>{<span class="dt">Float64</span>,<span class="fl">2</span>}:
<span class="fl">4.0</span>
<span class="fl">2.0</span>
julia> (x=linspace(<span class="fl">0</span>,pi,<span class="fl">201</span>); y=[<span class="fl">3</span>sin(x).^<span class="fl">3</span> sin(x)]; simps(y,x,<span class="fl">1</span>))
<span class="fl">1</span>x2 <span class="dt">Array</span>{<span class="dt">Float64</span>,<span class="fl">2</span>}:
<span class="fl">4.0</span> <span class="fl">2.0</span></code></pre>
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>