chapter 7. symbolic math toolbox
TRANSCRIPT
1 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Chapter 7. Symbolic Math Toolbox
2 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Outline
• 7.1 Introduction
• 7.2 Symbolic Math Toolbox Overview
• 7.3 Getting Started
• 7.4 Basic Operations
• 7.5 Linear Algebra and Calculus
• 7.6 Solving Algebraic and Differential Equations
• 7.7 Simplification and Substitution
• 7.8 Other Functions
• 7.9 Plotting Symbolic Expressions
• 7.10 Using Symbolic Expressions in Numerical Operations
• 7.11 LaTeX Representation of Symbolic Expression
• 7.12 Symbolic Calculators
• 7.13 Units of Measurements in Symbolic Math Toolbox (optional)
Engineering Computations and Modeling in MATLAB/Simulink
3 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
SMT History
Originally, the computational engine underlying the
Symbolic Math toolbox was the Maple kernel,
developed at the University of Waterloo in Canada.
Before R2007b, the Basic Symbolic Math toolbox
included in the Student Version was a collection of
more than a hundred MATLAB functions allowing
access to the functions in Maple’s linear algebra
package. The Extended Symbolic Math toolbox
augmented the basic toolbox by including access to
non-graphics packages, programming features and
user defined procedures in Maple.
As of R2007b+, Symbolic Math Toolbox - now a single
product - uses the MuPAD computational engine,
originally developed by the MuPAD research group at
the University of Paderborn, Germany. The
functionality of the MuPAD language, together with
included libraries, goes far beyond that of the previous
Symbolic Math Toolbox software. The functionality of
Extended Symbolic Math Toolbox has been taken over
by MuPad environment (MuPAD Notebook).
4 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Areas Supported by SMT
Subject Areas Supported
Linear AlgebraInverses, determinants, eigenvalues, singular value decomposition, canonical
forms of symbolic matrices
Calculus Differentiation, integration, limits, summation, and Taylor series
Solving (Systems of) Equations Symbolic and numerical solutions to algebraic and differential equations
Formula Manipulation and
Simplification
Simplification or modification of symbolic expressions, substitution of parts
of expressions
Integral Transforms Fourier, Laplace, z- transforms and their corresponding inverse transforms
Polynomials Characteristic and minimal polynomials, coefficients of polynomials
Numbers and Precision Variable-precision arithmetic and elementary number-theoretic operations
Number Theory Modular arithmetic operations and sequences of numbers
Special Functions
Special functions in applied mathematics such as Airy functions, binomial
coefficients, error functions, Bessel functions, Dirac delta function, Legendre
polynomials, Jacobian, and Chebyschev functions
Plotting Symbolic Expressions Easy 2-D and 3-D plotting of symbolic expressions
5 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
SMT Examples
6 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Basic Operations
a=sym('a');
syms x y
Y=(x^a+sqrt(x))/y
pretty(Y)
symvar(Y)
Y =
(x^a + x^(1/2))/y
a
x + sqrt(x)
------------
y
ans =
[ a, x, y]
Arithmetic Operations
+ Addition
- Subtraction
* Multiplication
.* Array multiplication
\ Left division
.\ Array left division
/ Right division
./ Array right division
^ Matrix or scalar raised to a power
.^ Array raised to a power
’ Complex conjugate transpose
.’ Real transpose
Basic Operations
ceil Round symbolic matrix toward positive infinity
conj Symbolic complex conjugate
eq Symbolic equality test
fix Round toward zero
floor Round symbolic matrix toward negative infinity
frac Symbolic matrix elementwise fractional parts
imag Imaginary part of complex number
latex LaTeX representation of symbolic expression
log10 Logarithm base 10 of entries of symbolic matrix
Iog2 Logarithm base 2 of entries of symbolic matrix
mod Symbolic matrix elementwise modulus
pretty Pretty-print symbolic expressions
quorem Symbolic matrix elementwise quotient and remainder
real Real part of imaginary number
round Symbolic matrix elementwise round
size Symbolic matrix dimensions
sort Sort symbolic vectors or polynomials
sym Symbolic numbers, variables, and objects
syms Shortcut for constructing symbolic objects
symvar Find symbolic variables in symbolic expression or matrix
… and other functions
7 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Linear Algebra & Calculus
syms a11 a22 a21 a22
inv([a11 a22; a21 a22])
syms x k a
diff(cos(x^2))
int(cos(x)^2,0,pi)
limit(sin(x)/x)
symsum(x^k/factorial(k),k,0,inf)
taylor(sin(x-a),x,a,'Order',10)
Linear Algebra
colspace Basis for column space
det Symbolic matrix determinant
diag Create or extract symbolic diagonals
eig Symbolic eigenvalues and eigenvectors
expm Symbolic matrix exponential
inv Symbolic matrix inverse
jordan Jordan canonical form
null Basis for null space
poly Characteristic polynomial of matrix
rank Symbolic matrix rank
rref Reduced row echelon form
svd Symbolic singular value decomposition
tril Symbolic lower triangle
triu Symbolic upper triangle
… and other functions
Calculus
diff Differentiate symbolic expression
int Integrate symbolic expression
gradient Gradient vector of a scalar function
hessian Hessian matrix of a scalar function
jacobian Jacobian matrix
laplacian Laplacian of a scalar function
limit Limit of symbolic expression
symsum Symbolic summation of series
taylor Taylor series expansion
… and other functions
( )
0
( )( ) ( )
!
nn
n
f af x x a
n
=
= −
2(cos( ))d x
dx
2
0
cos ( )x dx
0
sin( )limx
x
x→
0 !
k
k
x
k
=
8 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Solving Equations
syms a b c x, solve(a*x^2+b*x+c,x)
syms x y, [X,Y]=solve([5*x+6*y==8,2*x+3*y==1])
syms a x y, f=solve([a*x^2+y^2-1,x+sqrt(y)-1],[x,y]);
[f.x f.y]
dsolve('Dx-a*x=sin(t)','x(0)=1')
g=dsolve('Dx=y','Dy=cos(t)','x(0)=1','y(0)=0');
[g.x g.y]
dsolve('D3x=x-1','x(0)=0','Dx(0)=2','D2x(0)=0')
Key Functions
solve Symbolic solution of algebraic equations
dsolve Symbolic solution of ordinary differential equations
syms x(t); D1=diff(x); D2=diff(x,2);
dsolve(diff(x,3)==x-1,[x(0)==0,D1(0)==2,D2(0)==0])
Dx – the 1st-order derivative
D2x – the 2nd-order derivative
D3x – the 3rd-order derivative
solving a single equation with
respect to x
solving two equations with
respect to x and y
solving two equations with
respect to x and y
ob
so
lete
syn
tax
syms x y, [A,b]=equationsToMatrix([x+2*y-3,2x+3*y]); z=A\b alternative approach
9 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
syms y1(t) y2(t)
% Define system parameters (in kg and kg/s/s)
m1=1; m2=1; k1=3; k2=2;
% Define equations
eqn1 = m1*diff(y1,t,2) == -k1*y1+k2*(y2-y1);
eqn2 = m2*diff(y2,t,2) == -k2*(y2-y1);
% Define initial conditions (in m and m/s)
Dy1 = diff(y1,t); Dy2 = diff(y2,t);
cond = [y1(0)==1, Dy1(0)==-2*sqrt(6),...
y2(0)==2, Dy2(0)==sqrt(6)];
% Find a solution
Sol = dsolve([eqn1 eqn2],cond);
% Plot the results within t=[0;10]s
subplot(221), ezplot(Sol.y1,[0 10]), ylabel('y_1(t), m')
subplot(223), ezplot(Sol.y2,[0 10]), ylabel('y_2(t), m')
subplot(222), ezplot(diff(Sol.y1),[0 10]), ylabel('$$\dot{y}_1(t)$$, m/s','interpreter','latex')
subplot(224), ezplot(diff(Sol.y2),[0 10]), ylabel('$$\dot{y}_2(t)$$, m/s','interpreter','latex')
Dynamic Systems Modeling
m2mm22m1mm11
yy22((tt)) yy11((tt))
kk11kk22
1 1 1 1 2 2 1
2 2 2 2 1
( )
( )
m y k y k y y
m y k y y
= − + −
= − −
A system of differential equations governing an
oscillatory motion of two-body mass–spring system on a
horizontal frictionless surface
1 1
2 2
1 2 6 /
2 6 /
y m y m s
y m y m s
= = −
= =
2 2
1 2 1 21 3 / 2 /m m kg k kg s k kg s= = = =
10 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Linear and Nonlinear Equations and Systems
equationsToMatrix Converts a set of linear equations to the matrix form
finverse Performs a functional inverse
linsolve Solves a linear system of equations given in the matrix form
poles Finds the poles of an expression or function
solve Solves an equation or system of equations symbolically
vpasolve Solves an equation or system of equations numerically
Ordinary Differential Equations and Systems
dsolve Solves an ordinary differential equation or system of equations
massMatrixForm Extracts the mass matrix and right side of semi-linear system of differential algebraic equations (DAEs)
odeFunctionConverts a system of symbolic algebraic expressions to a MATLAB function handle suitable for ode45, ode15s, and
other ODE solvers
odeToVectorField Converts higher-order differential equations to systems of the 1st-order differential equations
Differential Algebraic Equations
daeFunction Converts a system of DAEs to a MATLAB function handle suitable for the ode15i solver
decic Finds consistent initial conditions for the 1st-order implicit ODE system with algebraic constraints
findDecoupledBlocks Searches for decoupled blocks in systems of equations
incidenceMatrix Finds an incidence matrix of system of equations
isLowIndexDAE Checks if the differential index of system of equations is lower than 2
massMatrixForm Extracts the mass matrix and right side of semi-linear system of DAEs
reduceDAEIndex Converts a system of the 1st-order DAEs to the equivalent system of differential index 1
reduceDAEToODE Converts a system of the 1st-order semi-linear DAEs to equivalent system of differential index 0
reduceDifferentialOrder Reduces a system of higher-order differential equations to equivalent system of the 1st-order differential equations
reduceRedundancies Simplifies a system of the 1st-order DAEs by eliminating redundant equations and variables
The Full List of Equation
Solving Functions
For advanced users
11 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Simplification & Substitution
syms x
y=cos(2*x)
simple(y)
children Extracting subexpressions or terms of symbolic expression
coeffs Determining coefficients of multivariate polynomial
collect Collecting coefficients
combine Combining the terms of an identical algebraic structure
compose Performing functional composition
isolate Isolating a variable or expression in equation
expand Symbolic expansion of polynomials and elementary functions
factor Factorization
horner Horner nested polynomial representation
numden Extracting numerator and denominator
lhs, hrs Extracting the left- and right-hand sides of equation
rewrite Rewriting an expression in terms of another function
simplifyFraction Simplifying symbolic rational expressions
simple Search for simplest form of symbolic expression
simplify Symbolic simplification
subexpr Rewriting a symbolic expression in terms of common subexpressions
subs Symbolic substitution
syms x y rho phi R a
zdc=(x-a)^2+y^2-R^2;
zpc=subs(zdc,{x,y},{rho*cos(phi),rho*sin(phi)})
simplify(zpc)
The simple function was removed after R2014b
syms x
y=cos(2*x); expand(y)
ans = 2*cos(x)^2 - 1ans = - R^2 + a^2 - 2*cos(phi)*a*rho + rho^2
syms x
[c,t]=coeffs(16*x^2 + 19*x + 11)
c = [ 16, 19, 11]
t = [ x^2, x, 1]
syms y(x)
f=y^2*x+y*x^2+y*sin(x)+x*y;
coefy=collect(f,y)
coefy(x)=x*y(x)^2+(x+sin(x)+x^2)*y(x)
Simplification and Substitution
syms x
sin2tan=rewrite(sin(x),'tan')
sin2tan=(2*tan(x/2))/(tan(x/2)^2+1)
T = [ -x*y, x^2, -y^2]
syms x y
T=children(x^2-x*y-y^2)
12 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Integral & z-Transforms
syms a t
laplace(t*exp(-a*t))
syms w x y
ilaplace(w/(y^2 + w^2),y)
Integral Transforms
fourier Fourier integral transform
ifourier Inverse Fourier integral transform
ztrans z-transform transform
iztrans Inverse z-transform
laplace Laplace transform
ilaplace Inverse Laplace transform
1( ) ( )
2
i tf t F e d
−
=
( ) ( )i t
F f t e dt
−
−
=
11( ) ( ) , 1, 2,...
2
n
z R
f n F z z dz ni
−
=
= =0
( )( )
nn
f nF z
z
=
=
0
( ) ( )st
F s f t e dt
−=
( ) ( )
c i
st
c i
f t F s e ds
+
−
=
ans =
1/(a + s)^2
ans =
sin(w*y)
13 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Other Functions
syms x
f=taylor(sin(x),'Order',4)
t=inline(char(f));
t(5)
f =
- x^3/6 + x
ans =
-15.8333
Conversions
ccode C code representation of symbolic expression
char Convert symbolic objects to strings
double Convert symbolic matrix to MATLAB numeric form
emlBlock Convert symbolic expression to MATLAB Function block
fortran Fortran representation of symbolic expression
int8, int16, int32, int64 Convert symbolic matrix to signed integers
latex LaTeX representation of symbolic expression
matlabFunction Convert symbolic expression to function handle or file
poly2sym Polynomial coefficient vector to symbolic polynomial
single Convert symbolic matrix to single precision
sym2poly Symbolic-to-numeric polynomial conversion
uint8, uint16, uint32, uint64 Convert symbolic matrix to unsigned integers
Variable Precision Arithmetic
digits Variable precision accuracy
vpa Variable precisian arithmetic
Special Functions
cosint Cosine integral
dirac Dirac delta
gamma Gamma function
heaviside Heaviside step function
hyperqeom Generalized hypergeometric
lambertw Lambert's W function
mfun Numeric evaluation of special mathematical function
mfunlist List special functions for use with mfun
sinint Sine integral
zeta Riemann Zeta
… and other functions
14 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
ezcontour Contour plotter
ezcontourf Filled contour plotter
ezmesh 3D mesh plotter
ezmeshc Combined mesh and contour plotter
ezplot 2D parametric curve plotter
ezplot3 3D parametric curve plotter
ezpolar Polar coordinate plotter
ezsurf 3D colored surface plotter
ezsurfc Combined surface and contour plot
Easy Visualization Tools &
Calculators
syms x(t) y(t)
g=dsolve([diff(x)==y,diff(y)==cos(t)],[x(0)==1, y(0)==0]);
ezplot(g.x,g.y)
syms r phi
x=r*cos(phi); y=r*sin(phi);
z=1+besselj(1,3*r)*cos(phi);
ezmesh(x,y,z)
Easy Plotting Functions(may be removed in the future releases)
Graphical Calculatorsfuntool Function calculator
rsums Interactive evaluation of Riemann sums
taylortool Taylor series calculator
15 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
z=[1 3 4]; x=poly(z);
fs=poly2sym(x)
fc=char(fs)
xz=fzero(fc,1.82)
fa=str2func(['@(x)' fc])
fplot(fa,[0 5]), grid
hold, plot(xz,fa(xz),'rp')
Symbolic to Character &
Function Handle
fs = x^3-8*x^2+19*x-12
fc = 'x^3-8*x^2+19*x-12'
xz = 1.0000
fa = @(x)19*x-8*x^2+x^3-12
16 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
finl =
Inline function:
finl(t) = exp(t)*co(t)
xmin = 3.9270
Current plot held
syms t
fsym=cos(t)*exp(t)
ezplot(fsym) % it works! :-)
xmin=fminsearch(fsym,3) % it doesn’t work :-(
finl=inline(char(fsym))
xmin=fminsearch(finl,3) % it works now :-)
hold, plot(xmin,finl(xmin),'rp')
Symbolic to Inline Function
fsym = exp(t)*cos(t)
Error using fcnchk (line 107)
If FUN is a MATLAB object, it must have an feval method.
Error in fminsearch (line 173)
funfcn = fcnchk(funfcn,length(varargin));
1
2
1
2
3
3
1
3
17 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
inline vs. fcnchk
syms t
fsym=cos(t)*exp(t)
ezplot(fsym)
finl=fcnchk(char(fsym)) % fcnchk is equivalent to inline
xmin=fminsearch(finl,3) % it works now :-)
hold, plot(xmin,finl(xmin),'rp')
ezplot('sin(2*x)')
ezplot('sin(2*t)')
syms t
fsym=cos(t)*exp(t);
finl=fcnchk(char(fsym),'vectorized')
finl =
Inline function:
finl(t) = cos(t).*exp(t).*ones(size(t))
What’s the difference?
finl=inline(vectorize(char(fsym)))
fzero('sin(2*x)',3)
fzero('sin(2*t)',3)
18 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
The matlabFunction
Function>> syms ps M
>> q = 0.7*ps*M^2;
>> hq = matlabFunction(q)
hq =
@(M,ps)M.^2.*ps.*(7.0./1.0e1)
syms lat lon
Rlon = [cos(lon) sin(lon) 0; -sin(lon) cos(lon) 0; 0 0 1];
Rlat = [cos(lat) 0 sin(lat); 0 1 0; -sin(lat) 0 cos(lat)];
R = Rlat*Rlon;
F = matlabFunction(R,'file','RotationMatrix.m')
function R = RotationMatrix(lat,lon)
%ROTATIONMATRIX
% R = ROTATIONMATRIX(LAT,LON)
% 13-Nov-2018 19:59:41
t2 = cos(lat);
t3 = sin(lon);
t4 = cos(lon);
t5 = sin(lat);
R = reshape([t2.*t4,-t3,-t4.*t5,t2.*t3,t4,-t3.*t5,t5,0.0,t2],[3,3]);
Creates a function
handle F to the
RotationMatrix
function saved as
RotationMatrix.m
>> M = [0.2,0.2,0.6]; ps = [101325, 60000, 101325];
>> hq(M,ps)
ans =
1.0e+04 *
0.2837 0.1680 2.5534
19 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
LaTeX Representation of
Symbolic Expression
>> syms x y t
>> F1 = diff(atan(x))
>> F2 = sym(hilb(3))
>> F4 =(x^2+1/x)/sqrt((sin(x)+cos(x))/x^3.5)>> F5 = taylor(sin(x),'Order',8,'ExpansionPoint',pi/2)
>> H1=latex(F1);
>> H2=latex(F2);
>> H4=latex(F4);
>> H5=latex(F5);>> text(0.2,0.25,['$' H1 '$'],'interpreter','latex','fontsize',14)
>> text(0.6,0.2, ['$' H2 '$'],'interpreter','latex','fontsize',16)
>> text(0.3,0.5, ['$' H4 '$'],'interpreter','latex','fontsize',18)
>> text(0.1,0.8, ['$' H5 '$'],'interpreter','latex','fontsize',14)
F1 =
1/(x^2 + 1)
F2 =
[ 1, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]
F4 =
(1/x + x^2)/((cos(x) + sin(x))/x^(7/2))^(1/2)
F5 =
(x - pi/2)^4/24 - (x - pi/2)^2/2 - (x - pi/2)^6/720 + 1
20 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Using odeToVectorField
Function
%% Integrate numerically within 0<t<2s
initConditions=[0,-3,5,-3]; % set initial conditions
ode23(odefun,[0,2],initConditions)
grid, xlabel('Time, s'), ylabel('Solution')
legend('$x(t)$','$\dot{x}(t)$','$\ddot{x}(t)$','$x^{(iii)}(t)$',...
'location','ne','interpreter','latex','fontsize',13)
( )2 10 0
ivy y yy+ − =
odefun =
@(t,Y)[Y(2);Y(3);Y(4);Y(3).*-2.0+Y(1).*Y(2).*1.0e1]
1 2
2 3
3 4
4 3 1 2
( )
( )
( )
2 10 ( 2 10 )
Y Y y
Y Y y
Y Y y
Y Y Y Y y yy
= =
= =
= =
= − + = − +
%% Rewrite equation into state space
[V,S]=odeToVectorField(eqn)
odefun=matlabFunction(V,'vars',{'t','Y'})
%% Define the 4th-order ODE
syms y(t)
eqn=diff(y,4)+2*diff(y,2)-10*y*diff(y);
V = Y[2]
Y[3]
Y[4]
10*Y[1]*Y[2] - 2*Y[3]
S = y
Dy
D2y
D3y
For advanced users
21 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Using odeFunction
Function to Create a Handle
%%
%% Define the 4th-order ODE
syms y(t)
eqn=diff(y,4)+2*diff(y,2)-10*y*diff(y);
%% Rewrite ODE as a system of 1st-order
ODEs
[eqs,vars]=reduceDifferentialOrder(eqn,y)
%% Find the mass matrix M and the right-
hand sides of equations
% $M(t,x(t),\dot{x}(t)=F(t,x(t))$
[M,F]=massMatrixForm(eqs,vars)
%% Rewrite the system in the standard form
% $\dot{x}(t)=f(t,x(t))$
f=M\F
%% Convert to a MATLAB function handle
odefun=odeFunction(f,vars);
For advanced users
22 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Riemann Sums Calculator>> syms x
>> rsums(sin(pi*x))
23 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Function Calculator>> funtool
24 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Taylor Series Calculator>> taylortool
>> taylortool('f')
25 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
%%
%% Define units
u=symunit; % loading units collection
d=5*u.fps % using foot per second
w=50*u.lbm % using pound mass
s=10*u.km/u.hr, % using length and time units
s=simplify(d) % simplifying to (default) speed unit
%% Combine the different-unit values
d=500*u.m+0.9*u.mi+70*u.ft+157*u.in
d=simplify(d) % simplifying to (default) single unit
d=separateUnits(d); % extracting expression with no units
d=double(d) % converting symbolic variable to a double-
precision number
%% Unit conversions
len=1*u.mi; % define 1 statute mile
len=unitConvert(len,u.inm) % convert to international nautical miles
len=unitConvert(len,u.nmile) % convert to British imperial nautical
miles
len=unitConvert(len,u.km) % convert to kilometers
[len,un]=separateUnits(len); % separate units from expression
len=double(len) % convert expression to double type
un % show units
%% Convert to SI or U.S. unit system
m=5*u.kg;
a=2*u.m/u.s^2;
F=m*a
F=unitConvert(F,'SI','Derived')
F=unitConvert(F,'US')
F=unitConvert(F,'US','Derived')
%% Use units in differential equations
syms V(t) a v0
T=t*u.s; % time in seconds
A=a*u.m/u.s^2; % acceleration in meters per second
eqn1=A==diff(V,T) % differential equation
cond=V(0)==v0*u.m/u.s; % initial condition
eqn2=V==dsolve(eqn1,cond)
SMT Units of Measurements
For advanced users
Introduced in R2017a
26 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
Unit Manipulations
checkUnits Check for compatible dimensions and consistent units
findUnits Find units in input
isUnit Determine if input is a symbolic unit
mixedUnits Split unit into sum of units
newUnit Define new unit
removeUnit Remove unit
rewrite Rewrite expression in terms of another function
separateUnits Separate units from expression
simplify Algebraic simplification
str2symunit Convert character vector or string to unit
symunit Units of measurement
symunit2str Convert unit to character vector
unitConversionFactor Conversion factor between units
unitConvert Convert units to other units of measurement
unitInfo Information on units of measurement
baseUnits Base units of unit system
derivedUnits Derived units of unit system
newUnitSystem Define unit system
removeUnitSystem Remove unit system
unitSystems List available unit systems
Unit Systems
Define, Create, Convert Units
For advanced users
27 out of 27Engineering Computations and Modeling in MATLAB/Simulink
All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means,
or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of
Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807.
The End of Chapter 7
Questions?