Péter Nagy, Csaba Hős 2015. H-1111, Budapest, Műegyetem rkp. 3. D building. 3 rd floor Tel: 00 36 1 463 16 80 Fax: 00 36 1 463 30 91 www.hds.bme.hu
Table of contents Homework Introduction to Matlab programming Solution of an initial value problem (IVP) Solution of a boundary value problem (BVP)
Homework Homework: The homework and these slides will be available on: http://www.hds.bme.hu/mota/eng/denum/index.html Submission: Only the source code and the plots have to be archived and submitted to pnagy@hds.bme.hu as NC_Name.zip. The subject has to be DENUM NC Name. The deadline is the end of 12 th week. Programming language: Matlab preferred, otherwise personal submission with own laptop is necessary Free alternatives: Octave, Freemat, Scilab
Introduction to Matlab programming MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment Current folder! Menu Command window Workspace Text editor Errors, warnings File explorer
Useful commands 1. Type stg. + Tab key -> autocomplete F1 key over a function -> Help+description+syntax ; at the end of a command -> the output is not written to the command window clc -> Clear command window clear all -> Delete variables close all -> close figures Avoid overwriting predefined functions and variables (F1 or Tab)!!! Predefined variables: pi, i-imaginary unit, exp(1) Euler number function output = name(var1, var2) global var1 var2 -> define global variables, subfunctions can read them [x1, x2, x3] -> generates a row vector [x1; x2; x3] -> generates a column vector A -> the transpose of A (A can be a matrix or a vector)
Useful commands 2 {.+.-.*./} -> calculate {+ - * /} element by element Defining vectors linspace(a, b, n) -> generates linearly spaced vectors between a and b, n is the number of elements a:b generates linearly spaced vectors between a and b, increment is 1 a:deltax:b generates linearly spaced vectors between a and b, increment is deltax v(i) -> the ith element of the vector A(i,j) -> the element of a matrix in the row i and column j A(i,:) -> the i th row of a matrix ; A(:,j) -> the j th column of a matrix Loops for i=1:num_steps end %code %comment while (statement) end %code %comment
Useful commands 3 Define functions f(x,y) = Expression can be defined in Matlab f=@(x,y) Expression; -> for simple expression (eg. ODES) or function output = f (x,y) output=expression; -> for longer, more complex calculation (eg. solver) end Important functions [t, y]=ode45(@(t,y) f(y,t), t 0, x 0 ) -> solve ODE (f(y,t)); initial time: t 0 ;initial condition x 0 fminsearch=(@(y) f(y, var1, var2), y 0 ) -> minimize f(y), the initial guess is y 0 Measure computation time (t) tic %code toc (or t=toc)
Useful commands 4 Visualization figure(i) -> generates an empty figure, (i is an integer) subplot(n, m, i) -> generates sub figures, n: number of rows, m: num. of columns, i: is the active subplot plot(x,y) -> plot points; x and y are vectors, further possibilities-> F1 in the case of multiple plots plot(x 1,y 1, x 2,y 2, x 3,y 3 ), where x i,y i are vectors title('title') -> generates title legend('title') -> generates title xlabel('label of x axis'), ylabel('label of y axis'), xlim([xmin xmax]), ylim([ymin ymax]) saveas(figure(i), 'name.png') -> save figure(i) as name.png
Solution of an IVP
Transcript the differential equation The ODE is given 1. If the ODE is higher order transform to first order ODE System Example: t (0) 0 y= f y, ; y y x t c x t s x t g x, t ; x 0 =x ; x 0 =x 0 0 In matrix form: x : y ; x : y x : y ; 1 2 2,, y t c y t sy t g x t 2 2 1 y t cy t sy t g x t y= y ; 2 2 1 T 1 y2 y1= x y2 0 1 0 x0 y= t (0) 0 s c y +, ; g y2, t f y y y x 0
Example In the case of Van der Pool oscillator 2 2 2 x 1 x x x 0; x 0 =x ; x 0 =x y 1 y y y 0 2 1 2 1 y 1 y y y y 2 1 2 1 y 1 2 0 0 Implemented as VdP_rhs(t,y) in ExpImplEu_VanDerPol_new.m
Explicit Euler method 2. Select a solver Explicit Euler method Implemented as y y dt f y n 1 n n n, t
Implicit Euler method Implicit Euler method y y dt f y Implemented as n1 n n1 n1 Try to minimize the expression n n1 n1 n1, t y dt f y, t y 0 r y = y dt f y, t y 0 n1 n n1 n1 n1
Break
Adaptive timestep Problem: Large timestep -> accuracy, stability problems Small timestep -> computationally expensive (=slow) Idea: 1. Predict the local error -> 2. hold between given limits 1. Prediction of the local error Calculate one step in two different ways with different accuracy (but with the same scheme!) The accuracy can be changed: the order of the scheme is changed (ode45) Solve the problem with different stepsize. First, in one step with dt, then in two steps with dt/2
Adaptive timestep The error: 1 n1 2 n1 Hold the error between limits y y the less accurate result the more accurate result 2 1 e = y n1 y n1 min, max If the estimated error is too large Else max max do not store the results decrease the timestep dt r dt r<1 new old store the results If the est. error is too small min increase the timestep
Adaptive timestep Implemented as:
Accuracy of solvers The problem: x t x t 3sin 2t ; x 0 = 2; x 0 = 1 sin y t y t 3 2t 2 1 y1= x y2 y 0 2 1 Analytical solution: sin cos sin x t t 2 t 2t
Accuracy of solvers
Accuracy of solvers
Boundary value problem x t y t v g x t 0 0 y t 0 0 y x 1 0 x t vt 0 y t x / v 0 y t 0 1?
BVP-Shooting method y t g y t 0 0 y t 0 Solve Implemented as: / D : y t x v D 0 1
BVP-Finite differences y t g y t 0 0 y t 1 / v 0 y 2y y y t 2 t yi 1 2yi yi 1 g 2 t i1 i i1 2 yi 1 2yi yi 1 t g 2 1 y2 1 1 2 y 3 1 2 tg y 1 2 1 n2 1 1 2 y n1 1
BVP-Finite differences
Thank you for your attention!