Mathematica Beyond Mathematics Although many books have been written about Mathematica, very few of them cover the new functionality added to the most recent versions of the program. This thoroughly revised second edition of Mathematica Beyond Mathematics: The Wolfram Language in the Real World introduces the new features using real-world examples based on the experience of the author as a consultant and Wolfram certified instructor. The examples strike a balance between relevance and difficulty in terms of Mathematica syntax, allowing readers to incrementally build up their Mathematica skills as they go through the chapters While reading this book, you will also learn more about the Wolfram Language and how to use it to solve a wide variety of problems. The author raises questions from a wide range of topics and answers them by taking full advan- tage of Mathematica’s latest features. For example: What sources of energy does the world really use? Are our cities getting warmer? Is the novel El Quixote written in Pi? Is it possible to reliably date the Earth using radioactive isotopes? How can we find planets outside our solar system? How can we model epidemics, earthquakes and other natural phenomena? What is the best way to compare organisms genetically? This new edition introduces the new capabilities added to the latest version of Mathematica (version 13), and discusses new topics related to machine learning, big data, finance economics, and physics. New to the Second Edition • Separate sections containing carefully selected additional resources that can be accessed from either Mathematica or online • Online Supplementary materials including code snippets used in the book and additional examples. • Updated commands to take full advantage of Mathematica 13. José Guillermo Sánchez León (http://diarium.usal.es/guillermo) is an engineer, physicist and mathematics PhD holder. He is currently a mathematics and statistical consultant and teaches Mathematical modeling at the University of Salamanca. He has worked in the energy industry and conducted research in a variety of fields: Modeling, optimization, medical physics, astron- omy, finance and others. In 1999, he was awarded a research grant at Wolfram Research Inc. headquarters in Champaign (Illinois, USA) after his statistical applications with Mathematica project won a competition sponsored by the company. Since then, he has been an active Math- ematica and webMathematica alpha and beta tester. He is also a Wolfram certified instructor and has extensive experience in teaching and develop- ing programs with both applications. Among his more than 100 articles, there are several where Mathematica and web Mathematica have been used extensively. Tay&lF orra ncis Tay&lFo rra nGcriosu p http://taylorandfrancis.com Mathematica Beyond Mathematics The Wolfram Language in the Real World Second Edition José Guillermo Sánchez León University of Salamanca, Spain Translated by Rubén García Berasategui Second edition published 2023 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN © 2023 José Guillermo Sánchez León CRC Press is an imprint of Taylor & Francis Group, LLC Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowl- edged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho- tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact [email protected] Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging‑in‑Publication Data Names: Sánchez León, José Guillermo, author. Title: Mathematica beyond mathematics : the Wolfram language in the real world / José Guillermo Sánchez León, University of Salamanca, Spain ; translated by Rubén García Berasategui. Description: Second edition. | Boca Raton, FL : Chapman & Hall/CRC Press, 2023. | Includes bibliographical references and index. Identifiers: LCCN 2022030387 (print) | LCCN 2022030388 (ebook) | ISBN 9781032004839 (hbk) | ISBN 9781032010236 (pbk) | ISBN 9781003176800 (ebk) Subjects: LCSH: Mathematica (Computer file) | Mathematics--Data processing. Classification: LCC QA76.95 .S26 2023 (print) | LCC QA76.95 (ebook) | DDC 510.285/536--dc23/eng/20221104 LC record available at https://lccn.loc.gov/2022030387 LC ebook record available at https://lccn.loc.gov/2022030388 ISBN: 978-1-032-00483-9 (hbk) ISBN: 978-1-032-01023-6 (pbk) ISBN: 978-1-003-17680-0 (ebk) DOI: 10.1201/ 9781003176800 Typeset in Times New Roman by KnowledgeWorks Global Ltd. Publisher’s note: This book has been prepared from camera-ready copy provided by the authors. Contents Preface IX 1. Getting Started 1 1.1 Mathematica, an Integrated Technical Computing System 1 1.2 First Steps 3 1.3 Editing Notebooks 12 1.4 Basic Ideas 19 1.5 From Graphics to Machine Learning 29 1.6 Additional Resources and Supplementary Materials 40 2. Programming: The Beauty and Power of the Wolfram Language 41 2.1 Mathematica’s Programming Language: The Wolfram Language 41 2.2 Lists Operations 46 2.3 Association and Dataset 50 2.4 Matrix Operations 52 2.5 Set, SetDelayed, and Dynamic Variables 55 2.6 Functional vs. Procedural Programming 57 2.7 Apply, Map, and Other Related Functions 60 2.8 Iterative Functions 63 2.9 Pure Functions 64 2.10 Global and Local Variables 70 2.11 Conditional Expressions and Conditions 72 2.12 Accuracy and Precision 79 2.13 Choosing the Method of Computation 82 2.14 Optimizing the Computation Time 84 2.15 Cloud Deployment 86 2.16 Package Development 87 2.17 Additional Resources 90 3. Interactive Applications, Image Processing, and More 91 3.1 The Manipulate Function 91 vi Mathematica beyond mathematics 3.2 Creating Demonstrations 97 3.3 Image Processing 100 3.4 Image Manipulation 105 3.5 Graphs and Networks 110 3.6 Application: Finding the Period of a Pendulum 113 3.7 Additional Resources 116 4. Accessing Scientific and Technical Information 117 4.1 The Wolfram Data Framework: Introducing Entities 117 4.2 Computable Data Functions 123 4.3 The Wolfram Data Repository 127 4.4 Weather Data in Real Time 129 4.5 Chemical and Physical Properties of Elements and Compounds 132 4.6 Life Sciences and Medicine 136 4.7 Earth Sciences and Geographic Data 142 4.8 Additional Resources 151 5. Data Analysis and Manipulation 153 5.1 Importing/Exporting 153 5.2 Statistical Analysis 162 5.3 Probability Distributions 169 5.4 Exploratory Data Analysis 181 5.5 Bootstrapping and Confidence Estimates 189 5.6 Curve Fitting 194 5.7 Time Series Analysis 203 5.8 Spatial Statistics 204 5.9 Additional Resources 208 6. Machine Learning and Neural Networks 207 6.1 What is Machine Learning 207 6.2 Classification 212 6.3 Prediction 221 6.4 Working with Neural Networks 225 6.5 Additional Resources 230 7. Calculating (cid:1) and Other Mathematical Tales 231 7.1 The Origins of (cid:1) 231 7.2 Archimedes’ Approximation 232 7.3 (cid:1) with More Than One Billion Decimals 235 7.4 Buffon’s Method 240 7.5 Application: Are the Decimal Digits of (cid:1) Random? 242 Contents vii 7.6 The Strange Connection 246 7.7 The Riemann Hypothesis 248 7.8 Looking for the Magic Prime Formula 252 7.9 Additional Resources 254 8. Looking at the Sky 255 8.1 A Short Astronomical Walk 255 8.2 Solar Analemma 259 8.3 Stargazing 260 8.4 Application: Determining the Color of the Stars 279 8.5 The Measurement of Distances Across the Universe 283 8.6 Application: Binary Systems and the Search for Exoplanets 287 8.7 Light Curves 291 8.8 Additional Resources 300 9. Nuclei and Radiations 301 9.1 Nuclear and Particle Physics 301 9.2 What are Isotopes? 302 9.3 Decay Constants, Decay Periods and Half-Lives 304 9.4 Decay Chains 308 9.5 Application: Modeling the Evolution of a Chain of Isotopes Over Time 313 9.6 Application: Dating the History of Humankind 316 9.7 Application: Calculating Binding Energies 321 9.8 Radiation Attenuation 328 9.9 Additional Resources 330 10. Modeling: Applications in Biokinetics, Epidemiology and More 331 10.1 Compartmental Modeling 331 10.2 Epidemiological Models 342 10.3 Physiological Modeling 346 10.4 Fitting a Model 351 10.5 Optimal Experimental Designs (OED) 355 10.6 BIOKMOD: The New Iodine OIR Model (ICRP 137) 360 10.7 Additional Modeling Examples 364 10.8 Modeling Using PDEs 366 10.9 System Modeler 369 10.10 Additional Resources 370 11. Economic, Financial and Optimization Applications 371 11.1 Accessing Economic Information 371 11.2 Financial Information 374 11.3 Financial Functions 381 viii Mathematica beyond mathematics 11.4 Optimization 392 11.5 The Shortest Path Problem 403 11.6 Optimum Flows 407 11.7 Blockchains 410 11.8 Additional Resources 412 12. Faster, Further 415 12.1 Parallel Computing 413 12.2 Parallel Programming 414 12.3 The Mandelbrot Set 422 12.4 Comparing Organisms Genetically 427 12.5 Software Development with Wolfram Workbench 431 12.6 Compute Unified Device Architecture (CUDA) 434 12.7 Connecting with Other Programs and Devices 434 12.8 Additional Resources 435 Index 437 Preface Mathematica and the Wolfram Language If you have used Mathematica occasionally or heard of it, you may have the false impression that it is a program for performing complicated calculations, usually for academic purposes. However, this idea is far from the truth. Actually, Mathematica is much more than that. By putting together the computational power and ease of use of the Wolfram Language, Mathematica’s high-level general-purpose programming language, the program can be used in any scientific or technical field: aerospace engineering, environmental sciences, financial risk management, medical imaging and many others. Mathematica can be considered a tool that empowers non-professional programmers to develop applications, although if you are a professional programmer, you will see that the software provides a development environment similar to the ones available for C++ or Python. You can even use the program to control external devices. About the Book Although many books have been written about Mathematica, very few of them cover the new functionality added to the most recent versions of the program. This text introduces the new features using real-world examples, based on the experience of the author as a consultant. In the process, you will also learn more about the Wolfram Language and how you can use it to solve a wide variety of problems. Both are the most important objectives of the book. To accomplish that, the author raises questions from a wide range of topics and answers them by taking full advantage of Mathematica’s latest features. For example: What is the hidden image in “The Ambassadors” painting by Holbein? What sources of energy does the world really use? How can we calculate tolerance limits in manufacturing processes? Are our cities getting warmer? Is the novel “El Quijote” written in Pi? How do we know how old our planet is? How can we find planets outside our solar system? How big is our galaxy? And the universe? How do we know it? How can