UcenlyOpen in the app

Mathematical Applications · BE5B15MAA · Topic 5 of 13

Functions in Matlab 2: advanced functions, graphical user interfaces

Week 5. Functions can take other functions as arguments, which is how Matlab finds roots, minima and ODE solutions. The GUI part of the week is still materials pending.

Open the interactive lesson

Key ideas

Functions of functions

fzero finds a zero near a starting value, fminsearch finds a minimum near x0, quad computes a definite integral (current releases recommend integral) and [t,y]=ode45(fun,tspan,y0) integrates a system of ODEs. The slides pass the function by its name; current releases accept a handle such as @f or an anonymous function.

Writing the right-hand side for ode45

ode45 wants a function that returns the derivatives of a state vector. For the series RLC circuit the state variables are i=y1 and uc=y2. From u=R i+L di/dt+uc and i=C duc/dt the function returns [(u−R y1−y2)/L; y1/C].

Random numbers and a checking function

randn(m,n) returns an m×n matrix of normally distributed numbers with mean 0 and standard deviation 1. The slides check the mean and the deviation of randn(1,1000) with a function of their own, count how many values fall into intervals of length 0.2 between −4 and 4, and draw the counts with plot and subplot.

In one line: Hand ode45 a function that maps the state to its derivative; everything else is the solver’s job.

Formulas

Sources

Written by Ucenly. Based on the BE5B15MAA course materials: