Page
1
Symbolic Math Toolbox™ User's Guide R2023b
Page
2
How to Contact MathWorks Latest news: www.mathworks.com Sales and services: www.mathworks.com/sales_and_services User community: www.mathworks.com/matlabcentral Technical support: www.mathworks.com/support/contact_us Phone: 508-647-7000 The MathWorks, Inc. 1 Apple Hill Drive Natick, MA 01760-2098 Symbolic Math Toolbox™ User's Guide © COPYRIGHT 1993–2023 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the government's needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to The MathWorks, Inc. Trademarks MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. Patents MathWorks products are protected by one or more U.S. patents. Please see www.mathworks.com/patents for more information.
Page
3
Revision History August 1993 First printing October 1994 Second printing May 1997 Third printing Revised for Version 2 May 2000 Fourth printing Minor changes June 2001 Fifth printing Minor changes July 2002 Online only Revised for Version 2.1.3 (Release 13) October 2002 Online only Revised for Version 3.0.1 December 2002 Sixth printing June 2004 Seventh printing Revised for Version 3.1 (Release 14) October 2004 Online only Revised for Version 3.1.1 (Release 14SP1) March 2005 Online only Revised for Version 3.1.2 (Release 14SP2) September 2005 Online only Revised for Version 3.1.3 (Release 14SP3) March 2006 Online only Revised for Version 3.1.4 (Release 2006a) September 2006 Online only Revised for Version 3.1.5 (Release 2006b) March 2007 Online only Revised for Version 3.2 (Release 2007a) September 2007 Online only Revised for Version 3.2.2 (Release 2007b) March 2008 Online only Revised for Version 3.2.3 (Release 2008a) October 2008 Online only Revised for Version 5.0 (Release 2008a+) October 2008 Online only Revised for Version 5.1 (Release 2008b) November 2008 Online only Revised for Version 4.9 (Release 2007b+) March 2009 Online only Revised for Version 5.2 (Release 2009a) September 2009 Online only Revised for Version 5.3 (Release 2009b) March 2010 Online only Revised for Version 5.4 (Release 2010a) September 2010 Online only Revised for Version 5.5 (Release 2010b) April 2011 Online only Revised for Version 5.6 (Release 2011a) September 2011 Online only Revised for Version 5.7 (Release 2011b) March 2012 Online only Revised for Version 5.8 (Release 2012a) September 2012 Online only Revised for Version 5.9 (Release 2012b) March 2013 Online only Revised for Version 5.10 (Release 2013a) September 2013 Online only Revised for Version 5.11 (Release 2013b) March 2014 Online only Revised for Version 6.0 (Release 2014a) October 2014 Online only Revised for Version 6.1 (Release 2014b) March 2015 Online only Revised for Version 6.2 (Release 2015a) September 2015 Online only Revised for Version 6.3 (Release 2015b) March 2016 Online only Revised for Version 7.0 (Release 2016a) September 2016 Online only Revised for Version 7.1 (Release 2016b) March 2017 Online only Revised for Version 7.2 (Release 2017a) September 2017 Online only Revised for Version 8.0 (Release 2017b) March 2018 Online only Revised for Version 8.1 (Release 2018a) September 2018 Online only Revised for Version 8.2 (Release 2018b) March 2019 Online only Revised for Version 8.3 (Release 2019a) September 2019 Online only Revised for Version 8.4 (Release 2019b) March 2020 Online only Revised for Version 8.5 (Release 2020a) September 2020 Online only Revised for Version 8.6 (Release 2020b) March 2021 Online only Revised for Version 8.7 (Release 2021a) September 2021 Online only Revised for Version 9.0 (Release 2021b) March 2022 Online only Revised for Version 9.1 (Release 2022a) September 2022 Online only Revised for Version 9.2 (Release 2022b) March 2023 Online only Revised for Version 9.3 (Release 2023a) September 2023 Online only Revised for Version 23.2 (R2023b)
Page
4
(This page has no text content)
Page
5
Getting Started 1 Symbolic Math Toolbox Product Description . . . . . . . . . . . . . . . . . . . . . . . 1-2 Create Symbolic Numbers, Variables, and Expressions . . . . . . . . . . . . . . . 1-3 Create Symbolic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Create Symbolic Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Use Existing Symbolic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Generate Elements While Creating a Matrix . . . . . . . . . . . . . . . . . . . . . . 1-11 Create Matrix of Symbolic Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Create Symbolic Matrix Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Use Symbolic Objects to Represent Mathematical Objects . . . . . . . . . . . 1-20 Symbolic Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20 Symbolic Scalar Variable, Function, and Expression . . . . . . . . . . . . . . . . 1-21 Symbolic Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21 Symbolic Vector and Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22 Symbolic Matrix Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23 Symbolic Matrix Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24 Comparison of Symbolic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-25 Perform Symbolic Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-28 Differentiate Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-28 Integrate Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-29 Solve Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-30 Simplify Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-32 Substitutions in Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 1-33 Plot Symbolic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-35 Use Assumptions on Symbolic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 1-41 Computational Mathematics in Symbolic Math Toolbox . . . . . . . . . . . . . 1-44 Next Step Suggestions for Symbolic Workflows in Live Editor . . . . . . . . 1-57 v Contents
Page
6
Symbolic Computations in MATLAB 2 Find Symbolic Variables in Expressions, Functions, and Matrices . . . . . . 2-2 Find a Default Symbolic Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Choose syms or sym Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Change Output Format of Symbolic and Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8 Change Output Display Format of Symbolic Results in the Live Editor . 2-11 Add Subscripts, Superscripts, and Accents to Symbolic Variables in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Copy and Paste Symbolic Output in Live Editor . . . . . . . . . . . . . . . . . . . . 2-18 Check Symbolic Equations, Inequalities, and Conditional Statements . 2-23 Numeric to Symbolic Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29 Choose Numeric or Symbolic Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . 2-32 Double-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32 Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32 Symbolic Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33 Comparisons of Numeric and Symbolic Arithmetic . . . . . . . . . . . . . . . . . 2-34 Increase Precision of Numeric Calculations . . . . . . . . . . . . . . . . . . . . . . . 2-36 Compute Binomial Coefficients Exactly . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-38 Find Almost Integers with High-Precision Arithmetic . . . . . . . . . . . . . . . 2-41 Decimal Digits of PI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-45 Units of Measurement Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-47 Unit Conversions and Unit Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-53 Convert Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-53 Temperature Unit Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-54 Convert to SI, CGS, or US Unit Systems . . . . . . . . . . . . . . . . . . . . . . . . . 2-54 Define Custom Unit System from Existing System . . . . . . . . . . . . . . . . . . 2-56 Define Custom Unit System Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-57 Unit System Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-58 Units and Unit Systems List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-60 Units List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-60 SI Unit Prefixes List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-70 Unit Systems List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-71 Defining Constants of SI Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-72 Units in Physics Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-74 vi Contents
Page
7
The Physics of the Damped Harmonic Oscillator . . . . . . . . . . . . . . . . . . . 2-78 Evaluating the Average Power Delivered by a Wind Turbine . . . . . . . . . . 2-88 Developing an Algorithm for Undistorting an Image . . . . . . . . . . . . . . . . 2-93 Electric Dipole Moment and Radiation Power . . . . . . . . . . . . . . . . . . . . . 2-99 Validate Simulink Model Using Symbolic Math Toolbox . . . . . . . . . . . . 2-102 Mathematics 3 Solve Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Solve an Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Return the Full Solution to an Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Work with the Full Solution, Parameters, and Conditions Returned by solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4 Visualize and Plot Solutions Returned by solve . . . . . . . . . . . . . . . . . . . . . 3-5 Simplify Complicated Results and Improve Performance . . . . . . . . . . . . . . 3-6 Solve System of Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 Troubleshoot Equation Solutions from solve Function . . . . . . . . . . . . . . 3-17 Return Only Real Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17 Apply Simplification Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17 Use Assumptions to Narrow Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18 Simplify Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19 Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19 Solve Equations Numerically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Solve System of Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Solve System of Linear Equations Using linsolve . . . . . . . . . . . . . . . . . . . 3-30 Solve System of Linear Equations Using solve . . . . . . . . . . . . . . . . . . . . 3-31 Select Numeric or Symbolic Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33 Solve Parametric Equations in ReturnConditions Mode . . . . . . . . . . . . . 3-34 Solve Algebraic Equation Using Live Editor Task . . . . . . . . . . . . . . . . . . . 3-36 Solve Differential Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43 First-Order Linear ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43 Solve Differential Equation with Condition . . . . . . . . . . . . . . . . . . . . . . . 3-43 Nonlinear Differential Equation with Initial Condition . . . . . . . . . . . . . . . 3-44 Second-Order ODE with Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . 3-44 Third-Order ODE with Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . . . 3-44 More ODE Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-45 Solve a System of Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 3-47 vii
Page
8
Solve a Second-Order Differential Equation Numerically . . . . . . . . . . . . 3-52 Solve Partial Differential Equation of Tsunami Model . . . . . . . . . . . . . . . 3-54 Solve Differential Algebraic Equations (DAEs) . . . . . . . . . . . . . . . . . . . . . 3-61 Solve Semilinear DAE System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-70 Solve DAEs Using Mass Matrix Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-77 Step 1. Convert DAEs to Function Handles . . . . . . . . . . . . . . . . . . . . . . . 3-77 Step 2. Find Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-78 Step 3. Solve DAE System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-79 Analyze and Manipulate Differential Algebraic Equations . . . . . . . . . . . 3-82 Derive and Apply Inverse Kinematics to Two-Link Robot Arm . . . . . . . . 3-89 Gauss-Laguerre Quadrature Evaluation Points and Weights . . . . . . . . . . 3-96 Simulate a Stochastic Process Using the Feynman–Kac Formula . . . . . 3-106 The Black–Scholes Formula for Call Option Price . . . . . . . . . . . . . . . . . 3-114 Choose Function to Rearrange Expression . . . . . . . . . . . . . . . . . . . . . . . 3-119 Combine Terms of Same Algebraic Structures . . . . . . . . . . . . . . . . . . . 3-119 Expand Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-120 Factor Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-121 Extract Subexpressions from Expression . . . . . . . . . . . . . . . . . . . . . . . 3-123 Collect Terms with Same Powers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-123 Rewrite Expressions in Terms of Other Functions . . . . . . . . . . . . . . . . . 3-124 Compute Partial Fraction Decompositions of Expressions . . . . . . . . . . . 3-125 Compute Normal Forms of Rational Expressions . . . . . . . . . . . . . . . . . . 3-125 Represent Polynomials Using Horner Nested Forms . . . . . . . . . . . . . . . 3-126 Extract Numerators and Denominators of Rational Expressions . . . . . 3-127 Simplify Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-129 Simplify Using Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-130 Simplify Using Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-132 Simplify Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-132 Simplify Symbolic Expressions Using Live Editor Task . . . . . . . . . . . . . 3-134 Substitute Variables in Symbolic Expressions . . . . . . . . . . . . . . . . . . . . 3-140 Substitute Elements in Symbolic Matrices . . . . . . . . . . . . . . . . . . . . . . . 3-142 Substitute Scalars with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-144 Evaluate Symbolic Expressions Using subs . . . . . . . . . . . . . . . . . . . . . . . 3-146 Abbreviate Common Terms in Long Expressions . . . . . . . . . . . . . . . . . . 3-147 Harmonic Analysis of Transfer Function Output . . . . . . . . . . . . . . . . . . 3-150 viii Contents
Page
9
Explore Single-Period Asset Arbitrage . . . . . . . . . . . . . . . . . . . . . . . . . . 3-156 Analytical Solutions of the Inverse Kinematics of a Humanoid Robot 3-160 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-171 Derivatives of Expressions with Several Variables . . . . . . . . . . . . . . . . . 3-172 More Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-173 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-176 Integration with Real Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-178 Integration with Complex Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 3-179 High-Precision Numerical Integration Using Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-180 Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-182 Fourier and Inverse Fourier Transforms . . . . . . . . . . . . . . . . . . . . . . . . . 3-184 Fourier Transform Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-184 Concept: Using Symbolic Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-184 Calculate Beam Deflection Using Fourier Transform . . . . . . . . . . . . . . . 3-184 Solve Differential Equations of RLC Circuit Using Laplace Transform 3-188 Solve Difference Equations Using Z-Transform . . . . . . . . . . . . . . . . . . . . 3-195 Symbolic Summation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-200 Comparing symsum and sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-200 Computational Speed of symsum versus sum . . . . . . . . . . . . . . . . . . . . 3-200 Output Format Differences Between symsum and sum . . . . . . . . . . . . . 3-201 Padé Approximant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-202 Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-209 One-Sided Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-209 Find Asymptotes, Critical, and Inflection Points . . . . . . . . . . . . . . . . . . 3-212 Functional Derivatives Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-217 Learn Calculus in the Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-222 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-227 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-235 Interactive Calculus in Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-239 Maxima, Minima, and Inflection Points . . . . . . . . . . . . . . . . . . . . . . . . . . 3-241 Find Extremum of Multivariate Function and Its Approximation . . . . . 3-250 Padé Approximant of Time-Delay Input . . . . . . . . . . . . . . . . . . . . . . . . . . 3-254 Solve Partial Differential Equation of Nonlinear Heat Transfer . . . . . . 3-260 ix
Page
10
Symbolic Matrix Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-267 Linear Algebraic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-274 Symbolic Hilbert Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-274 Symbolic Linear Algebra Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-274 Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-275 Symbolic Investigation of Singular Value . . . . . . . . . . . . . . . . . . . . . . . 3-276 Basic Algebraic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-278 Singular Value Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-280 Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-282 Jordan Canonical Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-286 Eigenvalues of the Laplace Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-288 Hilbert Matrices and Their Inverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-298 Markov Chain Analysis and Stationary Distribution . . . . . . . . . . . . . . . 3-302 Matrix Rotations and Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 3-307 Clear Assumptions and Reset the Symbolic Engine . . . . . . . . . . . . . . . . 3-314 Check Assumptions Set on Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-315 Effects of Assumptions on Computations . . . . . . . . . . . . . . . . . . . . . . . . 3-316 Recognize and Avoid Round-Off Errors . . . . . . . . . . . . . . . . . . . . . . . . . . 3-317 Use Symbolic Computations When Possible . . . . . . . . . . . . . . . . . . . . . 3-317 Perform Calculations with Increased Precision . . . . . . . . . . . . . . . . . . . 3-317 Compare Symbolic and Numeric Results . . . . . . . . . . . . . . . . . . . . . . . . 3-319 Plot the Function or Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-319 Increase Speed by Reducing Precision . . . . . . . . . . . . . . . . . . . . . . . . . . 3-321 Prime Factorizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-323 Handling Large Integers to Solve the 196 Problem . . . . . . . . . . . . . . . . 3-326 Graphics 4 Create Plots of Symbolic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Transform Spherical Coordinates to Cartesian Coordinates and Plot Analytically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Tangent Plane and Normal Line of Implicit Surface . . . . . . . . . . . . . . . . 4-17 Analytical Plotting with Symbolic Math Toolbox . . . . . . . . . . . . . . . . . . . 4-21 x Contents
Page
11
Simulate the Motion of the Periodic Swing of a Pendulum . . . . . . . . . . . 4-38 Animation and Solution of Double Pendulum Motion . . . . . . . . . . . . . . . 4-53 Animation and Model of Automotive Piston . . . . . . . . . . . . . . . . . . . . . . . 4-60 Code Generation 5 Generate C or Fortran Code from Symbolic Expressions . . . . . . . . . . . . . . 5-2 Generate MATLAB Functions from Symbolic Expressions . . . . . . . . . . . . . 5-3 Generating a Function Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Control the Order of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Generate a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 Name Output Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 Generate MATLAB Function Blocks from Symbolic Expressions . . . . . . . 5-6 Generate and Edit a Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Control the Order of Input Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Name the Output Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Generate Simscape Equations from Symbolic Expressions . . . . . . . . . . . . 5-8 Convert Algebraic and Differential Equations . . . . . . . . . . . . . . . . . . . . . . 5-8 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Deploy Generated MATLAB Functions from Symbolic Expressions with MATLAB Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10 Generate C Code from Symbolic Expressions Using the MATLAB Coder App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16 Derive Quadrotor Dynamics for Nonlinear Model Predictive Control . . . 5-30 Using Symbolic Mathematics with Optimization Toolbox Solvers . . . . . . 5-37 Improving Accuracy and Performance in Optimization . . . . . . . . . . . . . . 5-46 Analytical Model of Cantilever Truss Structure for Simscape . . . . . . . . . 5-52 Customize and Extend Simscape Libraries for a Custom DC Motor . . . . 5-60 Estimate Model Parameters of a Symbolically Derived Plant Model in Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-69 xi
Page
12
MuPAD to MATLAB Migration 6 MuPAD Engines and MATLAB Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Convert MuPAD Notebooks to MATLAB Live Scripts . . . . . . . . . . . . . . . . . 6-3 Convert a MuPAD Notebook .mn to a MATLAB Live Script .mlx . . . . . . . . . 6-3 Convert MuPAD Graphics to MATLAB Graphics . . . . . . . . . . . . . . . . . . . . 6-4 Known Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4 Troubleshoot MuPAD to MATLAB Translation Errors . . . . . . . . . . . . . . . . . 6-8 Troubleshoot MuPAD to MATLAB Translation Warnings . . . . . . . . . . . . . 6-15 Differences Between MATLAB and MuPAD Syntax . . . . . . . . . . . . . . . . . . 6-20 Call Built-In MuPAD Functions from MATLAB . . . . . . . . . . . . . . . . . . . . . 6-22 evalin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 feval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 evalin vs. feval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23 Floating-Point Arguments of evalin and feval . . . . . . . . . . . . . . . . . . . . . 6-23 Functions 7 xii Contents
Page
13
Getting Started • “Symbolic Math Toolbox Product Description” on page 1-2 • “Create Symbolic Numbers, Variables, and Expressions” on page 1-3 • “Create Symbolic Functions” on page 1-9 • “Create Symbolic Matrices” on page 1-11 • “Create Symbolic Matrix Variables” on page 1-13 • “Use Symbolic Objects to Represent Mathematical Objects” on page 1-20 • “Perform Symbolic Computations” on page 1-28 • “Use Assumptions on Symbolic Variables” on page 1-41 • “Computational Mathematics in Symbolic Math Toolbox” on page 1-44 • “Next Step Suggestions for Symbolic Workflows in Live Editor” on page 1-57 1
Page
14
Symbolic Math Toolbox Product Description Perform symbolic math computations Symbolic Math Toolbox provides functions for solving, plotting, and manipulating symbolic math equations. You can create, run, and share symbolic math code. In the MATLAB Live Editor, you can get next-step suggestions for symbolic workflows. The toolbox provides functions in common mathematical areas such as calculus, linear algebra, algebraic and differential equations, equation simplification, and equation manipulation. Symbolic Math Toolbox lets you analytically perform differentiation, integration, simplification, transforms, and equation solving. You can perform dimensional computations and convert between units. Your computations can be performed either analytically or using variable-precision arithmetic, with the results displayed in mathematical typeset. You can share your symbolic work with other MATLAB users as live scripts or convert them to HTML, Word, LaTex or PDF documents. You can generate MATLAB functions, Simulink® Function blocks, and Simscape™ equations directly from symbolic expressions. 1 Getting Started 1-2
Page
15
Create Symbolic Numbers, Variables, and Expressions This example shows how to create symbolic numbers, variables, and expressions. To learn how to work with symbolic math, see “Perform Symbolic Computations” on page 1-28. Create Symbolic Numbers with Exact Representations You can create symbolic numbers by using sym. Symbolic numbers are exact representations, unlike floating-point numbers. Create symbolic numbers by using sym and compare them to the same floating-point numbers. a1Sym = sym(1/3) a1Sym = 1 3 a1 = 1/3 a1 = 0.3333 a2Sym = sym(pi) a2Sym = π a2 = pi a2 = 3.1416 The symbolic numbers are represented in exact rational form, while the floating-point numbers are decimal approximations. Calculations on symbolic numbers are exact. Demonstrate this exactness by finding sin(pi) symbolically and numerically. The symbolic result is exact, while the numeric result is an approximation. bSym = sin(sym(pi)) bSym = 0 b = sin(pi) b = 1.2246e-16 When you use sym on a numeric input, the numeric expression is first evaluated to the MATLAB® default double-precision number that can be less accurate. Then, sym is applied on that double- precision number. To represent an exact number without evaluating it to double precision, use a character vector with quotes. For example, create a symbolic number to represent a very large integer exactly. inaccurateNum = sym(123456789012345678) inaccurateNum = 123456789012345680 accurateNum = sym('123456789012345678') accurateNum = 123456789012345678 Create Symbolic Numbers, Variables, and Expressions 1-3
Page
16
You can also create symbolic complex numbers, by specifying the imaginary part of a number as 1i, 2i, and so on. sym('1234567 + 1i') ans = 1234567 + i To learn more about symbolic representation of numbers, see “Numeric to Symbolic Conversion” on page 2-29. Create Symbolic Numbers with Variable Precision You can create symbolic numbers with variable-precision floating-point arithmetic by using vpa. By default, vpa calculates values to 32 significant digits. piVpa = vpa(pi) piVpa = 3.1415926535897932384626433832795 When you use vpa on a numeric expression, such as log(2), the expression is first evaluated to the MATLAB default double-precision number that has less than 32 significant digits. Then, vpa is applied on that double-precision number, which can be less accurate. For more accurate results, convert double-precision numbers in an expression to symbolic numbers with sym and then use vpa to evaluate the results with variable precision. For example, find log(2) with 17- and 20- digit precision. vpaOnDouble = vpa(log(2)) vpaOnDouble = 0.69314718055994528622676398299518 vpaOnSym_17 = vpa(log(sym(2)),17) vpaOnSym_17 = 0.69314718055994531 vpaOnSym_20 = vpa(log(sym(2)),20) vpaOnSym_20 = 0.69314718055994530942 When you convert large numbers, use quotes to represent them exactly. inaccurateNum = vpa(123456789012345678) inaccurateNum = 123456789012345680.0 accurateNum = vpa('123456789012345678') accurateNum = 123456789012345678.0 Create Symbolic Variables You can create symbolic variables using either syms or sym. Typical uses of these functions include: • sym – Create numbered symbolic variables, symbolic variables in MATLAB functions, or symbolic numbers whose values differ from their names in the MATLAB workspace. • syms – Create fresh symbolic variables for interactive symbolic workflows, that is, for symbolic variable creation at the MATLAB command line or in MATLAB live scripts. A fresh symbolic variable does not have any assumptions. 1 Getting Started 1-4
Page
17
The syms command is shorthand for the sym syntax, but the two functions handle assumptions differently. syms clears the assumptions when creating variables. However, recreating a variable using sym does not clear its assumptions. For more details about the differences of these two functions, see “Choose syms or sym Function” on page 2-4. Create the symbolic variables x and y using syms and sym, respectively. syms x y = sym('y') y = y The first command creates a symbolic variable x in the MATLAB workspace with the value x assigned to the variable x. The second command creates a symbolic variable y with the value y. With syms, you can create multiple variables in one command. Create the variables a, b, and c. syms a b c Create Array of Symbolic Variables If you want to create a MATLAB array of numbered symbolic variables, you can use the sym or the syms syntax. Use sym to create an array of many numbered symbolic variables. Clear the workspace. Create a row vector containing the symbolic variables a1, …, a10 and assign it to the MATLAB variable A. Display the variable in the MATLAB workspace. clear A = sym('a',[1 10]) A = a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 whos Name Size Bytes Class Attributes A 1x10 8 sym A is a 1-by-10 array of 10 automatically generated elements. These generated elements of A do not appear in the MATLAB workspace. Use syms to create many fresh symbolic variables with corresponding variable names in the MATLAB workspace. Clear the workspace. Create the fresh symbolic variables a1, ..., a10. Display the variables in the MATLAB workspace. clear syms a [1 10] whos Name Size Bytes Class Attributes a 1x10 8 sym a1 1x1 8 sym a10 1x1 8 sym a2 1x1 8 sym a3 1x1 8 sym a4 1x1 8 sym Create Symbolic Numbers, Variables, and Expressions 1-5
Page
18
a5 1x1 8 sym a6 1x1 8 sym a7 1x1 8 sym a8 1x1 8 sym a9 1x1 8 sym The MATLAB workspace contains 10 MATLAB variables in the workspace that are symbolic variables. The syms command is a convenient shorthand for the sym syntax, and its typical use is to create fresh symbolic variables for interactive symbolic workflows. Use the sym syntax to create the following: • Symbolic variables in MATLAB functions • Many numbered symbolic variables • Symbolic variable whose value differs from its name in the MATLAB workspace • Symbolic number, such as sym(3)/11 • Symbolic variable that inherits the assumptions from a previously used symbolic variable having the same name Create Symbolic Expressions Suppose you want to use a symbolic variable to represent the golden ratio φ = 1 + 5 2 . Use sym to create the golden ratio. phi = (1 + sqrt(sym(5)))/2; Now you can perform various mathematical operations on phi. For example: f = phi^2 - phi - 1 f = 5 2 + 1 2 2 − 5 2 − 3 2 Next, suppose you want to study the quadratic function f = ax2 + bx + c. First, create the symbolic variables a, b, c, and x. syms a b c x Then, create a symbolic expression f that represents the arithmetical expression ax2 + bx + c. f = a*x^2 + b*x + c f = a x2 + b x + c Solve the quadratic equation f = 0 for x by using solve. x_0 = solve(f == 0,x) x_0 = −b + b2− 4 a c 2 a −b− b2− 4 a c 2 a 1 Getting Started 1-6
Page
19
You can also apply a mathematical function to an arithmetical expression. For example, apply the Bessel function of the first kind J0 to the arithmetical expression f and find its derivative with respect to x. J_0 = besselj(0,f) J_0 = J0 a x2 + b x + c DJ_0 = diff(J_0,x) DJ_0 = − J1 a x2 + b x + c b + 2 a x To create a symbolic number in a symbolic expression, use sym. Do not use syms to create a symbolic expression that is a constant. For example, to create an expression whose value is 5, enter f = sym(5). The command f = 5 does not define f as a symbolic expression. You can also create symbolic expressions from strings by using str2sym when reading expressions from text files or when specifying numbers exactly. Reuse Names of Symbolic Objects If you set a variable equal to a symbolic expression and then apply the syms command to the variable, MATLAB removes the previously defined expression from the variable. For example, create a symbolic expression f. syms a b f = a + b f = a + b If you recreate f, then MATLAB removes the value a + b from the expression f. syms f f f = f You can use the syms command to clear variables of definitions that you previously assigned to them in your MATLAB session. syms clears the assumptions of the variables. These assumptions (which can be real, integer, rational, and positive) are stored separately from the symbolic object. However, recreating a variable using sym does not clear its assumptions. For more information, see “Use Assumptions on Symbolic Variables” on page 1-41. See Also sym | syms | vpa More About • “Create Symbolic Functions” on page 1-9 • “Create Symbolic Matrices” on page 1-11 • “Create Symbolic Matrix Variables” on page 1-13 • “Use Symbolic Objects to Represent Mathematical Objects” on page 1-20 Create Symbolic Numbers, Variables, and Expressions 1-7
Page
20
• “Choose syms or sym Function” on page 2-4 • “Perform Symbolic Computations” on page 1-28 • “Choose Numeric or Symbolic Arithmetic” on page 2-32 • “Add Subscripts, Superscripts, and Accents to Symbolic Variables in the Live Editor” on page 2- 14 1 Getting Started 1-8