Anda di halaman 1dari 33

MEGC Technical

Workshop

October 16th, 2018


Workshop Outline

Solving Differential Equations - ODEs


Fourier Transform – FFT Algorithm
Optimization Toolbox
What you should expect to get
out of this workshop?
ODE Solvers
Solving ODEs
• MATLAB has several ODE solvers - see
http://www.mathworks.com/help/matlab/math/or
dinary-differential-equations.html

• Common use - Runge-Kutta method solvers (ode23


and ode45)

• IMPORTANT: First step is to put your ODE into a set


of first-order equations (i.e. state-space form)
Solving ODEs – Format
Most basic format for calling an ODE solver

[time,q] = solver(@ODEFunction,timespan,q0)

• When inputting the ODEFunction you must always use an @ sign


in front of the function name
• ‘timespan’ can either be a start and end time or a vector of
increasing times to calculate over. If just a start and end time,
the solver will determine sample spacing
• ‘q0’ are the initial conditions of the solution
• ‘solver’ is the actual name of the ODE solver (e.g. ode45)
• The outputs are the time samplings that the integration was
calculated for and q is the output states at those samplings
Solving ODEs – Example
Solving ODEs – Example
Solving ODEs – Example
When implementing the ODE function to solve, it will
always be of the form:

For this example, it looks like:

Code: BallBounce1D_SingleBounce.m
Solving ODEs – Example
No definition of what the ground is so the ball flies
right through it
1D Ball Bounce with no Termination Callout
200

100

0
Y-Position of Ball

-100

-200

-300

-400
0 5 10 15
Time (sec)
Solving ODEs – Events
With ODE’s you can add events such that the
integration will stop if either the event is reached OR
the timespan reaches the last time entry

Use the same ball example but now we want it to


bounce when hits the ground

We will attenuate the ball velocity by 10% at every


bounce, using a 0.9 coefficient of restitution.
Solving ODEs - Events
• Stop integration using an event function
Solving ODEs - Events
We must engage the event function using odeset

Now the integrator will stop every time the event is reached
and we can define a new q0 for the next bounce.
Solving ODEs – Events Output
Code: BallBounce1D_10Bounces.m
1D Ball Bounce with Termination Callout
140
Integration Points
120 Ball Bounce Points

100
Y-Position of Ball

80

60

40

20

-20
0 10 20 30 40 50 60 70
Time
Solving ODEs – Events Output
Code: BallBounce1D_10Bounces.m
1D Ball Bounce with Termination Callout
50
Integration Points
40 Ball Bounce Points

30

20
Y-Velocity of Ball

10

-10

-20

-30

-40

-50
0 10 20 30 40 50 60 70
Time
PDE Solvers
Solving PDEs
Even in MATLAB PDEs can be challenging

The PDE toolbox expands capability, but there are


still challenges (meshing, convergence, etc)

A helpful resource for PDEs in MATLAB:


www.mathworks.com/help/pde/ug/equations-you-
can-solve.html
Parabolic PDEs
𝜕𝑢 𝜕𝑢 −𝒎
𝜕 𝒎
𝜕𝑢 𝜕𝑢
𝑐 𝑥, 𝑡, 𝑢, =𝑥 𝑥 𝑓 𝑥, 𝑡, 𝑢, + 𝑠 𝑥, 𝑡, 𝑢,
𝜕𝑥 𝜕𝑡 𝜕𝑥 𝜕𝑥 𝜕𝑥

Initial Condition: 𝑢 𝑥, 𝑡0 = 𝒖𝟎 (𝒙)

𝜕𝑢
Boundary Conditions: 𝒑 𝒙, 𝒕, 𝒖 + 𝒒 𝒙, 𝒕 𝑓 𝑥, 𝑡, 𝑢, =0
𝜕𝑥

Must define 2 sets of p & q


(one for each boundary)!
Solving a PE in MATLAB
sol = pdepe(m, PDE, IC, BC, x, t)

m – scalar auxiliary exponent


PDE – function defining the PDE
IC - function defining the initial condition
BC - function defining the boundary conditions
x - vector defining space domain to solve over
t - vector defining time domain to solve over

sol – your solution!


Solving a PE in MATLAB
𝝏𝒖 𝝏 𝝏𝒖
𝝅𝟐 =
𝝏𝒕 𝝏𝒙 𝝏𝒙

IC: 𝑢 𝑥, 0 = sin(3 𝜋𝑥)

𝑢 0, 𝑡 = 0
BC: 𝜕𝑢
−𝑡
𝜋𝑒 + (1, 𝑡)
𝜕𝑥
Fourier Transforms
Fourier Transform
MATLAB has a Fourier Transform tool to go between
time and frequency domain
http://www.mathworks.com/help/matlab/math/four
ier-transforms.html

Why Frequency Domain Analysis?

IMPORTANT: The fft function uses a Fast Fourier


Transform Algorithm
Fast Fourier– Format
Most basic format for calling an fft function

Y = fft(Input array,n,dim)

Input array : a vector, matrix or multidimensional array


n : Transform Length specified as [], or nonnegative integer scalar
dim : Dimension to operate along, specified as a positive integer
fft : the function being called
Y : Fourier transform of the input array
Fast Fourier - Example
Analyze variations in data such as event in nature over
a period of time
• For 300 years astronomers have tabulated the
number and size of sunspots

Code: sunspot.m
Fast Fourier - Exercise
In scientific applications, signals are often corrupted
with random noise disguising their frequency
Fast Fourier - Exercise
In scientific applications, signals are often corrupted
with random noise disguising their frequency

Use Fourier Transform to reveal the frequencies


Consider a sinusoidal signal x as a function of time t
with frequencies 15Hz and 20Hz
Inject Gaussian noise into the original signal x

Code: noisySignal.m
Optimization Toolbox
Optimization Toolbox
Provides functions for finding parameters that
minimize or maximize objectives while satisfying
constraints

Includes solvers for Linear Programming, Mixed-


Integer Linear Programming, Quadratic
Programming, Nonlinear Programming, etc.
https://www.mathworks.com/help/optim/
index.html
Optimization Toolbox - Example
Minimization of Rosenbrock’s function over a unit disk
f(x) = 100(x2–x12)2 + (1 – x1)2
In other words, minimize function f(x) over the set
x12 + x22<=1 i.e. minimization of a nonlinear function
with a nonlinear constraint.

NOTE Rosenbrock's function is a standard


test function in optimization. It has a unique
minimum value of 0 attained at the point
(1,1). Finding the minimum is a challenge
for some algorithms since it has a shallow
minimum inside a deeply curved valley.
Optimization Toolbox - Example
f(x) is the objective function – create a function
function f = rosenbrock(x)
f = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;

x12 + x22<=1 is called a constraint. Can have any


number of constraints – inequalities or equations

Constraints function must be formulated as c(x)<=0 or


ceq(x) = 0
function [c,ceq] = unitdisk(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [];
Optimization Toolbox - Example
• Running the optimization
• Running from within the command line

options = optimoptions(@fmincon,...
'Display','iter','Algorithm','interior-point');
[x,fval] = fmincon(@rosenbrock,[0 0],...
[],[],[],[],[],[],@unitdisk,options)

• Can also use the optimization app by typing


optimtool in command line
Takeaway Ideas
MATLAB provides tools for more advanced
mathematics & computing
• ODE solvers to solve system of first-order differential
equations – most common are ode45 and ode23

• PDE solvers – you must know what you’re putting in!


• Fourier Transform using fft function
• Optimization toolbox to find minimum or maximum of
an objective subjected to constraints
For questions or comments, contact:
megc.workshops@umich.edu

Anda mungkin juga menyukai