Matlab: Advanced Applications · 2019. 10. 14. · MATLAB functions can be used to solve...

31
MATLAB: Advanced Applications with Tyler Dunbar Please log in to a computer, and then open MATLAB R2016a by clicking on “Start” and then typing “MATLAB” While MATLAB is opening, please visit the ULC’s “Additional Support” page and download the files for this class. matlabtraining.engr.wisc.edu Please check in at the front desk

Transcript of Matlab: Advanced Applications · 2019. 10. 14. · MATLAB functions can be used to solve...

  • MATLAB: Advanced Applicationswith Tyler Dunbar

    Please log in to a computer, and then open MATLAB R2016a by clicking on “Start” and then typing “MATLAB”

    While MATLAB is opening, please visit the ULC’s “Additional Support” page and download the files for this class. matlabtraining.engr.wisc.edu

    Please check in at the front desk

    http://matlabtraining.engr.wisc.edu/

  • About the ULC• Free drop-in tutoring

    Sunday-Thursday, 6:30pm-9:00pm

    • Supplemental Instruction & Study Tables

    We offer support for MATLAB and other courses for both programs!

    Go to engr.wisc.edu/ulc

    to learn more!

  • Today’s Topics● Solving linear and nonlinear equations● Solving overdetermined systems● Solving first-order differential equations● Solving second-order differential equations● Eigenvalues and Eigenvectors● Polynomial curve fitting● Advanced plotting● Toolboxes

  • Solving Linear EquationsExample: Loop Analysis of Electric Circuits: Find the current flowing in each branch of this circuit.

    𝐴𝐴𝑛𝑛×𝑛𝑛𝑥𝑥𝑛𝑛×1 = 𝑏𝑏𝑛𝑛×1 ⇒ 𝑥𝑥 = 𝐴𝐴−1𝑏𝑏

    𝑎𝑎11𝑥𝑥1 + 𝑎𝑎12𝑥𝑥2 + 𝑎𝑎13𝑥𝑥3 = 𝑏𝑏1𝑎𝑎21𝑥𝑥1 + 𝑎𝑎22𝑥𝑥2 + 𝑎𝑎23𝑥𝑥3 = 𝑏𝑏2𝑎𝑎31𝑥𝑥1 + 𝑎𝑎32𝑥𝑥2 + 𝑎𝑎33𝑥𝑥3 = 𝑏𝑏3

    𝑎𝑎11 𝑎𝑎12 𝑎𝑎13𝑎𝑎21 𝑎𝑎22 𝑎𝑎23𝑎𝑎31 𝑎𝑎32 𝑎𝑎33

    𝑥𝑥1𝑥𝑥2𝑥𝑥3

    =𝑏𝑏1𝑏𝑏2𝑏𝑏3

  • Step 1: Form a system of linear equations (using Kirchhoff's Voltage Law for each loop)

    Step 2: Write down the Matrix form

    1𝐼𝐼1 + 5 𝐼𝐼1 − 𝐼𝐼2 + 10 𝐼𝐼1 − 𝐼𝐼3 = 105 𝐼𝐼2 − 𝐼𝐼1 + 5𝐼𝐼2 + 5 𝐼𝐼2 − 𝐼𝐼3 = 0

    10 𝐼𝐼3 − 𝐼𝐼1 + 5 𝐼𝐼3 − 𝐼𝐼2 + 15𝐼𝐼3 = 0

    16𝐼𝐼1 − 5𝐼𝐼2 − 10𝐼𝐼3 = 10-5𝐼𝐼1 + 15𝐼𝐼2 − 5𝐼𝐼3 = 0−10𝐼𝐼1 − 5𝐼𝐼2 + 30𝐼𝐼3 = 0

    Collecting terms, this becomes:

    16 −5 −10−5 15 −5−10 −5 30

    𝐴𝐴�

    𝐼𝐼1𝐼𝐼2𝐼𝐼3𝑥𝑥

    =�

    1000𝑏𝑏

    Solution:

    Solving Linear Equations

  • Solving Linear Equations Step 3: Solve 𝐴𝐴𝑥𝑥 = 𝑏𝑏 using backslash “\” command (x=A\b) or

    linsolve(A,b)

    x1 =

    1.04940.49380.4321

    Answer:

    x2 =

    1.04940.49380.4321

    x3 =

    1.04940.49380.4321

  • On your Own: Linear Equations

    Solve for 𝐼𝐼1 and 𝐼𝐼2 in the schematic below.

    The two equations to solve are:

    Answer:

    7𝐼𝐼1 − 7𝐼𝐼2 = 10−7𝐼𝐼1 + 10𝐼𝐼2 = 0

    𝐼𝐼1 = 4.7619, 𝐼𝐼2 = 3.3333

  • Linear Equations:Overdetermined System

    Solve the following system of linear equations2𝑥𝑥1 + 1𝑥𝑥2 = −1

    −3𝑥𝑥1 + 𝑥𝑥2 = −2

    −𝑥𝑥1 + 𝑥𝑥2 = 1

    The system can be solved using linear squares; ie, by min𝑥𝑥

    𝐴𝐴𝑥𝑥 − 𝑏𝑏 or 𝑥𝑥 =𝐴𝐴𝑇𝑇𝐴𝐴 −1𝐴𝐴𝑇𝑇𝑏𝑏. This can be very slow in MATLAB. Instead, try using

    mldivide() or \ command, as shown:

    Answer:

    𝑥𝑥1 = 0.1316 , 𝑥𝑥2 = −0.5789

  • Solving Non-linear EquationsExample: use the fsolve() command to solve the following non-linear system of equations.

    0 = 𝑥𝑥2 + 𝑥𝑥 − 𝑦𝑦2 − 1

    0 = 𝑦𝑦 − sin 𝑥𝑥2

    Solution: Step 1: Create a new function with an input x that computes the left-hand

    side of these equations. Make sure to save as ’myfun.m’ to your folder.

  • Solving Non-linear Equations Step 2: Write a new code (either as a script or in the Command Window)

    to solve the nonlinear system: Call your function using fun=@myfun; Make an initial guess for the starting point (example [0,0]) Use the command fsolve to solve the problem

    𝑥𝑥1 = 0.7260 𝑥𝑥2 = 0.5029

    Answer:

  • On Your Own: Non-linear EquationsSolve the following system of equations

    𝑥𝑥2 + 𝑥𝑥𝑦𝑦3 = 9

    3𝑥𝑥2𝑦𝑦 − 𝑦𝑦3 = 4

    Start with initial guesses: 𝑥𝑥0,𝑦𝑦0 = (1.25,2.5), (-2,2.5),(-1.2,-2.5),(3,0).

    𝒙𝒙𝟎𝟎,𝒚𝒚𝟎𝟎 Solutions(1.25,2.5) (1.3364,1.7542)(-2,2.5) (-3.0016,0.1481)

    (-1.2,-2.5) (-0.9013,-2.0866)(3,0) (2.9984,0.1481)

    Answer:

  • Solving Differential EquationsMATLAB functions can be used to solve differential equations

    (ode45, ode23, ode113, ode15s, ode15i,…) Type “help ode45” in the command window to read function

    description. An example is a differential equation describing the velocity 𝑣𝑣

    of a falling parachutist as function of time 𝑡𝑡 using Newton’s law

    𝑑𝑑𝑣𝑣𝑑𝑑𝑡𝑡

    = 𝑔𝑔 −𝑐𝑐𝑚𝑚𝑣𝑣

  • Solving Differential EquationsExample: use the ode45 command to solve the following differential equation. (use 𝑔𝑔 = 9.81 𝑚𝑚/𝑠𝑠2, c = 12.5 N.s/m, 𝑚𝑚 = 68.1 kg)

    𝑑𝑑𝑣𝑣𝑑𝑑𝑡𝑡

    = 𝑔𝑔 −𝑐𝑐𝑚𝑚𝑣𝑣

    Solution: Step 1 : Create a new function with inputs t and v that computes the

    right-hand side of the differential equation. Save the function as ‘diffeq.m’ in your MATLAB folder.

  • Solving Non-linear Equations Step 2: Write a new code to solve the differential equation and plot the

    solution: Use the command ode45 to solve the problem and specify the time span

    and initial condition. Type help ode45 to see the required syntax.

    Answer:

  • Solving Differential EquationsExample: Use the ode45 command to solve the following differential equation. (use 𝑔𝑔 = 9.81 𝑚𝑚/𝑠𝑠2, 𝑏𝑏 = 𝑐𝑐/𝑚𝑚𝑙𝑙2 = 1.5, 𝑙𝑙 = 1 m)

    𝑑𝑑2𝜃𝜃𝑑𝑑𝑡𝑡2

    +𝑐𝑐𝑚𝑚𝑙𝑙2

    𝑑𝑑𝜃𝜃𝑑𝑑𝑡𝑡

    +𝑔𝑔𝑙𝑙

    sin𝜃𝜃 = 0

  • Solving Differential EquationsExample: Use the ode45 command to solve the following differential equation. (use 𝑔𝑔 = 9.81 𝑚𝑚/𝑠𝑠2, 𝑏𝑏 = 𝑐𝑐/𝑚𝑚𝑙𝑙2 = 1.5, 𝑙𝑙 = 1 m)

    𝑑𝑑2𝜃𝜃𝑑𝑑𝑡𝑡2

    + 𝑏𝑏𝑑𝑑𝜃𝜃𝑑𝑑𝑡𝑡

    +𝑔𝑔𝑙𝑙

    sin𝜃𝜃 = 0

    Solution: Step 1: Change the second order differential equation to a system of first

    order differential equations.

    𝑠𝑠𝑎𝑎𝑦𝑦 𝑦𝑦1 = 𝜃𝜃, 𝑎𝑎𝑎𝑎𝑑𝑑 𝑦𝑦2 =𝑑𝑑𝜃𝜃𝑑𝑑𝑡𝑡 , 𝑡𝑡𝑡𝑡𝑡𝑎𝑎

    𝑑𝑑𝑦𝑦1𝑑𝑑𝑡𝑡 =

    𝑑𝑑𝜃𝜃𝑑𝑑𝑡𝑡 = 𝑦𝑦2

    𝑑𝑑𝑦𝑦2𝑑𝑑𝑡𝑡 =

    𝑑𝑑2𝜃𝜃𝑑𝑑𝑡𝑡2 = −𝑏𝑏𝑦𝑦2 −

    𝑔𝑔𝑙𝑙 sin𝑦𝑦1

    Our two resulting differential equations to solve for are:𝑑𝑑𝑦𝑦1𝑑𝑑𝑡𝑡 = 𝑦𝑦2

    𝑑𝑑𝑦𝑦2𝑑𝑑𝑡𝑡 = −𝑏𝑏𝑦𝑦2 −

    𝑔𝑔𝑙𝑙 sin 𝑦𝑦1

  • Solving Differential EquationsSolution:

    𝑑𝑑2𝜃𝜃𝑑𝑑𝑡𝑡2

    +𝑐𝑐𝑚𝑚𝑙𝑙2

    𝑑𝑑𝜃𝜃𝑑𝑑𝑡𝑡

    +𝑔𝑔𝑙𝑙

    sin𝜃𝜃 = 0

    𝑑𝑑𝑦𝑦1𝑑𝑑𝑡𝑡

    = 𝑦𝑦2𝑑𝑑𝑦𝑦2𝑑𝑑𝑡𝑡

    = −𝑏𝑏𝑦𝑦2 −𝑔𝑔𝑙𝑙

    sin𝑦𝑦1

    Step 2: Create a new function with inputs t and y that computes the right-hand side of the differential equation. Save the function.

  • Solving Non-linear Equations Step 3: Write a new code to solve the differential equation:

    Use the command ode45 to solve the problem and specify the time span and initial condition.

    Plot the angle vs. time, as shown below.

    Plot 1:

  • Solving Non-linear Equations Step 3: Plot the angular velocity vs. time, as shown.

    Plot 1: Plot 2:

  • On your Own: Solving Differential Equations

    Solve the following differential equation using ode45:

    𝑡𝑡2𝑦𝑦′ = 𝑦𝑦 + 3𝑡𝑡

    when y(1) = -2 and over the interval 1 ≤ 𝑡𝑡 ≤ 4

    Answer:

  • Solving Differential Equations: Remarks

  • Eigenvalues and Eigenvectors An eigenvector of a square matrix is a vector that does not change its

    direction under the associated linear transformation.

    𝐴𝐴𝑣𝑣 = 𝜆𝜆𝑣𝑣 ⇒ 𝐴𝐴 − 𝜆𝜆𝐼𝐼 𝑣𝑣 = 0

    𝐴𝐴 is square matrix, 𝑣𝑣 is eigenvector, and 𝜆𝜆′s are the eigenvalues.

    MATLAB function: [V,D] = eig(A)

    V is a matrix of eigenvectors.

    D is a matrix of eigenvalues on the diagonal.

  • Eigenvalues and EigenvectorsExample: Find the eigenvalues, 𝜆𝜆, and eigenvectors, 𝑣𝑣, for the following matrix

    𝐴𝐴 =1 7 32 9 125 22 7

    Solution: Step 1: Create a 3-by-3 matrix and use “eig” command.

    Answer:

  • Polynomial Curve Fittingp = polyfit(x,y,n) is a MATLAB function that returns the coefficients for a polynomial p(x) of degree n that is a best fit for a given a set of data (x,y). The input arguments are: x = independent data y = dependent data n = order of polynomial

    Example: Let’s fit a polynomial to a cosine function

    Step 1: Store 100 equally spaced points between 0 and 3 * pi to the variable x, and a cosine function evaluated at x.

  • Polynomial Curve FittingExample: Let’s fit a polynomial to a cosine function

    Step 2: Use the polyfit(x, y, n) command to get the coefficients for a 7th degree polynomial fit and save the coefficients to the variable p, as shown:

  • Polynomial Curve FittingExample: Let’s fit a polynomial to a cosine function

    Step 3: Create 𝑦𝑦1 that will be equal to the polynomial fit. Plot the two curves.

    𝑝𝑝 𝑥𝑥 = −0.0001𝑥𝑥7 + 0.0033𝑥𝑥6−0.0392𝑥𝑥5 + 0.1994𝑥𝑥4 − 0.3301𝑥𝑥3− 0.1662𝑥𝑥2 − 0.1224𝑥𝑥 + 1.0007

  • Given the data in polyfit.xlsx for the thermal conductivity of silicon near absolute zero (T = 0K = −273.15℃), graph the points and fit a polynomial to the data.

    On Your Own: Poly Curve Fitting

    T [K] k [W/m-K]5 542

    10 222815 399220 515725 538630 508835 428440 358845 324150 273955 251860 236665 202470 195875 162280 154985 138790 111095 1106

    100 1161105 854110 904115 814120 762125 754

    0 20 40 60 80 100 120 140500

    1000

    1500

    2000

    2500

    3000

    3500

    4000

    4500

    5000

    5500

    The data can either be entered in manually in MATLAB or read in through the file on the page:matlabtraining.engr.wisc.edu And saving the code as “polyfit” in your MATLAB folder and entering the following code:

    http://matlabtraining.engr.wisc.edu/

  • On Your Own: Poly Curve Fitting

    0 20 40 60 80 100 120 1400

    1000

    2000

    3000

    4000

    5000

    6000

    data

    n = 7

    n = 9

  • Graphing Using Plots TabCreate graphs using the Plots tab: scatter(x,y)area(A)bar(A)histogram(H)pie(P)

  • MATLAB Toolboxes

    MATLAB offers the ability to install add-ons for increased functionality. This can be done through toolboxes.

    EXAMPLES: ● Signal Processing Toolbox● Wavelet Toolbox

    http://www.mathworks.com/help/

    http://www.mathworks.com/help/

  • Additional Resources

    • Visit mathworks.com/help/matlab/ for documentation from the MATLAB support site.

    • Search Google (possibly the most convenient resource).• Come to drop-in tutoring. Visit the ULC’s drop-in page:

    https://www.engr.wisc.edu/academics/student-services/ulc/tutoring/ for info on tutor schedules and other classes that tutors are available for.

    • Please consider taking a survey describing your experience today:matlabtraining.engr.wisc.edu

    Thank you!

    https://www.engr.wisc.edu/academics/student-services/ulc/tutoring/http://matlabtraining.engr.wisc.edu/

    MATLAB: Advanced ApplicationsSlide Number 2Today’s TopicsSolving Linear EquationsSolving Linear EquationsSolving Linear EquationsOn your Own: Linear EquationsLinear Equations:�Overdetermined SystemSolving Non-linear EquationsSolving Non-linear EquationsOn Your Own: Non-linear EquationsSolving Differential EquationsSolving Differential EquationsSolving Non-linear EquationsSolving Differential EquationsSolving Differential EquationsSolving Differential EquationsSolving Non-linear EquationsSolving Non-linear EquationsOn your Own: Solving Differential EquationsSolving Differential Equations: RemarksEigenvalues and EigenvectorsEigenvalues and EigenvectorsPolynomial Curve FittingPolynomial Curve FittingPolynomial Curve FittingOn Your Own: Poly Curve FittingOn Your Own: Poly Curve FittingGraphing Using Plots TabMATLAB ToolboxesAdditional Resources