• No se han encontrado resultados

Octave has several built-in functions to calculate definite integrals. We will use the built-inquad command. “Quad” is short forquadrature, which is a historical term referring to the process of calculating area by dividing into rectangles.

Example 3.2.1. Estimate

Z π/2

0

ex2cos(x) dx using Octave’squad algorithm.

Solution. The correct syntax isquad('f', a, b). We need to first define the function. >> f u n c t i o n y = f ( x )

y = exp( x . ˆ 2 ) .*c o s( x ) ;

end

>> quad('f', 0 , p i/ 2 )

ans = 1 . 8 7 5 7

Note that the function exp(x) is used forex. In this example, we used the function. . . end construction to define f. This is a versatile format that allows for multiple operations and outputs. We could have instead used an anonymous function, but note that no quotes are used around the name f if using an anonymous function withquad.

3.2.2 Octave scripts

Now suppose we want to write our own code for numeric integration. The midpoint rule, trapezoid rule, and Simpson’s rule are common algorithms used for numerical integration. These types of algorithms are easily implemented inOctave scriptfiles.

Let {a = x0, x1, x2, . . . , xn = b} be a partition of [a, b] into n subintervals, each of width ∆x= b−a

n . Then

Z b

a

f(x) dxcan be approximated as follows.

Midpoint rule:

∆x[f(m1) +f(m2) +· · ·+f(mn)] wheremi is the midpoint of the ith subinterval.

Trapezoid rule: ∆x 2 [f(x0) + 2f(x1) + 2f(x2) +· · ·+ 2f(xn−1) +f(xn)] Simpson’s rule: ∆x 3 [f(x0) + 4f(x1) + 2f(x2) + 4f(x3) +· · ·+ 2f(xn−2) + 4f(xn−1) +f(xn)] wherexi=a+i∆x.

Octave script files are plain text files containing a series of Octave commands. A script file needs to have a “.m” extension (not the .txt used by default in Windows for text files) and cannot begin with the keywordfunction. You can use any text editor, such as Notepad, Notepad++, or Emacs, but the Octave GUI has its own built-in text editor which can be accessed by changing to the “Editor” tab option displayed below the main command window. The built-in editor is ideal for creating, editing, and running .m files and will automatically color code comments and key words.

Example 3.2.2. Write an Octave script to calculate a midpoint rule approximation of

Z π/2

0

ex2cos(x)dx

using n= 100.

Solution. The basic strategy is to use a for loop that adds an additional function value to a running total with each iteration. Then the final answer is found by multiplying the sum by ∆x.

The following code can be used. Switch to the editor tab and enter the code in a plain text file. Save it as midpoint.m. It must be placed in your working directory, then it can be run by typing midpoint at the command prompt, or by clicking the “save and run” button on the editor toolbar (be sure to switch back to the command window to see the output).

Octave Script 3.1: Midpoint rule approximation

1 % f i l e 'm i d p o i n t .m' 2 % c a l c u l a t e s a m i d p o i n t r u l e a p p r o x i m a t i o n o f 3 % t h e i n t e g r a l from 0 t o p i /2 o f f ( x ) = exp ( x ˆ 2 ) c o s ( x ) 4 % −−t r a d i t i o n a l l o o p e d c o d e 5 6 % s e t l i m i t s o f i n t e g r a t i o n , number o f t e r m s and d e l t a x 7 a = 0 8 b = p i/2 9 n = 100 10 dx = ( b − a ) /n 11 12 % d e f i n e f u n c t i o n t o i n t e g r a t e 13 f u n c t i o n y = f ( x ) 14 y = exp( x . ˆ 2 ) .*c o s( x ) ; 15 end 16 17 msum = 0 ; % i n i t i a l i z e sum 18 m1 = a + dx / 2 ; % f i r s t m i d p o i n t 19 20 % l o o p t o c r e a t e sum o f f u n c t i o n v a l u e s 21 f o r i = 1 : n 22 m = m1 + ( i − 1 )*dx ; % c a l c u l a t e m i d p o i n t 23 msum = msum + f (m) ; % add t o m i d p o i n t sum 24 end

25

26 % m i d p o i n t a p p r o x i m a t i o n t o t h e i n t e g r a l 27 approx = msum*dx

Now run midpoint.m. >> m i d p o i n t a = 0 b = 1 . 5 7 0 8 n = 100 dx = 0 . 0 1 5 7 0 8 approx = 1 . 8 7 5 8

The traditional code works fine, but because Octave is a vector-based language, it is also possible to write vectorized code that does not require any loops.

Example 3.2.3. Write a vectorized Octave script to calculate a midpoint rule approximation of

Z π/2

0

ex2cos(x) dx

usingn= 100.

Solution. Now our strategy is to create a vector of thex-coordinates of the midpoints. Then we evaluatef over this midpoint vector to obtain a vector of function values. The midpoint approximation is the sum of the components of the vector, multiplied by ∆x.

Octave Script 3.2: Midpoint rule approximation - vectorized

1 % f i l e 'm i d p o i n t 2 .m' 2 % c a l c u l a t e s a m i d p o i n t r u l e a p p r o x i m a t i o n o f 3 % t h e i n t e g r a l from 0 t o p i /2 o f f ( x ) = exp ( x ˆ 2 ) c o s ( x ) 4 % −−v e c t o r i z e d c o d e 5 6 % s e t l i m i t s o f i n t e g r a t i o n , number o f t e r m s and d e l t a x 7 a = 0 8 b = p i/2 9 n = 100 10 dx = ( b − a ) /n 11 12 % d e f i n e f u n c t i o n t o i n t e g r a t e 13 f u n c t i o n y = f ( x ) 14 y = exp( x . ˆ 2 ) .*c o s( x ) ; 15 end 16 17 % c r e a t e v e c t o r o f m i d p o i n t s 18 m = [ a + dx /2 : dx : b − dx / 2 ] ; 19 20 % c r e a t e v e c t o r o f f u n c t i o n v a l u e s a t m i d p o i n t s 21M = f (m) ; 22 23 % m i d p o i n t a p p r o x i m a t i o n t o t h e i n t e g r a l 24 approx = dx*sum(M)

This code will give the same results as the traditional looped code, but it executes faster, and is arguably more intuitive.

3.3

Parametric, polar, and implicit functions

Documento similar