ebook img

An introduction to programming with Mathematica PDF

556 Pages·2005·6.481 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview An introduction to programming with Mathematica

This page intentionally left blank This page intentionally left blank AnIntroductiontoProgrammingwithMathematica(cid:1)r An Introduction to Programming with Mathematica(cid:1)r is de- signedtointroducetheMathematicaprogramminglanguage to a wide audience. Since the last edition of this book was published, significant changes have occurred in Mathemat- icaanditsuseworldwide.Keepingpacewiththesechanges, this substantially larger, updated version includes new and revised chapters on numerics, procedural, rule-based, and front end programming, and gives significant coverage to the latest features up to, and including, version 5.1 of the software. Mathematicanotebooks,availablefromwww.cambridge.org/ 0521846781, contain examples, programs, and solutions to exercises in the book. Additionally, material to supplement laterversionsofthesoftwarewillbemadeavailable.Thisis theidealtextforallscientificstudents,researchers,andpro- grammers wishing to deepen their understanding of Math- ematica, or even those keen to program using an interac- tive language that contains programming paradigms from all major programming languages: procedural, functional, recursive,rule-based,andobject-oriented. An Introduction to Programming with Mathematica(cid:1)r Third Edition PaulR.Wellin|RichardJ.Gaylord|SamuelN.Kamin    Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge  , UK Published in the United States of America by Cambridge University Press, New York www.cambridge.org Information on this title: www.cambridge.org/9780521846783 This book is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2005 - ---- eBook (NetLibrary) - --- eBook (NetLibrary) - ---- hardback - --- hardback Cambridge University Press has no responsibility for the persistence or accuracy of s for external or third-party internet websites referred to in this book, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Mathematica, Mathlink and Mathsource are registered trademarks of Wolfram Research, Inc. All other trademarks used herein are the property of their respective owners. Mathematica is not associated with Mathematica Policy Research, Inc. or MathTech, Inc. Wolfram Research is the holder of the copyright to the Mathematica software system described in this document, including without limitation such aspects of the system as its code, structure, sequence, organization, “look and feel”, programming language, and compilation of command names. Use of the system unless pursuant to the terms of a license granted by Wolfram Research or an otherwise authorized by law is an infringement of the copyright. 1 An introduction to Mathematica Mathematica is a very large and seemingly complex system. It contains hundreds of functions for performing various tasks in science, mathematics, and engineering, including computing, programming, data analysis, knowledge representation, and visualization of information. In this introductory chapter, we introduce the elementary operations in Mathematica and give a sense of its computational and programming breadth and depth. In addition, we give some basic information that users of Mathemat- ica need to know, such as how to startMathematica, how to get out of it, how to enter simple inputs and get answers, and finally how to use Mathematica’s documentation to get answers to questions about the system. 1.1 A brief overview of Mathematica Numerical computations Mathematica has been aptly described as a sophisticated calculator. With it you can enter mathematical expressions and compute their values. .08 12 In[1]:= Sin .86 Log 1 12 Out[1]= 0.481899 You can store values in memory. In[2]:= rent 350 Out[2]= 350 In[3]:= food 175 Out[3]= 175 In[4]:= heat 83 Out[4]= 83 2 An Introduction to Programming withMathematica In[5]:= rent food heat Out[5]= 608 Yet Mathematica differs from calculators and simple computer programs in its ability to calculate exact results and to compute to an arbitrary degree of precision. 1 1 1 In[6]:= 15 35 63 1 Out[6]= 9 In[7]:= 2500 Out[7]= 3273390607896141870013189696827599152216642046043064789483291 368096133796404674554883270092325904157150886684127560071009 217256545885393053328527589376 In[8]:= N , 500 Out[8]= 3.14159265358979323846264338327950288419716939937510582097494 459230781640628620899862803482534211706798214808651328230664 709384460955058223172535940812848111745028410270193852110555 964462294895493038196442881097566593344612847564823378678316 527120190914564856692346034861045432664821339360726024914127 372458700660631558817488152092096282925409171536436789259036 001133053054882046652138414695194151160943305727036575959195 309218611738193261179310511854807446237996274956735188575272 48912279381830119491 Symbolic computations One of the more powerful features of Mathematica is its ability to manipulate and compute with symbolic expressions. For example, you can factor polynomials and simplify trigono- metric expressions. In[9]:= Factor x5 1 Out[9]= 1 x 1 x x2 x3 x4 In[10]:= TrigReduce Sin 3 1 Out[10]= 3Sin Sin 3 4 1 An introduction to Mathematica 3 You can simplify expressions using assumptions about variables contained in those expres- sions. For example, ifk is assumed to be an integer,sin 2 k x simplifies to sin x . In[11]:= Simplify Sin 2 k x , k Integers Out[11]= Sin x This computes the conditions for which a general quadratic polynomial will have both roots equal to each other. In[12]:= Reduce x,ax2 bx c 0 y,ay2 by c 0 x y , a, b, c b2 Out[12]= a 0&&b 0 a 0&&bc 0 a 0&&c 4a You can create functions that are defined piecewise. In[13]:= Piecewise 1, x 0 , Sin x x 1 x 0 Out[13]= Sin x True x The knowledge base of Mathematica includes algorithms for solving polynomial equations, and computing integrals. In[14]:= Solve x3 ax 1 0, x 2 1 3a 9 3 27 4a3 1 3 Out[14]= x 3 , 1 3 21 332 3 9 3 27 4a3 1 3 a x 1 3 22 331 3 9 3 27 4a3 1 3 1 3 9 3 27 4a3 , 221 332 3 1 3 a x 1 3 22 331 3 9 3 27 4a3 1 3 1 3 9 3 27 4a3 221 332 3 1 In[15]:= x 1 x4 1 Out[15]= 2ArcTan 1 2 x 2ArcTan 1 2 x 4 2 Log 1 2 x x2 Log 1 2 x x2 4 An Introduction to Programming withMathematica Graphics The ability to visualize functions or sets of data often allows us greater insight into their structure and properties. Mathematica provides a wide range of graphing capabilities. These include two- and three-dimensional plots of functions or data sets, contour and density plots of functions of two variables, bar charts, histograms and pie charts of data sets, and many packages designed for specific graphical purposes. In addition, theMathemat- ica programming language allows you to construct graphical images “from the ground up” using primitive elements, as we will see in Chapter 9. Here is a simple two-dimensional plot of the function sin x 2 sin x2 . In[16]:= Plot Sin x 2 Sin x2 , x, , 1 0.5 3 2 1 1 2 3 0.5 1 Out[16]= Graphics You can combine two or more plots in a single graphic by enclosing them inside curly braces. In[17]:= Plot Sin x , Sin 2x , x, 0, 2 ; 1 0.5 1 2 3 4 5 6 0.5 1

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.