chapter 7 topics

11
EGR115 Introduction to Computing for Engineers Chapter 7 MATLAB Live Script & Symbolic Mathematics S. Hayashibara, ERAU College of Engineering (Prescott) Chapter 7 Topics Introduction (1): MATLAB Symbolic Math Toolbox Introduction (2): MATLAB Live Script (Live Editor) 7.1 Create Live Scripts in the Live Editor 7.2 Starting MATLAB Symbolic Math 7.3 Symbolic Numbers, Variables, & Expressions 7.4 Plotting Symbolic Function 7.5 Integration, Differentiation, & Limits 7.6 Units of Measurement 7.7 MATLAB Live Script Example 7.8 Symbolic Math (Live Script) Example

Upload: others

Post on 14-Feb-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

EGR115 Introduction to Computing for Engineers

Chapter 7

MATLAB Live Script & SymbolicMathematics

S. Hayashibara, ERAU College of Engineering (Prescott)

Chapter 7 Topics

➢ Introduction (1): MATLAB Symbolic Math Toolbox

➢ Introduction (2): MATLAB Live Script (Live Editor)

➢ 7.1 Create Live Scripts in the Live Editor

➢ 7.2 Starting MATLAB Symbolic Math

➢ 7.3 Symbolic Numbers, Variables, & Expressions

➢ 7.4 Plotting Symbolic Function

➢ 7.5 Integration, Differentiation, & Limits

➢ 7.6 Units of Measurement

➢ 7.7 MATLAB Live Script Example

➢ 7.8 Symbolic Math (Live Script) Example

MATLAB Toolboxes https://www.mathworks.com/help/matlab/matlab_prog/create-and-share-custom-matlab-toolboxes.html You can package MATLAB files to create a toolbox to share with others. These files can include MATLAB code, data, apps, examples, and documentation. When you create a toolbox, MATLAB generates a single installation file (.mltbx) that enables you or others to install your toolbox. The following list includes commonly used (widely-accepted and well-developed) MATLAB toolboxes: https://www.mathworks.com/products.html • Simulink (now this evolved into an independent product family of itself) • Symbolic Math Toolbox (now it is standard package of MATLAB default installation) • Application-Based Toolboxes

o Signal Processing o Image Processing & Computer Vision o Control Systems o Test & Measurement o RF & Mixed Signal o Wireless Communications o Autonomous Systems / Automotive / Aerospace o Computational Finance o Computational Biology o Code Verification

Chapter 7Slide 1 of 10

Introduction (1): MATLAB Symbolic Math Toolbox

CALCULUS:Integration | Differentiation | Series Expansion, Integral

Transformation, and Vector Analysis

EQUATION SOLVING, SIMPLIFICATION, AND

SUBSTITUTION:Solve Algebraic and Differential Equations | Simplify and

Rewrite Expressions | Substitute Variables

LINEAR ALGEBRA:Matrix Operation and Transformation | Solve Systems of

Linear Equations

VISUALIZATION:Analytical Plotting | Animation

VARIABLE-PRECISION ARITHMETIC:Precision and Computing Speed

UNITS AND DIMENSIONAL ANALYSIS:Define, Create, and Convert Units | Perform Dimensional

Analysis

DOCUMENTATION AND SHARING:Interactive Computations | Share MATLAB Code and Live

Scripts

CODE GENERATION:Generate Code: for MATLAB, Simulink | for Simscape, C,

Fortran, LaTex, and MathML

MATLAB Symbolic Math Toolbox Symbolic Math Toolbox provides functions for solving, plotting, and manipulating symbolic math equations. You can create, run, and share symbolic math code using the MATLAB Live Editor. The toolbox provides functions in common mathematical areas such as calculus, linear algebra, algebraic and ordinary differential equations, equation simplification, and equation manipulation. Symbolic Math Toolbox lets you analytically perform differentiation, integration, simplification, transforms, and equation solving. You can also perform dimensional computations and unit conversions using SI and US unit systems. Your computations can be performed either analytically or in variable-precision arithmetic with the results displayed in mathematical typeset. MATLAB Live Editor MATLAB Live Scripts are program files that contain your code, output, and formatted text together in a single interactive environment called the Live Editor. In live scripts, you can write your code and view the generated output and graphics along with the code that produced it. You can add formatted text, images, hyperlinks, and equations to create an interactive narrative that you can share with others. You can share your symbolic work with other MATLAB users as live scripts or convert them to HTML or PDF for publication. Important Note • MATLAB Script is basically a simple (ASCII) text file (.m), while Live Script is a MATLAB

specific (.mlx) file. Live script requires "true line-by-line interpreter" that means execution could be slow and often may not be suitable for regular numeric math type of analysis.

Chapter 7Slide 2 of 10

SHARE YOUR WORK:Add interactive controls to allow others to experiment with

parameters in your code. Hide your code to create simple

applications and dashboards. Publish live scripts as HTML,

PDF, LaTeX, or Microsoft Word. Create documentation for

live functions with formatted text, images, hyperlinks, and

equations. Share your work through MATLAB Online and

MATLAB Drive™

GET TO YOUR RESULTS MORE QUICKLY:MATLAB helps you code with contextual hints for function

arguments, file names, and more. Use interactive tools to

explore figures and tables in the output. Then get

automatically generated code to reproduce your changes.

Select blocks of code to create reusable functions. Use the

fully integrated debugger to troubleshoot your code.

COMPLETE STEPS INTERACTIVELY:Use Tasks in the Live Editor to complete steps in your

analysis. Interactively explore parameters and options and

immediately see the results. Generate code in the script for

the completed task and preview. Save a Live Editor Task

as part of the live script for sharing or subsequent use.

MATLAB Live Script + MATLAB Symbolic Math Toolbox= Extremely Powerful Engineering Analytical Tool

Important Note • Fundamentally, MATLAB Symbolic Math Toolbox and MATLAB Live Script are independent

and different components of MATLAB. It is certainly possible to perform "symbolic math without live editor (means: in regular editor)," or "numeric (non-symbolic) math in live editor." Of course, these are possible . . . typically, however, these are poor practices.

EXERCISE 7.1: Create Live Scripts in the Live Editor

Chapter 7Slide 3 of 10

7.1 Create Live Scripts in the Live Editor

https://www.mathworks.com/help/matlab/matlab_prog/create-live-scripts.html

MATLAB Live Editor: Let’s get started . . .

Create Live Script

Add Code

Run Code

Display Output

Format Text

EXERCISE 7.2: Symbolic Math with Live Editor in MATLAB

Chapter 7Slide 4 of 10

7.2 Starting MATLAB Symbolic Math

SYMBOLIC MATH with LIVE EDITOR in MATLABMATLAB has a powerful symbolic math capability. Rather than making calculations

based on known variables (numeric math), we can make calculations based on

symbols and symbolic expressions (symbolic math).

WHY SYMBOLIC MATH?MATLAB allows you to create symbolic math expressions, just like you do so in your

piece of paper and a pen. If your analysis does not need to immediately compute

numerical results (rather, you need symbolic results), this is useful. If you want to work

on mathematical formula (i.e., simplify, substitute, differentiate, integrate, . . ., etc.),

this is useful. Also, MATLAB allows you to use appropriate engineering units,

associated with your analysis.

WHY LIVE EDITOR?As you work on symbolic math analysis, you strongly prefer an instant result display of

mathematical expression, line-by-line. If your work involves symbolic math, it is best if

MATLAB responds interactively as you move on with your analytical work. MATLAB

Live Editor is simply the best tool for Symbolic Math type of analysis.

EXERCISE 7.3: Symbolic Math with Live Editor (Continued)

Chapter 7Slide 5 of 10

7.3 Symbolic Numbers, Variables, & Expressions

Create Symbolic NumbersYou can create symbolic numbers by using sym.

Create Symbolic VariablesYou can create symbolic variables using either syms or sym.

Create Symbolic Expressions & MatricesYou can create symbolic expressions & matrices with numbers and variables.

Add Subscripts, Superscripts, & AccentsYou can create symbolic expressions & matrices with numbers and variables.

EXERCISE 7.4a: Plotting Symbolic Function (Part a)

EXERCISE 7.4b: Plotting Symbolic Function (Part b)

Chapter 7Slide 6 of 10

7.4 Plotting Symbolic Function

EXERCISE 7.5: Performing Symbolic Mathematics

Chapter 7Slide 7 of 10

7.5 Integration, Differentiation, & Limits

i ere i e i e re i i di

i e r e i e re i i i t

e er i e e i i i i i it

e er i e e e e i ve r ri e i s

i i e i e re i i si i

i v e i v ri e ev e i s s

EXERCISE 7.6: Units of Measurement

Slide 8 of 10

7.6 Units of Measurement

Define and Convert UnitsLoad units by using symunit.

Convert unit by using unitConvert.

Temperature Units (Absolute or Difference)Te er re i e ei er e (Ke vi r R ki e) r “ i ere e” (Ce i r

Fahrenheit), and can be converted.

Verify DimensionsYou can observe the units that appear in an expression by using findUnits.

Check if the units have the same dimensions by using checkUnits.

Units in Differential EquationsYou can use units in differential equations, just as in standard equations.

EXAMPLE 7-1 (NONSENSE) EXAMPLE 7-1 (BETTER)

Chapter 7Slide 9 of 10

7.7 MATLAB Live Script Example

EXAMPLE 7-1 (Basic Engineering Units)

Consider a low-speed commercial aircraft (Cessna) in flight at a standard sea-level equivalent

atmospheric condition, but under the gravitational acceleration of 27 ft/s2 (note: this is not

standard condition of gravity at sea-level). Over the wing of this aircraft, the air pressure and

temperature are measured as: 0.7 atm (absolute) and 20 F, respectively. Calculate the

corresponding specific weight of air at that point, both in SI (“kilograms per cubic

centimeters”) and U.S. Customary (“pounds per cubic inches”) units.

EXAMPLE 7-1 (THE BEST)?

Solve this EXAMPLE 7-1 using

SMath Studio (and you should be

able to experience how awesome this

software really is . . . ?)

?

Do-It-Yourself (DIY) Exercise 7-1

CLASSIC (Paper & Pen) Engineering Solution:

EXAMPLE 7-2 (Solution by Symbolic Math in MATLAB Live Script): ==> EXAMPLE 7-2 (Solution by SMath Studio)

Chapter 7Slide 10 of 10

7.8 Symbolic Math (Live Script) Example

EXAMPLE 7-2 (Engineering Mechanics: Statics)

Determine the distance to the center of mass of the

homogeneous rod bent into the shape shown.