You are on page 1of 53

MATLAB Language and

Scientific Computation
Dingyu Xue
Northeastern University, Shenyang, China
Email: xuedingyu@mail.neu.edu.cn
Materials:
https://mechatronics.ucmerced.edu/Scientific-Computing-with-MATLAB-2ndEd

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 1/53


with MATLAB, 2nd Edition. CRC Press, 2016
MATLAB & Scientific
Computation
□ Textbook
Xue Dingyu and YangQuan Chen. Scientific
Computing with MATLAB, 2nd Edition, CRC
Press, 2016
Chinese version, 3rd edition, 2013, 4th edition is
Scheduled for 2016/2017
Full set of MOOC in Chinese is almost ready for
releasing.

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 2/53


with MATLAB, 2nd Edition. CRC Press, 2016
Target of the Course

□ Learn a new computer language MATLAB


□ Revisit and extend knowledge to all the
math courses
Concentrate on problem solutions
Three-phase solution strategy:
WHAT – HOW – SOLVE
Avoid trivial mathematics, get problem solved
with leading edge computer tools
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 3/53
with MATLAB, 2nd Edition. CRC Press, 2016
Why Mathematics and
Computation So Important?
□ Mathematics are essential in almost all
science and engineering branches
□ Research in science and engineering may
usually turn out to be mathematical models
How to build the models?
How to solve the models?
□ With Math, explore unknown world
□ Scared of mathematics? A new solution!
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 4/53
with MATLAB, 2nd Edition. CRC Press, 2016
Chapter 1
Computer Mathematical
Languages: An Overview
Scientific Computing with
MATLAB, 2nd Edition
CRC/Taylor & Francis Press
Chinese version by Tsinghua University Press

PPT by Wenbin Dong and Jun Peng, Northeastern University, PRC


Proofread by Dingyu Xue & YangQuan Chen

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 5/53


with MATLAB, 2nd Edition. CRC Press, 2016
Chapter 1: Computer
Mathematical Languages: An
Overview
□ Computer Solutions to Mathematics Problems
□ Summary of Computer Mathematics Languages
□ Outlines of the Book
□ A Guide to Online Resources

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 6/53


with MATLAB, 2nd Edition. CRC Press, 2016
1.1 Computer Solutions to
Mathematics Problems
□ Why studying computer mathematics language?
□ Analytical solutions versus numerical solutions
□ Mathematics software packages: an overview

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 7/53


with MATLAB, 2nd Edition. CRC Press, 2016
1.1.1 Why Should We Study
Computer Mathematics Languages?

□ Ways of solving math problems


Manual derivations
Using a computer
Using numerical methods and writing code
from the low level
Applying the existing software
Analytical solutions versus numerical solutions
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 8/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.1 Derivatives
□ College math question: Given a function

□ How to find

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 9/53


with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.2 Polynomial equations
□ Abel-Ruffini Theorem: There is no general
solution in radicals to polynomial equations of
degree five or higher.
□ Numerical Method
Lin-Bairstrow’s method
http://mathworld.wolfram.com/BairstowsMethod.html
□ Solve the polynomial equation

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 10/53


with MATLAB, 2nd Edition. CRC Press, 2016
Polynomial equation
□ Equation

□ Double-precision solutions

□ Error analysis

□ Symbolic solutions

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 11/53


with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.3 Determinants
□ Matrix determinant (行列式)
Linear algebra: algebraic complement method
Determinant of nxn matrix to n determinants
of (n-1)x(n-1) matrices
Theory: analytical solution exists
□ Complexity neglected
Flops: n=25
1535 years on fastest mainframes in the world
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 12/53
with MATLAB, 2nd Edition. CRC Press, 2016
Hilbert matrix
□ Elimination method should be used, even with
symbolic methods
□ Example n=20, Hilbert matrix

□ Double-precision
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 13/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ Analytical solutions

□ Exact Results

4198377534864930331853312344197593106445
8518758576681657377344056575986726555897
1765638419710793303386582324149811241023
5544891661547178096352577978368000000000
00000000000000000000000000
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 14/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.4 Differential Equations
□ Differential equation solutions
Van der Pol equation, no analytical solution

Stiff equation
Delay differential equation

Fractional-order differential equation

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 15/53


with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.5 Optimization
□ Linear programming problem (线性规划)

□ MATLAB code

□ Mixed integer programming


5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 16/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.6 Math Branches
□ College Math Courses
Calculus, Linear algebra,
Integral transform, Complex variable functions,
Ordinary/partial differential equations,
Data interpolation and fitting,
Probability and mathematical statistics
□ Can the students still remember how to solve
the problems after the final exams?
□ Problems to face in later work
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 17/53
with MATLAB, 2nd Edition. CRC Press, 2016
Other Courses Related to
Mathematics and Computations
□ Other math topics related to mathematics
Integral Transformation, Functions of Complex
Variables, Partial Differential Equations, Data
Interpolation and Fitting, Probability Theory and
Statistics, Numerical Analysis
□ Other courses related to this textbook
Electrical Circuit Analysis, Electronic Techniques,
Power Electronics Techniques, Electrical Machines
and Drives, Automatic Control Theory etc.
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 18/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.1.2 Analytical Solutions versus
Numerical Solutions
□ Mathematicians versus engineers
Mathematicians: Prove the theory strictly, existence
and uniqueness issues;
Engineers: Get a useful and reliable solution.
□ When analytical solutions cannot be used
Nonexistence, e.g.,

Mathematicians’ solution: introduce a notation erf(a)


Engineers’ solution: a look-up table (LUT) -
numerical solution for an approximate value!
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 19/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ When analytical solutions cannot be used
Nonexistence: irrational number, p
Mathematicians: Obtain an accurate value with as
much as possible digits
Engineers: Get a value accurate enough.
Zu, Chongzhi’s 3.1415926, Archimedes’ 3.1418
http://mathworld.wolfram.com/Pi.html
□ When analytical solution exists but too
computationally expensive to be useful
Determinant formula (via minors) for high
dimensional matrix
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 20/53
with MATLAB, 2nd Edition. CRC Press, 2016
Applications of Numerical
Solutions
□ Mechanics – finite element methods for PDEs
□ Aerospace, aeronautical and automatic control –
Numerical linear algebra, numerical methods for ODEs
□ Engineering and non-engineering system simulation –
Simulation core includes simulation of
difference/differential equations etc.
□ High-tech digital signal processing – Discrete fast
Fourier transform is ubiquitous.
□ ……
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 21/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.1.3 Math Software Packages:
An Overview

□ Internationally renowned packages


□ Math Software Packages: Comments
□ Example: eigenvalue problem
□ Limitations of C Languages by Examples
□ Mathematical library supports

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 22/53


with MATLAB, 2nd Edition. CRC Press, 2016
Internationally Renowned
Packages
□ LINPACK http://www.netlib.org/linpack
□ EISPACK http://www.netlib.org/eispack
□ NAG (Oxford: Numerical Algorithm Group)
http://www.nag.com
□ Press W H, et al. “Numerical Recipes, The
Art of Scientific Computing”. Cambridge:
Cambridge University Press, 1986
(www.nr.com)
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 23/53
with MATLAB, 2nd Edition. CRC Press, 2016
Math Software Packages: Comments

□ Historically important
□ Strong support for computer math language
□ Not to be over dependent on these packages
– too low level for average researchers.
□ Tedious and rigid to use
□ Should only be just a part of the “computer
math language” but not all.
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 24/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example: Eigenvalue Problem
□ With EISPACK

□ Too difficult and error-prone


□ MATLAB solution: eig(A)
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 25/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.1.4 Limitations in C
Programming
□ Symbolic computation?
□ Graphics with C?
□ Numerical solutions only
Cannot adopt the best algorithms
Lengthy programs, error-prone
Unreliable, cannot trust the results, hard to
validate
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 26/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.8 Fibonacci Sequence
□ Fibonacci sequence generation

C code

Correct? Too much low-level things to consider


5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 27/53
with MATLAB, 2nd Edition. CRC Press, 2016
MATLAB Solution
□ Simple MATLAB statements

□ Exact computation

□ How to store it in C? what data structure?


5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 28/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1.9 Subroutine for
Matrix Multiplication
□ Write a universal subroutine: A*B
□ C code, main part

□ Correct?
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 29/53
with MATLAB, 2nd Edition. CRC Press, 2016
Possible Errors or Bugs
□ Did not consider whether A*B exist
Modify code
 if (L1==L2) multiply, otherwise error
□ Induced error: scalar not considered
More if else statements
□ Any problem?
Complex matrices not considered
□ MATLAB implementation: C = A*B
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 30/53
with MATLAB, 2nd Edition. CRC Press, 2016
Example 1-10 Plot and Surface

□ Sinusoidal curves

□ Three-dimensional plots

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 31/53


with MATLAB, 2nd Edition. CRC Press, 2016
Example 1-11 Implicit
Functions Plotting
□ An implicit function

□ How to draw?
MATLAB implementation

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 32/53


with MATLAB, 2nd Edition. CRC Press, 2016
Example 1-12 Beautiful Plots
□ Fractals --- Mandelbrot sets

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 33/53


with MATLAB, 2nd Edition. CRC Press, 2016
Mathematical Library Supports

□ EISPACK
□ LINPACK
□ The leading-edge LaPACK
□ Provides support to mathematical software
and languages.
MATLAB and
Scilab

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 34/53


with MATLAB, 2nd Edition. CRC Press, 2016
1.2 Summary of Computer
Mathematics Languages
□ A brief historic review of MATLAB
□ Three widely used computer mathematics
languages
□ Introduction to Free Scientific Open-source
Software
□ Advantages of MATLAB language
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 35/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.2.1 A Brief Historic Review
of MATLAB
□ Cleve Moler
Professor
Head Computer Science Dept
New Mexico University
1978, MATLAB
Named for MATrix LABoratory
□ 1984, Jack Little and C Moler co-founded
The MathWorks, Inc., to develop MATLAB
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 36/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ 1984, first release
□ Most recent release, 2014b
□ Release new version biannually, a,b

2008 with Jack Little, Beijing


MathWorks Beijing Office Open 2012 with Cleve Moler, Shanghai

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 37/53


with MATLAB, 2nd Edition. CRC Press, 2016
1.2.2 Three Widely Used
Computer Math Languages
□ Three Representatives:
MATLAB, Mathematica, Maple
□ MATLAB
Numerical computation, programming
□ Mathematica, Maple
Pure mathematics; programming with extensive
pattern matching operations
□ MATLAB+ (Extended) Symbolic Math Toolbox
Symbolic formula derivation , MuPAD
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 38/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.2.3 Introduction to Free Scientific
Open-source Software
□ MATLAB etc. are Commercial
□ Scilab developed/maintained by INRIA,France
http://www.scilab.org/
□ Octave was first released in 1993
http://www.gnu.org/software/octave/
□ Others Small-scale numerical matrix software
Freemat, http://freemat.sourceforge.net/wiki/index.php/Main_Page
SpeQ, http://www.speqmath.com/index.php?id=1
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 39/53
with MATLAB, 2nd Edition. CRC Press, 2016
Advantages of MATLAB
□ Easy to program, similar to other language, C
□ Highly integrated with better expansion capability
□ Powerful numerical methods for math problems
□ Symbolic computation
□ Numerous toolboxes in different math and engineering
and non-engineering fields.
□ Powerful dynamic system simulation and the GUI
based Simulink modeling
□ Number one language in automatic control community.
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 40/53
with MATLAB, 2nd Edition. CRC Press, 2016
1.3 Outlines of the Book
□ Chapter-1 (This chapter). Overview of MATLAB and
other Computer Math Languages.
□ Chapter-2. Programming Fundamentals of MATLAB
□ Chapter-3. Calculus Problems
□ Chapter-4. Linear Algebra Problems
□ Chapter-5. Integral Transformation and Functions of
Complex Variable
□ Chapter-6. Algebraic Equations and Optimization
Problems

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 41/53


with MATLAB, 2nd Edition. CRC Press, 2016
□ Chatper-7. Differential Equation Problems
□ Chapter-8. Numerical Interpolation and Functional
Approximation Problems
□ Chatper-9. Probability and Statistics Problems
□ Chapter-10. Non-traditional Solution Methods
 Fuzzy Logic and Fuzzy Inference
 Neural Network for Data Fitting Applications
 Genetic Algorithm for Optimization Applications
 Wavelet Theory for Data Processing.
 Rough Set Theory and Its Applications.
 Fractional-Order Calculus and Its Computational Techniques
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 42/53
with MATLAB, 2nd Edition. CRC Press, 2016
Detailed Contents of Each
Chapter
□ Chapter 2 Fundamentals of MATLAB
Programming, briefly introduces
Programming essentials of MATLAB
Data structure
flow control structures
M-function programming
Two-dimensional graphics
Three-dimensional graphics
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 43/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 3: Calculus Problems, covers
the problems in college calculus, from a
different viewpoint.
Limits, derivatives and integrals of single-variable
and multivariable functions.
Series expansion, including Taylor series, Fourier
series, series sums and products
Numerical differentiation and integration
Path integral, line integral and surface integrals

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 44/53


with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 4: Linear Algebra Problems,
studies linear algebra problems using both
analytical and numerical methods
Special matrices input
Basic matrix analysis, Matrix transformation,
Matrix decomposition
Matrix equation solutions, including linear
equations, Lyapunov equation and Riccati
equations
Matrix functions, exponential function and the
functions of arbitrary forms.
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 45/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 5: Integral Transforms and
Complex Variable Functions,
including
Laplace transform and its inverse
Fourier transform and its variation
Z transform and its inverse
Mellin and Hankel transforms
The analysis of complex variable functions are
also introduced, including poles, residues, partial
fraction expansion and closed-path integral
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 46/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 6: Nonlinear Equations and
Optimization Problems
Search methods for linear, nonlinear equations and
matrix equations
Unconstrained optimization
Constrained optimization, linear programming,
quadratic programming and nonlinear
programming
Mixed integer programming and binary
programming
Linear matrix inequality

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 47/53


with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 7: Differential Equations
Problems, mainly covers
Analytical solutions to ordinary differential
equations
Numerical solutions to ordinary differential
equations, including stiff equations, implicit
equations, differential algebraic equations, delay
differential equations
Boundary valued equations
Partial differential equations
Simulink modeling and block diagrams

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 48/53


with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 8: Data
Interpolation and
Functional Approximation Problems,
with the following topics
Interpolation, 1D, 2D and multi-D
Cubic spline and B-spline
Numerical differentiation and integration
Polynomial fitting, continued fraction expansion
and Padé approximation, least squares curve fitting
Fast Fourier transform, signal filtering and
de-noising problems
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 49/53
with MATLAB, 2nd Edition. CRC Press, 2016
□ Chapter 9: Probability
and
Mathematical Statistics Problems,
Probability distributions, PDF, CDF and inverse
Pseudo-random number generators
Statistical analysis, mean, variance, moments,
covariance and correlation analysis
Hypothesis tests, mean, normality and arbitrary
given distribution
Analysis of variance, 1D and 2D

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 50/53


with MATLAB, 2nd Edition. CRC Press, 2016
A Guide to Online Resources

□ The MathWorks official website


Products and full-set of toolbox manuals.
http://www.mathworks.cn
□ Third-party contributions: File-Exchange
 http://www.mathworks.com/matlabcentral/fileexchange/
□ User Forum
http://www.mathworks.com/matlabcentral/newsreader

5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 51/53


with MATLAB, 2nd Edition. CRC Press, 2016
More Materials for the Chapter

□ Cleve Moler’s talk in Tongji University, 2012,


Evolution of MATLAB
 Video, Xue Dingyu subtitled
□ Dingyu Xue’s presentation, Mathematics
Education Made More Practical with MATLAB,
PPT, MathWorks Asian Summit, Tokyo, 2014
□ Both downloadable from
http://pan.baidu.com/s/1pJzAczT :edpi
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 52/53
with MATLAB, 2nd Edition. CRC Press, 2016
Product Information
□ For MATLAB product information, please
contact:
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA, 01760-2098 USA
Tel: 508-647-7000
Fax: 508-647-7101
E-mail: info@mathworks.com
Web: http://www.mathworks.com
5/19/2018 Dingyü Xue and YangQuan Chen. Scientific Computing 53/53
with MATLAB, 2nd Edition. CRC Press, 2016

You might also like