Top Banner
Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic Computing [email protected] http://web.mit.edu/violeta/www/IAP2006
24

Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

Jan 30, 2018

Download

Documents

nguyenque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

Introduction to MATLAB

Violeta Ivanova, Ph.D.Educational Technology ConsultantMIT Academic Computing

[email protected]://web.mit.edu/violeta/www/IAP2006

Page 2: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Topics

MATLAB Interface and BasicsLinear Algebra and CalculusGraphicsProgrammingGraphical User InterfaceMath On the Web (optional)

Page 3: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Class Materials

On laptops download from:http://web.mit.edu/acmath/matlab/IntroMATLABOn Athena copy from locker acmathathena% add acmathathena% cp

/afs/athena.mit.edu/astaff/project/acmath/matlab/IntroMATLAB/LA_Calculus.tar .

Page 4: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Help in MATLAB

Command line help>> help command

e.g. help eig>> lookfor keyword

e.g. lookfor eigenvalue>> helpwin or helpdesk or doc

Desktop menuHelp->Help MATLAB

Page 5: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

MATLAB Help BrowserMATLAB

+ Mathematics+ Matrices and Linear Algebra

Function Summary+ Matrices in MATLAB+ Solving Linear Systems of Equations

Eigenvalues+ Differential Equations

+ Initial Value Problems for ODEsODE Function Summary

Other Toolboxes

Page 6: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

MATLAB Linear Algebra

MatricesLinear EquationsCurve Fitting

Page 7: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Scalar-Vector Math

A + 2: element-wise additionA * 2: element-wise multiplicationA + A: element-wise additionA .^2: element-wise exponentiationA ./2: element-wise division

Page 8: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Matrix Math

Operators + and ->> X=[x1 x2 x3]’; Y=[y1 y2 y3]’; X+Yans =

x1+y1x2+y2x3+y3

Page 9: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Matrix Math (continued)

Operators .*, ./, and .^>> A = [X.^2 X ones(3,1)] ans =

x12 x1 1x22 x2 1x32 x3 1

Page 10: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Matrix Math (continued)

Operators *, ^, /, and \ on matrices>> Ainv = A^(-1)>> C = Ainv * Y>> B = A \ Y

Page 11: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Solving Linear Equations

Example: Find the quadratic that passes through three points (x1,y1), (x2,y2), and (x3,y3), i.e. find (c1, c2, c3) such that:

y1 = c1x12 + c2x1 + c3

y2 = c1x22 + c2x2 + c3

y3 = c1x32 + c2x3 + c3

Page 12: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Solving Linear Equations (continued)

Matrix form: Y = ACSolution:

Define matrix A>> A = [X.^2 X.^1 X.^0];Solve matrix equation>> C = A \ Y

Page 13: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Curve Fitting

Example: Fit a polynomial through points (xi,yi).Solution using polyfit>> C1 = polyfit(X, Y, 1);>> C2 = polyfit(X, Y, 2);>> Cn = polyfit(X, Y, n);

Solution using matrix math>> Cn = An \ Y

Page 14: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Built-In Operators & FunctionsPolynomials>> polyfit>> polyval

Matrix math>> \>> ones>> diag>> eig>> rref

Page 15: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

More Built-In FunctionsMatrix analysis>> det(A) >> rank(A) >> trace(A)

Solving linear equations>> inv(A)>> linsolve(A,B)

Eigenvalues and singular values>> poly(A)

Page 16: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

MATLAB Calculus

Differential EquationsFunction HandlesIntegration

Page 17: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Differential EquationsOrdinary Differential Equations (ODE)

Differential-Algebraic Expressions (DAE)

MATLAB solvers for ODEs and DAEs>> ode45; ode23; ode113; ode23s …

y ' = f (t, y)

M (t, y)y ' = f (t, y)

Page 18: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

ODE and DAE SolversSyntax>> [T,Y] = solver(odefun,tspan,y0)

solver: ode45, ode23, etc.odefun: Function Handletspan: interval of integration vectorY0: vector of initial conditions[T, Y]: numerical solution in two vectors

Page 19: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

ODE ExampleProblem:Solution:C = 0.002; R = 150; q0 = -0.001;tspan = [0 : 0.01 : 100];[t,q] = ode45(@ode, tspan, q0)% --------------------function Dq = ode(t,q)Dq = -1/(R*C)*q

dq t( )dt

= −1

RCq t( )

Page 20: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

PDE SolverPartial Differential Equations (PDEs)

Elliptic and parabolice.g. Laplace’s equation:

Numerical PDE solver: pdepeSyntax:>> solution = pdepe(m, pdefun, icfun, bcfun, xmesh, tspan)Function handles: pdefun, icfun, bcfun

∂2V∂x2 +

∂2V∂y2 = 0

Page 21: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Integration

Polynomial integration

Analytical solution Built-in function polyint>> P = polyint(p); assumes C=k>> P = polyint(p, k); assumes C=k

p1∫ xn + ...+ pnx + pn+1dx = P1xn+1 + ...+ Pn+1x + C

Page 22: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

More Polynomial Integration

Area under a curve

Built-in function polyval>> P = polyint(p)>> area = polyval(P, b) - …

polyval(P, a)

p1a

b

∫ xn + p2xn−1...+ pn−1x

2 + pnx + pn+1dx

Page 23: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Differentiation

Polynomial differentiation

Built-in function polyder>> P = [P1 P2 … Pn C]

>> p = polyder(P)

ddx

(P1xn + ...+ Pnx + C) = p1x

n−1 + ...+ pn

Page 24: Introduction to MATLAB - MITweb.mit.edu/acmath/matlab/IntroMATLAB/HandoutLACalculus.pdf · Introduction to MATLAB Violeta Ivanova, Ph.D. Educational Technology Consultant MIT Academic

IAP 2006 Introduction to MATLAB: Linear Algebra and Calculus

Algebra and Calculus Exercises

Exercise One: example1.mMatrices, vectors, and matrix operatorsSystems of linear equationsEigenvalues and eigenvectors

Exercise Two: example2.mPolynomialsCurve fitting

Exercise Three: example3.mOrdinary differential equationsFunction handles and function M-files