Tải bản đầy đủ - 0 (trang)
1 Do, For a While

1 Do, For a While

Tải bản đầy đủ - 0trang

56



3. Defining functions



f[x_,y_]:=Sqrt[x^2+y^2]

f[3,4]

5



Problem 3.5

Define the function f (x, y) = ||x|−|y|| in Mathematica and plot its graph for

−10 ≤ x, y ≤ 10.

=⇒ Solution.

f[x_, y_] := Abs[Abs[x] - Abs[y]]

Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10}]



Problem 3.6

Recall that if one wants to prove, by mathematical induction, that a statement

P (n) is valid for all natural numbers n, one needs first to check P (1) is valid,

and then assuming P (k) is correct, prove that P (k + 1) is also valid.



3.1 Formulas as functions



57



Using Mathematica and mathematical induction, prove the following identities:

n(n + 1)

2

n(n

+ 1)(2n + 1)

12 + 22 + · · · + n2 =

.

6

1 + 2 + ··· + n =



=⇒ Solution.

We first prove the first identity. Define a function as follow:

g[n_] := n (n + 1)/2



Clearly the identity is valid for n = 1.

g[1]

1



Now we suppose it is correct for k and check that it is also valid for k + 1.

Thus we assume

1 + 2 + · · · k = k(k + 1)/2 = g(k),

and we need to show that

1 + 2 + · · · + k + k + 1 = g(k) + 1 = g(k + 1).

We ask Mathematica for help

Simplify[g[k] + k + 1 == g[k + 1]]

True



Thus we proved, by mathematical induction, that the first identity is valid.

Here is the code for the second identity.

f[n_] := n (n + 1) (2 n + 1)/6

f[1]

1

f[n] + (n + 1)^2

(1 + n)^2 + 1/6 n (1 + n) (1 + 2 n)

f[n + 1]

1/6 (1 + n) (2 + n) (1 + 2 (1 + n))

Simplify[f[n] + (n + 1)^2 == f[n + 1]]

True



There are other ways to define a function (a pattern). For example, we can

define



58



3. Defining functions



x ⊕y :=x+y+x*y

We check this in fact operates correctly.

2⊕3= 11

x⊕0= x

a ⊕ b= a + b+ a*b

We look at these types of definitions systematically in Problem 10.10 (see

also Problem 8.17).

One can define functions with pre-defined (default) values. Here we define



the function f (x) = x so that if we don’t pass any value to f , it will return

1.

f[x_: 1] := Sqrt[x]

f[16]

4

f[]

1



Here is another example, a function with two variables, both given default

values. We use two built-in functions Text and Style to change the size and

the colour of the text we pass into the function (see Problem 3.7).



 


     



 






  







Later, in Chapter 11, we will look at these types of functions further. We

will also define functions with conditions, functions with several definitions and

functions containing several lines of code (a procedure).



3.2 Anonymous functions

Sometimes we need to “define a function as we go” and use it on the spot.

Mathematica enables us to define a function without giving it a name (nor any

reference to any specific variables), use it, and then move on! These functions



3.2 Anonymous functions



59



are called anonymous or pure functions. Obviously if we need to use a specific

function frequently, then the best way is to give it a name and define it as we

did in Section 3.1. Here is an anonymous function equivalent to f (x) = x2 + 4:

(#^2+4)&



The expression (#^2+4)& defines a nameless function. As usual we can plug

in data in place of #. The symbol & determines where the definition of the

function is completed.

(#^2+4)&[5]

29



Compare the following:

r[x_] := x (x + 1)

r[4]

20

# (# + 1) &[4]

20

4 // # (# + 1) &

20



Anonymous functions can handle several variables. Here is an example of

an anonymous function for f (x, y) = x2 + y 2 .

Sqrt[#1^2+#2^2]&[3,4]

5



As you might guess, #1 and #2 refer to the first and second variables in the

function.



Problem 3.7

Describe what the following function does.

Text[Style["Sydney", Blue, Italic, #]] &



=⇒ Solution.

We have put several of Mathematica’s built-in functions together. The argument (which, here, should be a number) passed to this function will be placed

where # is. If one looks at the Help for Style, one sees that the number will

determine the size of the font. Then Text will present the word Sydney, in blue

italic and the size given in #.



60



3. Defining functions



?Text

Text[expr] displays with expr in plain text format.

?Style

Style[expr,options] displays with expr formatted using the

specified option settings.



Here is the result of applying the function to the number 80.

[[

 
  #]] []





Exercise 3.2

Investigate what the following pure functions do.

Fibonacci[15]//Mod[#,5]&

PrimeQ[#! + 1] &@4

18//2^#+#&

Plot[Sin[x^2], {x, 0, 2 Pi}, PlotStyle -> {#1, #2}] &[Red, Thick]

(x #1 + y #2) &[1, 2]



4



Lists



A list is a collection of data. In this chapter we study how

to handle lists and have access to the elements in the lists.

Functions which generate lists are studied and methods

of selecting elements from a list with a specific property

are examined. Get ready for some serious programming!



One can think of a computer program as a function which accepts some

(crude) data or information and gives back the data we would like to obtain.

Lists are the way Wolfram Mathematica R handles information. Roughly speaking, a list is a collection of objects. The objects could be of any type and pattern.

Let us start with an example of a list:



       










  



As is shown, the objects (of any type and format) are arranged between

brackets, { , }. This looks like a mathematical set. One difference is that lists

respect order:

{1, 2} == {2, 1}

False



The other difference is that a list can contain a copy of the same object

several times:

{1,2,1} == {1,2}

False

© Springer International Publishing Switzerland 2015

R. Hazrat, Mathematica : A Problem-Centered Approach, Springer Undergraduate

Mathematics Series, DOI 10.1007/978-3-319-27585-7_4



61



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

1 Do, For a While

Tải bản đầy đủ ngay(0 tr)

×