You are on page 1of 24

# NUMERICAL ANALYSIS IN MATLAB

BASIC COMMANDS AND FUNCTIONS OF THE VIZUALIZATION AND PROGRAMMING ENVIRONMENT

Prof. Aleˇ Proch´zka s a
Institute of Chemical Technology, Prague Department of Computing and Control Engineering Technical Street 1905, 166 28 Prague 6

CONTENTS
1. MATLAB Environment 2. Basic Operations 3. Control Commands 4. Subroutines 5. Objects and 2D Graphics 6. 3D Graphics 7. Data Files 8. MATLAB Graphical User Interface 9. Multidimensional Arrays 10. Symbolic Mathematics 11. SIMULINK Environment 12. Symbolic and Numeric Solution of Systems of Linear Equations 13. Approximation of Functions 14. Linear and Nonlinear Approximation 15. Statistical Data Analysis 16. Symbolic and Numeric Solution of Nonlinear Equations 17. Systems of Nonlinear Equations 18. Symbolic and Numeric Diﬀerentiation and Integration 19. Symbolic and Numeric Solution of Diﬀerential Equations 20. Boundary Problem of the Solution of Diﬀerential Equations 21. Boundary Problem Simulation 22. Applications

Textbook of Mathematical Methods in Engineering Akademic Year 2005/2006 Prof. Ing. Aleˇ Proch´zka, CSc s a

x=inv(A)*b Solution of Examples SIN(X) 1 0.. b=20. EDIT window Conversational and programming style. 7]. x=a:h:b. delete of variables (clear list ) 5.b a=0.2: Solution of the set of linear equations A x = b A=[1 2 3. Visualization of variables (whos).4 1. Command ended by a semicolon =⇒ store of a variable without its visualization 2..2) 1.1. b=5.3 0. ylabel(’y’). plot(x. Help (help.7 0. MATLAB ENVIRONMENT Characteristics • • • • Matrix oriented computing and graphical environment COMMAND window. b=2.) 4. xlabel(’x’). plot(x. Selected operations (transposition.2 0.5 y 0.8 −1 0 2 4 6 8 10 x 12 14 16 18 20 ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ 1 2 3 6 1 A = ⎝4 5 6⎠ b = ⎝15⎠ x = ⎝1⎠ 7 8 1 16 1 ABS(COS(X)) 1 0.8 0. j %%% >> >> >> >> Example 1.3 Programming Computational Mode The style of work in EDIT and COMMAND window: 1. The necessity of the use of correct dimensions of matrices and vectors 2. . Opening of the EDIT window 2.6 −0.. x=a:h:b. h=0.9 0. h=0.8 0. h=0.f1).1) 1. Return to previously written commands =⇒ up-arrow key 3. b and step h (a=0. . Save of the ﬁnal programme under a selected name 4.4 0.1.a+h..2 −0. b and step h (a=0. multiplication. environment initialization Possibilities of data export and import. b=20.1. f1=sin(x)..1. i. inversion) %%% >> >> >> Example 1.3: Plot of a function f(x) for x=a.programming 3.b >> sem1prog % Programme call %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% SCRIPT SEM1PROG.2 Matrix and Vector Operations Basics of the work with matrices: 1.1: Plot of a function f(x) for x=a.. Selection of commands .. f2=abs(cos(x)). help command .2 0 y −0. h=0.. title(’ABS(COS(X))’) −0. b=[5.6 0. Start of the programme from the COMMAND window %%% Example 1. FIGURE window.1 0 0 2 4 6 8 10 x 12 14 16 18 20 EXAMPLES 1 1. 3 3]. 4 5 6.6 0. ylabel(’y’). eps. The use of standard variables: pi.1 Evaluate and plot function f(x)=exp(x) for x∈ a.3 Solve the system of equations A x = b pro A=[1 2. 7 8 1].. the use of toolboxes. lookfor key ..) and DOS commands (dir. xlabel(’x’).4 0.a+h.M %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% a=0.1 Conversational Computational Mode Notes to basic commands: 1. simulation COMMANDS PLOT XLABEL YLABEL TITLE SIN COS ABS INV 1. b=[6 15 16]’.2 Evaluate and plot function f(x)=log(x) for x∈ a. title(’SIN(X)’).f2).. 1.

2 0.5 3 3.<= • logical: &. 1 3 ⎛ 1 ⎝3 1 4 9 16 ⎞ 1 1 1 2 2 2⎟ ⎟ 3 3 3⎠ 4 4 4 s2 = 1 2 .7568 −0. r1=v*v’ r2=v..1 0 1 1.8 0.3 0.2 Functions Function categories: 1..4 5].’ vector: .~ COMMANDS EXP SUM MIN MAX DET ONES ZEROS RAND Solution 2.7 0. MIN. EXP. LOG.3) plot(z3).N) assuming their number N=100.2.<.5 Example 2.5: Special functions z1=zeros(2. DET =⇒ function is applied for the whole matrix %%% >> >> >> >> %%% %%% >> >> >> >> Example 2.3) scalar function vector function matrix function 0.*v r3=v’*ones(1. BASIC OPERATIONS Operators • arithmetic: matrix: +.1 Deﬁne matrix A=[1 1. b= 1 2 4 ⎞ 2 1 4 2⎠ 1 1 r1 = 30 r3 = s1 = A = C = 2. 500. .5 2 2.1 1. STD.3) z2=ones(1..’ (element-by-element operations) • relational: >. MEAN.9589 5 7 f 3 = −3 GRAF MATICE RAND(4.*./.^. Scalar: SIN.3: Matrix operations A=[1 2.minimum and maximum values of the whole matrix . MAX.9093 −0./. f1=sin(A) % f2=sum(A) % f3=det(A) % Functions 1 0.2.1 2. COS. =⇒ function is applied to each matrix element 2. Matrix: INV.5 4 EXAMPLES 2 2.2 Apply function HIST for the study of distribution of values v=RAND(1.. . 2. Vector: SUM. =⇒ function is applied for each matrix column 3.4) Example 2.>=.|. b’.7] and evaluate .9.^.3)’) 0.determinant of the given matrix 2..3) z3=rand(4.5 1. 3 4] b=[1 2] C=[A.4 0..4 2.8415 0.9 Solution A = f1 = f2 = 1 2 4 5 0.1: Operations with vectors v=[1 2 3 4].4: A=[1 2.1.[1 1 1]] v = r2 = 1 2 3 4 1 ⎛ 1 ⎜2 ⎜ ⎝3 4 0.==. title(’PLOT OF MATRIX R=RAND(4.*..2: Relational expressions s1=3>5 s2=3<5 Example 2.6 0.1 Assignment Commands variable = expression %%% >> >> >> >> %%% %%% >> >> %%% >> >> >> Example 2. 1000.-.mean values of its rows and columns . Plot resulting random values by function PLOT (apply key ZOOM IN as well) .=~..7 1.

2 Enlarge the algorithm resulting from the previous example by the MENU command for selection of variable N ..3.1 Loop Commands for variable = expression commands end %%% Example 3. axis([0 2*pi -5 5]). h=(b-a)/N. b=2*pi.1 Evaluate approximate value of the integral of function f (x)=sin(x) in limits a=0. % Evaluation of function values % Standard algorithm for the evaluation of the sum of given values S=0. Q1=S*h % Compressed algorithm for the evaluation of the sum of given values Q2=sum(f(1:N))*h COMMANDS FOR-END IF-ELSEIFELSE-IF MENU FIGURE AXIS GRID 3. b=π for N=20. b=20..’Cos(x)’. N=20. b=20 for N=20 its parts %%% using rectangular rule: %%% Q ~ h*sum(f(x(i)) pro x(i)=a+(i-1)*h...cos(x)) else plot(x. CONTROL COMMANDS 3. x=a:h:b. end. end grid on PRUBEH FUNKCE 5 4 3 2 1 0 −1 −2 −3 −4 −5 0 1 2 3 4 5 6 EXAMPLES 3 3.sin(x)) elseif k==2 plot(x.2: Plot of the given function in selected limits %%% a=0. h=0. S=S+f(i).tan(x)).’Sin(x)’. % Definition of given values x=a:h:b.2 Decision Structures if relational expression 1 commands 1 elseif relational expression 2 commands 2 else commands 3 end %%% Example 3. 40 and 60 its parts using the trapezoidal rule 3.N %%% a=0. for i=1:N. i=1.’Tan(x)’). f=sin(x). k=menu(’FUNCTION’.1.1: Evaluate the approximate value of integral of function %%% f(x)=sin(x) in limits a=0. if k==1 plot(x.2.

B=A(2.4 1.2: Logical matrix operations >> L=A(:. 2 4 6. SUBMATRICES 4.3 Function Subroutines function [ output parameters ]= name ( input parameters ) commands Notes: 1.:) C=A(:. 4 5 6.1 1.2 Logical Operations %%% Example 4. Q=sem4prog(a. A1=A(L.3: Evaluate the approximate value of integral %%% of function f(x)=sin(x) in limits a=0.1 Deﬁne function subroutine for evaluation of minimum and maximum value of the given matrix and apply it for matrix A = [1 3 5..2. vectors.:) >> v=A(:)’ 1 4 7 2 5 8 3 6 9 4.1: Basic matrix operations A=[1 2 3.7 1.9 2. 7 8 9].3 1.8 5 1.1 1..5] by elimination of values that diﬀer by more than the standard deviation from the mean value of the given sequence . b=20 for %%% N=20 its parts using rectangular rule: %%% Q ~ h*sum(f(x(i)) pro x(i)=a+(i-1)*h. b=20.1 Submatrices %%% >> >> >> >> Example 4.b. N=20.4.[3:-1:1]) Solution ⎛ 1 2 3 A = ⎝4 5 6⎠ 7 8 9 B = 4 5 6 ⎛ ⎞ 1 3 C = ⎝4 6⎠ 7 9 ⎛ ⎞ 0 L = ⎝0⎠ 1 v = ⎛ ⎞ 3 2 1 D = ⎝6 5 4⎠ 9 8 7 A1 = 7 8 9 ⎞ COMMANDS : CLC BREAK FUNCTION 4.3)>8.N) EXAMPLES 4 4. The number of real parameters can be smaller than that of formal parameters %%% Example 4. i=1. 1 1 1] 4.2 Modify the given vector x=[1.[1 3]) D=A(:. strings 2.N %%% a=0.. Formal parameters: matrices..

1: Definition of figure properties.’Units’. COMMANDS PLOT SUBPLOT XLABEL YLABEL TITLE TEXT AXIS. figure.1.8 0.0.’Units’.0.2 Plot f (x) = cos(x) and its derivative in one ﬁgure window axes using diﬀerent colors for each function 5.35]). ylabel(’y’).1. xlabel(’x’).’Normal’.3 Use GINPUT command for estimation of the smallest positive root of equation: sin(x) − 0.1 Object Properties name = command ( property name . b=20. ylabel(’y’).54.abs(cos(x))).1 in two separate windows %%% a=0.1 0. b = 20 in three separate ﬁgures 5. plot(x. EXAMPLES 5 5.2). GET DELETE HOLD ON HOLD OFF GINPUT 5.57.1:29)) get(h1) %%% %%% Definition of a property set(h1.35]). set(gcf.1.5 0.[0. plot(sin(0:0. subplot(2. OBJECTS AND 2D GRAPHICS 5.2 Two-dimensional Graphics %%% Example 5. title(’SIN(X)’). GRID SET. set(gcf. %%% b=20 and h=0.1 Plot function f (x) = sin(x)2 and its the ﬁrst and second derivative for x ∈ a. b for a = 0.’Position’. h1=axes(’Position’.53.2: Plot functions f1(x)=sin(x) and %%% f2(x)=abs(cos(x)) for x=a:h:b choosing a=0. x=a:h:b.’Units’.3]). property value ) %%% Example 5.’Position’. plot(x.3 0.3.1 0.[0.5.1x = 0 . h=0.sin(x)). axes properties.[0.0.[0.5 0.’Position’.3]).0.0.’Normal’.’Normal’.0.5. title(’ABS(COS(X))’). subplot(2. %%% and their modification %%% figure.1).3.

3 0.1 −0. meshc(X. 2 6. Palette of m colors is deﬁned by values of matrix Mm. y) = −x2 y 2 for x ∈ −2..2:2 %%% together with corresponding contour lines %%% [X.2: Store the present colormap into matrix M1.3 −0. Z=-X.2 0.4 −0.1 Plot function f (x. 0 1 0. 3. xlabel(’x’).*exp(-X.2 0.2 Colors Notes to the use of basic commands: 1. Selection of the viewpoint =⇒ function VIEW 4.5 1 0.5 1 0.Z). colormap([0 0 1]) COMMANDS MESHGRID MESH MESHC GRID ON GRID OFF LINE VIEW HOLD ON HOLD OFF CONTOUR COLORMAP COLORBAR 6. 2 and y ∈ −2.1 0 0] colormap(M2) colorbar 0. %%% define a new colormap M2 consisting of three basic colors %%% and apply it to the given 3D surface %%% M1=colormap M2=[0 0 1.4 0.y)=−x*exp(−x −y ) 2 2 f(x.4 0.3 −0.5 0 −0.^2-Y.5 6. ylabel(’y’).5 0.1 −0.1 0 0.^2).2 0.3 2.1 0 −0.5 −1 −1.1: Plot function %%% f(x.2 −0. y) = −x exp(−x2 − y 2 ) using four selected colors and use a separate window to plot its contour plot for x ∈ −2.5 2 1.5 y −2 −2 x −1 0 1 2 f(x.2:2). Selection of 3D plotting =⇒ function MESH. The choice of the number of contour lines and their description =⇒ funkce CONTOUR %%% Example 6. 2 with line description . Deﬁnition of x-axis and y-axis values are deﬁned by corresponding values of matrices X.5 y −2 −2 −1 x 0 −0. 3D GRAPHICS 6. 2 and y ∈ −2.2 −0. title(’f(x..3 Visualize contour plot of function f (x.Y]=meshgrid(-2:0. Y =⇒ function MESHGRID 2.2 Plot function f (x. 2.5.4 1 2 −0.2 −0. y) = −x2 y 2 for x ∈ −2.2:2 and y=-2:0.1 Fundamentas of 3D Graphics Notes to the use of basic commands: 1. 2 and y ∈ −2.5 0. Plot of the color map =⇒ function COLORBAR %%% Example 6.1 EXAMPLES 6 6.3 −0.4 0. Each of m colors is deﬁned by the combination of 3 basic colors (Red-Green-Blue) and their intensity in the range of 0.y)=-x*exp(-x^2-y^2)’). 1 3.1 0 −0.y)=-x exp(-x^2-y^2) for x=-2:0.y)=−x*exp(−x2−y2) 0.4 −0. MESHC. .5 −1 −1.6.5 2 1. Application of the color map =⇒ function COLORMAP 4.3 0.Y.5 0 −0.3 0.

MAT ﬁle and plot these variables in chosen limits .7.’r r1 c c1 :r r2 c c2 ’).xls’).2 Data Retrieving load f ile name Notes to the data retrieving: 1. In case that the list of variables is not deﬁned the whole workspace is saved 2.1: Saving of variables A=[1 2 3. COMMANDS SAVE LOAD WHOS DIR DDEINT DDEREQ 7.c1. plot(D1) EXAMPLES 7 7. DATA FILES MANIPULATION 7.3 Data Import from EXCEL Steps: 1.’r10c3:r1000c3’).XLS import into the MATLAB environment the matrix of measured values from the second up to the seventh column.XLS import into the MATLAB environment the power consumption (the 3rd column) and channel temperature in the glass furnace (the 6th column).’g2010. where r1.xls’). In case of the binary ﬁle (with MAT extension) all variables are retrieved together with their names 2.2 Using table G2010. Parameter −ascii allows data store as a plain text (without its name) %%% Example 7.r2. Opening of the MATLAB environment and EXCEL f ile neme .c2 stand for initial and ﬁnal number of rows and columns %%% Example 7.2: Data retrieving load dat1 7.xls 2. save dat1 A b.1 Data Saving save f ile name [ list of variables ] [-ascii] Notes to data saving: 1.1 Using table G2010. D1=ddereq(chan. plot these variables and evaluate their mean values 7. b=[6 15 16]’. 4 5 6. Data transport from EXCEL to MATLAB: chan=ddeinit(’excel’. In case of the plain text ﬁle all its values are retrieved under the ﬁle name %%% Example 7. save them into the *. 7 8 1].3: Reading of the EXCEL table chan=ddeinit(’excel’. D1=ddereq(chan.’ f ile name .

Either opening of a new environment or existing ﬁgure 3.3 Hidden Functions Use 1. Opening of the environment =⇒ Tools / MenuEditor 2.2 Modiﬁcation of plotted function . Modiﬁcation of callback functions for speciﬁc object in the EDITOR environment Note: Object handles are available for all functions EXAMPLES 8 8. Creation of the menu structure 3.1: Plot of a harmonic function with its frequency modified by a RADIO BUT slider 8.2 Menu Creation 1. GRAPHICAL USER INTERPHASE 8.1 Modiﬁcation of slider limit values 8. Modiﬁcation of related functions in the EDITOR environment CHECK BOX EDIT TEXT STATIC TEXT MENU AXES 8. Opening EDITOR and modiﬁcation of commands SLIDER %%% Example 8.1: Plot of a selected function %%% Example 8.8. Opening of the PROPERTY INSPECTOR for each object and property PUSH BUT 4.1 Steps of GUI Construction 1. Opening of the environment =⇒ File / New / GUI OBJECTS 2.

>> C{2. >> STATION(1).DATA SANTINKA [1 2 3] 10 11 12 13 1.1}=STATION.2 1 2 3 4 5 1. Data items can be of any kind %%% Example 9...2) .1) .2.POSITION STATION...DATA=[10 11. 4 5 6.1: Multidimensional array definition >> A(2.1) COMMANDS WHOS 1... .1 Using table G2010.N.1 Multidimensional Arrays a(1.2. 8 9 10].DATA % Data selection 9..2: Structured array definition >> STATION(1). Commands for multidimensional data processing are the same as that for matrices %%% Example 9.2}=’Jonathan Smith’.N. 5 and 6) and store them in the structured array .3 Cells CELL 1. a(1.K) a(1.2 ’ Jonathan Smith’ CELL 2.1}.2}=[1 2 3 4 5]..1.. MULTIDIMENSIONAL ARRAYS 9.2)=2. Data items can be of the diﬀerent kind %%% Example 9.1.DATA CELL 2. >> C{1.POSITION=[1 2 3].1 1 4 7 2 5 8 3 6 9 CELL 1.1) . >> A % Variable display 9..POSITION .1)=1.N.NAME=’SANTINKA’. a(M. a(M. a(1.2 Structured Arrays STATION . a(M.12 13]..1}=[1 2 3.1.NAME STATION. C % Variable display >> P=C{2. The system enables construction of simple databases 2. >> STATION % Variable display >> D=STATION(1).1 STATION STATION..2) a(1. >> A(2. a(1.XLS import into the MATLAB environment temperatures measured at diﬀerent parts of the glass furnace (in columns 4.N.N...K) .1.POSITION(2) % Data selection EXAMPLES 9 9. The number of levels is not limited 2.NAME .N. >> C{2..1).. >> STATION(1).. a(M..2) .9. .K) .3: The field of cells >> C{1.

SYMBOLIC MATHEMATICS 10.{1 4}) 10.b}.2 Symbolic Substitution %%% Example 10. intf2=int(f2) EXAMPLES 10 10. y2=5*x^2+6.1 Symbolic Manipulation %%% Example 9. y5=simplify(y4) >> ezplot(y3.. >> y1=3*x^2+5*x+3. >> s1=symsum(1/n^2. df=diff(f1) >> f2=1/x. y3=y1+y2. pretty(y3) >> y4=sin(x)^2+cos(x)^2..1: Definition and manipulation with symbolic variables >> syms a b c x n t p.3 Symbolic Summation %%% Example 10.10.[-2 1]) COMMANDS SYMS PRETTY SIMPLIFY SUBS SYMSUM DIFF EZPLOT 10..{a.2: Substitution and visualization >> d=(a+b)^(1/2). >> f1=subs(d.a.1 Plot selected symbolically deﬁned functions 10.0) >> f2=subs(d.4 Symbolic Diﬀerentiation and Integration %%% Example 10.2.1.2 Evaluate chosen sums of given sequences .3: Evaluation of sum(1/n^2) for n=1.4: Substitution and visualization >> f1=sin(a*x+b)^2.inf) 10.

y’(0)=1 %%% Solution: 1. Deﬁnition of block values 3. Opening of block library and model window =⇒ model composition 2. Solution of diﬀerential equations and continuous signal modelling 2. Explicit definition of the highest derivative: y’’=-y %%% 2. Time series and matrix manipulation resulting in discrete signals modelling 3. y(0)=0. Integration and corresponding block definition: y’=int(-y) dy %%% 3.1 Steps of Simulink Modelling 1.1: Visualization of a harmonic function BLOCKS SIGNAL GEN SINE CONSTANT GAIN SCOPE XY GRAPH DISPLAY FROM/TO WORKSPACE 11. Deﬁnition of modelling parameters and start of modelling %%% Example 11.2 Solution of given diﬀerential equation . SIMULINK ENVIRONMENT 11. Real data acquisition using applications libraries and submodels deﬁnition %%% Example 11. Description of another integration block: y=int(y’) dy EXAMPLES 11 11.11.2: Solution of differential equation: y’’+y=0.2 Applications 1.1 Visualization of selected functions 11.

2 · · · a1. [m. pretty(simple(x)) %%% Alternative >> EQ=A*[x1.2: Gauss-Jordan method A=[4 -1 1.i). x=A\b.1 · · · a1. break.4) 12.1 Finite Methods Mathematical background of ⎛ a1.1 aN. SYSTEMS OF LINEAR EQUATIONS Problem statement: Solution of system of equations ⎛ a1.x2.N ⎜ a2. if max(abs(Z(m.1 Evaluate symbolic and numeric solution of a selected system of linear equations 12.3: Iterative solution x=[0 0 0]’.:)=S(i.{a11 a12 a21 a22 b1 b2}. SOLUTION=solve(EQ(1).1 COMMANDS SYMS SOLVE SUBS SIMPLE PRETTY INV DIAG SPARSE FULL SPY GPLOT 12. b=[4 9 2]’.2 Numerical Solution 12.1 6 2. for i=1:m [Max.x’]. >> s1=SOLUTION. % Localization of the maximum element of i-th column S([j i].N xN./diag(A).2 Study methods of solution of sparse systems of linear equations . S=[A. end end.1: Symbolic solution of linear equations >> syms a11 a12 a21 a22 b1 b2 x1 x2 % Definition of symbolic variables >> A=[a11 a12.{1 1 2 1 3 4}) % Substitution 12.2 Iterative Methods Mathematical background: ⎛ (k) ⎞ ⎛⎛ ⎞ ⎛ x b1 0 a1.:)))<delta.2 · · · a2. Possibilities of simpliﬁcation of symbolic solution 2.2 ⎜ 1 ⎟ ⎜⎜ ⎟ ⎜ 0 ⎜x(k) ⎟ ⎜⎜ b2 ⎟ ⎜ a2. [m.1 a2.i)*S(i. x2]-b.N ⎜ ⎝ ··· aN.5 0 0 2 4 6 8 10 12 14 16 EXAMPLES 12 12.n]=size(S).2.i)))./ ⎝ · · · ⎠ ⇒ x = (b−(A−diag(diag(A)) x ··· ⎠⎠ (k−1) ··· 0 aN. plot(Z) 1 0.2 ⎜ a2.N ⎛ ⎞ ⎛ ⎞ x1 b1 ⎜ x2 ⎟ ⎜ b2 ⎟ ⎜ ⎟=⎜ ⎟ ⎝ · · · ⎠ ⎝ · · ·⎠ xN bN ⇔ AN. a21 a22].EQ(2)). MAX=50. % Row exchange S(i.N ⎟⎟ ⎜ ⎜ 1 · · · a2. % Initial estimate of the solution delta=0. pretty(simple(s1)) >> s2=SOLUTION.0001.2./diag(A) ⎠ ⎝ · · · ⎟⎟ .N ⎟ ⎟ ⎠ aN.2 x N ⎞ ⎞ ⎛ (k−1) ⎞⎞ ⎛ x a1.2 ⎜ ⎝ aN. iterations for i=1:MAX x=(b-(A-diag(diag(A)))*x).:)-Z(m-1. % Division of the i-th row by the diagonal element for ii=[1:i-1.:)/S(i.j]=max(abs(S(:.1 aN.x1. pretty(simple(s2)) >> s=subs(x.b].N x N 1.:)=S([i j]. % Precission. Z=x’.2 ⎟ (k) (k−1) ⎟ ⎟⎜ 2 ).1 aN.i+1:m] % Elimination S(ii.1 a1. x=S(:.1 ⎜ 2 ⎟ = ⎜⎝ ⎠ − ⎝ ⎝ ··· ⎠ ⎝ ··· (k) bN aN. x.:).N a2.1 a1.5 %%% Example 12.-1 -2 5].n]=size(Z). b=[b1.2 ··· ··· ··· ··· ⎞ a1. end end.:)-S(ii.N ⎟ ⎜x(k−1) ⎟⎟ ⎜ a2.N %%% >> >> >> >> >> >> >> >> >> Gauss-Jordan ⎞ b1 b2 ⎟ ⎟ ⇒ ⎠ bN method: ⎛ 1 0 ⎜0 1 ⎜ ⎝ 0 0 ··· ··· ··· ··· 0 0 1 ⎞ x1 x2 ⎟ ⎟ ⎠ xN System deﬁnition ⎞ ⎛ ⎞ ⎛ ⎞ 4 −1 1 4 x1 6 2⎠ b = ⎝9⎠ x = ⎝x2 ⎠ A=⎝ 1 x3 −1 −2 5 2 ⎛ Example 12.:)=S(ii.1 a2. Substitution allows conversion to numerical solution %%% Example 12.2 · · · aN. b2].1 = bN.1 Symbolic Solution Characteristics: 1. Z=[Z.12.:).

.6 1.2 8.5 15]’. y=[5.9]’. 11 10 9 8 APROXIMACE 13.. for j=1:21 S(i. 1.2 Library Functions y 7 6 5 4 0 %%% Example 13. for i=1.7 10 8.1 Approximate the given sequence of values x=[0. title(’ERROR SURFACE’). xlabel(’x’). plot(xx.2).y(i)). The choice of the type of approximation function • Linear combination of base functions: f (a.2 4. 0...*y) sum(y)]’. by polynomials of orders M = 0.9]’. y=[5.5 2 2.4 4.N by a %%% polynomial f(x)=a(1)*x^M+a(2)*x^(M-1)+ .6 7. by a function f (x) = a(1)/x + a(2) and plot results 13.6 1. M=2 % Definition of given values x=[0 0.5 1 x 1. for i=1:21.’or’. y=[1 1.4 8. xlabel(’x’)...^3) sum(x. a2=2.5 2]’.^2+a(2)*xx+a(3).8 1 x 1.y(i)) %%% for i=1.f).5 EXAMPLES 13 13.1 The Mean Square Error Method %%% Example 13. plot(x. Minimization of the mean square error: N M j=1 a(j) COMMANDS SUM INV POLYFIT POLYVAL STEM MESH MESHC CONTOUR HOLD ON HOLD OFF gj (x) S(a) = i=1 (f (a. xlabel(’a2’).9 1.. subplot(2.9 2]’.9 2]’.5 4.y).2 1. x(i)) − y(i)) 2 (1) 3.4 0. ylabel(’y’).*y) sum(x. a=inv(A)*b % Plot of given and approximation values xx=[0:0.. x) 13.8 1 1. . xx=[0:0..2 1.5 3 3. APPROXIMATION OF FUNCTIONS Problem statement: Approximation of given values {x(i).a1.05:2]’.1: Derive normal equations for approximation of values (x(i). grid on.3 0. title(’CONTOUR PLOT’).5 6 6.9 2. y=[11. mesh(a2.5 3 a1 2 2. x) = • Nonlinear function 2.2. ylabel(’y’).9 10.^4) sum(x.2 0.4 1.5:0.j)=sum((a1(j)*x+a2(i)-y). f=polyval(a.2 0.2 Approximate sequence of values x=[0 0. stem(x..4 0.2 1.7 1. title(’APPROXIMATION’).^2.3 10.1 1.5 a2 4 4.3 7.2 4.5 0.. grid on subplot(1.. 5 and plot dependence of the mean square error with respect to the polynomial order .2. sum(x. x) of parameters a = {a(j)}M j=1 Solution: 1. end.5 a2 2.. Evaluation of constants a of the approximation function f (a. sum(x.^2) sum(x) length(x)].2 1.6 1. xlabel(’a2’).^2).8 2 PLOCHA CHYB APROXIMACE 12 10 8 6 4 2 55 50 45 40 35 30 25 20 15 10 y 0 0 0.1).2.4 8.y.S). hold on. % Evaluation of the sum of square errors for given ranges % of parameters a1.N by a function f(x)=a(1)*x^M+a(2)*x^(M-1)+. f=a(1)*xx.2 0..3 6]’.1 9.5 0.5.3 4.. ylabel(’a1’). y(i)}N by i=1 a function f (a.xx).5 2 VRSTEVNICE 4 3.f).7 2]’. % Definition of the matrix of normal equations and its solution A=[sum(x.5 3 4 2.3 4..4 1.9 10.2: Approximate values (x(i).^2) sum(x).2 6.05:2]’. a(M)*x+a(M+1) for M=1 % Definition of given values x=[0 0.S).9 5.y.13.^3) sum(x.2 0..6 0.5 0.1:4.a1. ylabel(’a1’).6 1. end subplot(2. hold off.5 4.9 5.5 a1 5 4 3 3.^2).7 0.7 0.8 1 1..4).2. % Evaluation of coefficients a and values of % the approximation function a=polyfit(x..1:4.5 3. a2 and plot of results a1=2:0. title(’APPROXIMATION’).xx. b=[sum(x.+a(M)*x+a(M+1).2 8.5 6 6. contour(a2.4 4.1). grid on..6 0.

C2(j)]. alpha=0. plot(c20. c20(1)=-3. Statement of the mean square error evaluation: N S(a) = i=1 (f (a.3 2 2.05:2.7 %%% by a function f(x)=f(x)=1/(exp(c1*x+c2)+1) 0. %%% Example 13.2.6*x+0.y(i)).7 0.6 0.N %%% by a function f(x)=a(1)*x^3+a(2)*x % Definition of given values x=[0 0.4 0. y=[0 0. c10(k+1)=c10(k)-alpha*dc1.8 1 1.end. x(i)) − y(i))2 (2) 2.c10.*x).c20(k)]. plot(x..7 8..’o’). end 0 1 −3 −2 −1 0 0. contour(C2.2 0./(exp(c10(k)*x+c20(k))+1).. title(’APPROXIMATION’). end. axis tight f=1.2 1. y(i)}N by i=1 a function f (a.8 6]’..2 1.^2).2 0. −2 −2 C2 −3 −2 −1 0 1 C1 −2 C2 if(abs(dc1)+abs(dc2)<0.2 Nonlinear Approximation and Gradient Method Problem statement: Approximation of given values {x(i).2: Approximate values (x(i). grid on xlabel(’x’).8 0. C2=-3:0.4).4 for i=1:length(C1). 0.5 0.*. plot(c20. % Plot of given and approximation values function f=fa(c.S).x)). a(2) = a(2) − α ∗ DA2.xx.. end..5 2]’. f=a(1)*xx.1: Approximate values (x(i). y=1.x)).4 0. LINEAR AND NONLINEAR APPROXIMATION 14. 1 2 −1 0 dc2=sum((y-fa([c10(k). ./(exp(-0.^2 . COMMANDS backslash LSQLIN LSQNONLIN FMINSEARCH LSQCURVEFIT SOLVE 14.8 % Solution by gradient method CONTOUR PLOT 0.’o’).xx).6 1..0 1.6 2 c10(1)=1. break.C1./(exp(c(1)*x+c(2))+1).2. grid on...50). for j=1:length(C2) 0.c20(k)]. .2.14.2.*.6 C1=-2:0.2 1 0 dc1=sum((y-fa([c10(k).2:2.f). hold on.y(i)). xx=min(x):0. Gradient evaluation DA1 = ∂(S)/∂(a(1).^3+a(2)*xx.2 S(i. C1 1 2 2 3 EXAMPLES 14 14.000001).S.1).8 10]’. c20(end)].’or’.. hold on. y=[0.C1. Update of coeﬃcients: a(1) = a(1) − α ∗ DA1.2.x) subplot(1.6 1.^3 x].9 2]’..1 1.1 General Linear Approximation %%% Example 14.j)=sum((y-fa([C1(i). 2 0 exp(c10(k)*x+c20(k)). stem(x. M=250. pro i=1. by a function f (x) = x + a(2) ∗ x2 and plot results . pro i=1.3 0. Coeﬃcients estimation: coeﬃcients a.2). DA2 = ∂(S)/∂(a(2).fa([c10(end).x)).4 1. α and loop including 3..^2).7 3. c20(k+1)=c20(k)-alpha*dc2.1:max(x).6 1..8. meshc(C2. 0.y). 0 −1 (exp(c10(k)*x+c20(k)))..1 Approximate given sequence x=[0. plot(xx..4)+1). ylabel(’y’). subplot(2. hold off.9 1.6 % Definition of given values and error surface plot 0.4 for k=1:M 0.’r’). x) of parameters a = {a(j)}M j=1 Solution: 1.c10.2:3.9 5.0 1.5 x=[-3 -1 1 2]’. hold off subplot(2. a=A\y % Plot of given and approximation values xx=0:0. 4.N ERROR SURFACE APPROXIMATION 0.3 1.. 0.y. % Definition of the matrix of normal equations and its solution A=[x./(exp(c10(k)*x+c20(k))+1).

1) for a chosen number N of its values .8 1 EXAMPLES 15 15.N evaluate %%% their mean value.6 0.h.9 2]’. i=1.2: Evaluate and plot the histogram of distribution of %%% random values rands(1000.7 0.2 Analyse distribution of random values generated by function R = randn(N.4 −0..15. standard deviation.1) [h.y) COMMANDS MEAN STD CORRCOEF RANDS HIST BAR %%% Example 15. bar(x. and correlation coefficient % Definition of given values x=[0 0.2 0 0.2.1)).’g’) 120 100 80 60 40 20 0 −1 −0.2 0.8 −0.y(i)).2 0. y=x+0.1).1 Basic statistical characteristics %%% Example 14.6 1. STATISTICAL DATA ANALYSIS 15... % Evaluation [mean(x) mean(y)] [std(x) std(y)] corrcoef(x.8 1 1.1*rands(10..4 0.6 −0.2 1. 5) 15.5 0.x]=hist(rands(1000.1: For the set of given values (x(i).1 Evaluate basic statistical characteristics of all columns of matrix R = rands(100.

1 Symbolic Solution Characteristics: 1.2 Numeric Solution 16. double(r) ezplot(’tan(x)+sin(x)-2’).2.5 0 −0. Deﬁnition of the tangent to function f (x) at point [x(1).2.1: Symbolic solution of equation: ax^2+bx+c=0 syms a b c x r=solve(’a*x^2+b*x+c=0’). Intersection of the tangent with x-axis: x(2) = x(1) − f (x(1))/f (x(1)) %%% Example 16. f (x(1))] y − f (x(1)) = f (x) (x − x(1)) 3.x(2:end)]. eps=0.[-2 8]). if (abs(x(i)-x(i-1))<eps)..length(x(2:end)))]).{1 7 8}) % Example 16. stem(x. grid on tan(x)+sin(x)=2 16. accuracy eps and maximum number of iterations M x(1)=4.’o’). r=roots(c) polar(angle(r).5 30 0 %%% Example 16.[f(x(1:end-1)). 1 0. Symbolic solution is not always possible 2.{a b c}. abs(r). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=f(x) function yd=fd(x) y=sin(x)-0.1 General Iterative Methods for Real Roots Estimation Principle of the Newton Method: 1. Substitution allows conversion to numerical solution % Example 16.. grid on cc=poly(r) 210 330 240 270 300 EXAMPLES 16 16. for i=2:M x(i)=x(i-1)-f(x(i-1))/fd(x(i-1)).1. hold off line([x(1:end-1). zeros(1.2 Roots of Algebraic Equations Equation deﬁnition: f (x) ≡ c1 xn + c2 xn−1 + · · · + cn+1 = 0 180 150 0.1*x.0001.f(x)).1 Evaluate symbolic and numeric solution of a selected nonlinear equation 16.1 x 2 3 x 4 5 6 7 8 90 120 1.5 60 1 16. NONLINEAR EQUATIONS Problem statement: Solution of equation: f (x) = 0 Symbolic solution: manipulation with expressions Numeric solution: (i) separation of roots (ii) iterative approximation of separate roots COMMANDS SYMS SOLVE SUBS EZPLOT PRETTY ROOTS POLY POLAR 16.1*x’.4: Solution of algebraic equation x^12+1=0 c=[1 zeros(1. M=10. Estimation of the initial approximation of the root: x(1) 2. end end ezplot(’sin(x)-0.2: Symbolic solution of equation: syms a b c x r=solve(’tan(x)+sin(x)=2’). yd=cos(x)-0.3: Solution of equation sin(x)-0.16.5 −2 −1 0 1 sin(x)−0.11) 1].1x=0 by Newton method for %%% initial approximation x(1). pretty(simple(r)) r1=subs(r1. grid on hold on.2 Find and plot roots of selected algebraic equations . break.25..5 −1 −1.

y) = 0 Symbolic solution: manipulation with expressions Numeric solution: (i) separation of roots (ii) iterative approximation of separate roots COMMANDS SYMS SOLVE DOUBLE EZPLOT FUNCTION 17. SYSTEM OF NONLINEAR EQUATIONS Problem statement: Solution of equations: f (x. PG=P.’x^2+4*y^2-4=0’). title(’SOLUTION EVOLUTION’) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function z=f(P) function z=g(P) x=P(1).2 0 x 0.5 1 0. break.x.4 −0. (2*x) (8*y)]. Substitution allows conversion to numerical solution % Example 17. y=P(2).5=0’. eps=1e-12. z=x^2+4*y^2-4. x=P(1). y=P(2).’or’). plot(PG’).1 Symbolic Solution Characteristics: 1. DP=inv(DF)*F.8 −0. M=40. R1.Y1([1 4]).8 1 SOLUTION EVOLUTION 4 3.y. accuracy eps and for %%% maximum number of iterations M P=[4 2]’. Y1=double(R1. plot(X1([1 4]).4 0.5 2 1.2 Evaluate numeric solution of a selected system of nonlinear equations .5.6 0.y)=x^2+4*y^2-4=0 syms x y R1=solve(’x^2-2*x-y+0.8 −1 −1 −0.8 0. Symbolic solution is not always possible 2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function W=J(P) x=P(1).6 −0. for k=2:M DF=J(P). P=P+DP. y 0.2 0. end end xk=P(:. grid on hold on.[-1 3]). z=x^2-2*x-y+0. R1.y)=x^2-2*x-y+0.17. Expansion of multivariable functions into Taylor series is used 2.5 0 1 2 3 4 5 6 7 8 EXAMPLES 17 17.2: Solution of system of nonlinear equations %%% Equation1: f(x. y=P(2).4 0.5=0 % Equation2: g(x.4 −0. -g(P)]. W=[(2*x-2) (-1).5=0 %%% Equation2: g(x.1 Evaluate symbolic solution of a selected system of nonlinear equations 17.y)=x^2+4*y^2-4=0 %%% for initial approximation P.y) % Visualization ezplot(’x^2-2*x-y+0. y) = 0 g(x. PG=[PG P].2 Numeric Solution Principle of the Newton method for the system of equations: 1. F=[-f(P).2 0 −0.2 −0.y)=x^2-2*x-y+0. ezplot(’x^2+4*y^2-4=0’). X1=double(R1.x).1: Symbolic solution of system of nonlinear equations % Equation1: f(x. if abs(sum(DP))<eps. grid on.6 −0. Principle is the same as in the one dimensional case %%% Example 17.5 3 2.5=0’.end).6 0. hold off title(’SYSTEM OF NONLINEAR EQUATIONS’) 1 SYSTEM OF NONLINEAR EQUATIONS 17.

’r’).3 with Using symbolical methods ﬁnd extrems and limits of the given function Derive estimates of the diference approximations the ﬁrst and second derivative Evaluate the deﬁnite integral of the given function using various numerical methods a chosen step and compare results with the symbolical solution .’g’) % Evaluation and plot of extrems of the given function x0=double(solve(df)). plot(extrem(:.XI. plot(xi.YI]=meshgrid(-2:0.’o’).’g’. Interpolation in one or more dimensions: linear. N=100.Z.1: 1D and 2D data interpolation figure(1).3: Numerical derivative a=0.5 2 1 0 −1 −2 −2 DATA GRID 2 1 0 −1 1 0.2.4: Symbolic analysis of a given function syms x.ZI+1).YI. axis([-6 6 -8 4]).^2-y.6 0. f=(x+1)/(x^2+5*x+6).’r’).YI).2).1 18. y=sin(x).’LineWidth’. [XI.YI. AM=limit(df. hold off COMMANDS INTERP1 INTERP2 GRIDDATA DIFF LIMIT INT EZPLOT 2D INTERPOLATION 1. hold off 0 −0.^2-Y.x.*exp(-X.Y]=meshgrid(-2:0. [XI.yilin. hold on fint=int(f).XI. mesh(X. pretty(df). grid on.’o’).YI. h=(b-a)/N.5. Q=h/2*(sin(a)+sin(b)+2*sum(sin(a+[1:N-1]*h))) %%% Example 18. z=-x.3 Numeric and Symbolic Integration Basic deﬁnitions: 1. hold off. Z=-X. hold on h=ezplot(df). xi=0:. Possibilities of triangulation based randomly located 3D data points % Example 18. yilin=interp1(x. hold off [X.+inf).*exp(-x.5).1:2). stem3(x. mesh(XI. yd=diff(y)/(pi/100). ezplot(f).5 1 0. Numerical estimate of a derivative: y (k) ≈ (y(k + 1) − y(k))/h 2. Symbolic deﬁnition f (x) = limh→0 (f (x + h) − f (x))/h %%% Example 18.1)-0.YI]=meshgrid(-2:0.’Color’.Y.{x}. % 1D interpolation yisp=interp1(x.^2). mesh(XI.’Color’.’spline’).’Color’.y. NUMERICAL DIFFERENTIATION AND INTEGRATION 18. hold off % Example 18. f=(x^2-5*x+6)/(x-1). %%% Example 18. hold off EXAMPLES 18 18.’spline’). y=4*(rand(100. pretty(f).xi. line([-6 6].2 Numeric and Symbolic Derivative Basic deﬁnitions: 1. x=0:10.x. b=pi. pretty(simple(df)) ezplot(f). h=ezplot(fint). b=pi.6: Symbolic integration syms x.y.18.5).5 18. extrem=[x0 subs(f.2 18.8 0.2 0 2 1 0 −1 −2 −2 0 −1 1 2 (2 x−5)/(x−1)−(x2−5 x+6)/(x−1)2 4 2 0 −2 −4 −6 18.y.double([AM AP]).4 0. x=[a:h:b]. spline 2.1:2).2: data gridding x=4*(rand(100.y. N=100. ZI=griddata(x.-inf). ZI=interp2(X. y=sin(x). y=sin(x). df=diff(f). x=[a:h:b].extrem(:. h=(b-a)/N.’r’).^2)+0.xi.1).5: Numerical integration by the trapesoidal rule a=0. hold on.Y.2:10.xi). hold on.y. grid on. cubic. grid on % Evaluation and plot of the function derivative limits AP=limit(df.1)-0.z. Integration by a trapesoidal rule: Q = b a −8 −6 −4 −2 0 x 2 4 6 f (x) dx ≈ h/2 ∗ (f (a)+2 N −1 i=1 f (a+i h)+f (b)) %%% Example 18.’or’).{x0})] hold on.ZI). set(h. figure(3).yisp. axis([-4 4 -4 4]).5:2).Z). plot(x.1 Interpolation Characteristics: 1.z. set(h. figure(2). % 1D interpolation hold on.

’y(0)=0’. axis tight. xk> clear.MSE=’.5 1 1. y1(i)=y1(i-1)+h*(-2*y1(i-1)-5*y2(i-1)).6 0. where xi = xi−1 + h. xk> %%%%%% A. plot(x. Vector solution: y(i) = y(i−1) + h f (xi−1 . plot(x. Substitution allows conversion to numerical solution %%% Example 19. ys=dsolve(’D2y+2*Dy+5*y=0’.2). N=(xk-x1)/h+1. axis tight subplot(2. x(1)=x1.1 0 −0. y(1)=0.’-or’).y. hold off subplot(2.5 5 4 4.Initial-Value Problem %%% Equation: y’=1-y. axis tight. grid on. for i=2:N x(i)=x(i-1)+h. hold on.5 0. 3. 3. yi }.1 0 0.1): ’).3 0.5 2 2.t.2: Ordinary Differential Equations . axis tight subplot(2.’t’). delete(get(0. axis tight 19.’-or’).’Dy(0)=2’). y2(1)=0.[x1 xk]).03 0. Step value can aﬀect accuracy and stability %%% Example 19. 1−exp(−t) 0.1).Initial-Value Problem % y’’+2y’+5y=0. e=y-subs(ys. x1=0. Step value can aﬀect accuracy and stability %%% Example 19. y2(i)=y2(i-1)+h*y1(i-1). y1(1)=2. %%%%%%%%%%%%%% . y(x1 ) = y1 Solution: (i) analytical: function y(x) (ii) numerical: sequence {xi .1. ezplot(ys.2 0.’children’)).04 0. · · · COMMANDS SYMS DSOLVE PRETTY EZPLOT ODE23 HOLD ON HOLD OFF PLOT 19. for x1=0 in range <x1.05 0.2): ’). yi−1 ).3: System of Ordinary Differential Equations . ezplot(ys. y2(x1)=0..5 0 0. i = 2.5 1 0. Symbolic Solution %%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear. syms t. y(x1)=0.1 Evaluate symbolic solution of a selected system of diﬀerential equations with given initial conditions 19.2). NUMERICAL SOLUTION OF DIFFERENTIAL EQUATIONS Problem statement: Solution of equation: y = f (x.Initial-Value Problem %%% Equation: y’+y=1.1 %% h=input(’Step (=0. · · · and h is a chosen step 2. axis tight %%%%%% B. y(i−1) ).1: Ordinary Differential Equations .x).5 5 EXAMPLES 19 19.num2str(S)]).4 0. hold on.00018658 0.[x1 xk]). y). y(x1 ) = y 1.5 2 2. Approximate solution: yi = yi−1 + h f (xi−1 . delete(get(0.19. xk=5.1.8 0.5 t 3 3.2 0 0 5 10 t ERROR SEQUENCE − MSE=0. end subplot(2.3 Solution of System of Equations 0 5 10 15 Euler method application for solution of a system: y = f (x. y(x1)=0.’y(0)=0’.1.1.5 1 1. grid on.’-o’).5 0 −0. y(i)=y(i-1)+h*(1-y(i-1)).[x1 xk]). ys=dsolve(’Dy+y=1’.[x1 xk]). end hold on. grid on. h=input(’Step (=0.5 (1) 4 4. xk=15.1 Symbolic Solution Characteristics: 1.y2. S=sumsqr(e)/length(e).02 0.19.4 0. subplot(2. for i=2:N x(i)=x(i-1)+h.1).’children’)).2).. y(x1)=0. i = 2. plot(x. N=(xk-x1)/h+1.e. Cont of Ex. axis tight.1). where xi = xi−1 + h. · · · and h is a chosen step 2.’-or’). plot(x.01 0 15 19.1. grid on title([’ERROR SEQUENCE .5 t 3 3. ezplot(ys. pretty(simplify(ys)).5 −exp(−t) sin(2 t)+2 exp(−t) cos(2 t) 2 1. hold off subplot(2. syms t x1=0.2 Numeric Solution Principle of Euler method for the Initial Value Problem: 1. Numeric Solution: Euler Method %%%%%%%%%%%%%%%% % Equation: y1’=-2*y1-5*y2.2 Evaluate numeric solution of a selected system of diﬀerential equations with given initial conditions . i = 2. Symbolic solution is not always possible 2. x(1)=x1. y’(x1)=2 for x1=0 in range <x1. hold off exp(−t) sin(2 t) 0.1. 3. y1(x1)=2 % y2’=y1. y).y1. ezplot(diff(ys).

xb=4. A=[(-2/h^2-3) (1/h^2+1/h) 0 % Function for Ex.y]=ode23(’f’.[y1a y2a]).+1/(3 exp(−12)+exp(4)) exp(t) 1 0.+1/(3 exp(−12)+exp(4)) exp(t) 20. alpha=0.’or’). y(xb)=1.1 Shooting Method Principle: 1. xa=0.. Symbolic Solution %%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear. xb=4 %%%%%% A. y(xb)=1 for xa=0.1 0 DIFFERENCE METHOD N= 4 0 0.’y(0)=0’. % y2=y1. y(xa)=0. BOUNDARY PROBLEM Solution of equation: f (x.5 1 1. plot(x. 3.5 4 Principle: 1. xb=4 %%%%%% A. delete(get(0. end 0.’children’)) syms t.6 0.8 0. y=inv(A)*b. Symbolic Solution %%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear. x=xa:h:xb.3 0. y(1)]. y2(xa)=0.1: Ordinary Differential Equations . t=plot(x. axis tight %%%%%% B. Numeric Solution: Difference Method %%%%%%%%%%% % Equation: y’’(k)+2*y(k)+3*y(k). hold off y1a=y1a-alpha*(y(end. y. yb]. y(xb ) = yb 20.y(:.’children’)) syms t.’r’). yk .’Dy(4)=1’). y1a=input(’Volba pocatecni podminky (=0): ’).2 0. · · · .5 2 t 2. y1(xa)=.8 0.’y(0)=0’. pretty(simplify(ys)). for i=1:30 [x.2)-y2b).3 0.5 0.9 0. % (1/h^2)(y(k-1)-2y(k)+y(k+1)) + 2 (1/(2h))(y(k+1)-y(k-1)) . y(xb)=1 for xa=0... hold on.01. xb=4. y(xa)=0. y ) = 0. hold on. grid on. The new estimate of the second initial condition: y (xa ) = y (xa ) − α (ˆ(xb ) − y(xb )) for a chosen α y %%% Example 20.1 COMMANDS SYMS DSOLVE PRETTY EZPLOT ODE23 INV PLOT −. b= [-(1/h^2-1/h)*ya 0 -(1/h^2+1/h)*yb]’. y.1 Evaluate symbolic solution of a boundary value problem for ordinary diﬀerential equations using the ﬁnite diﬀerence method .7 0. yk . xa=0.7 0.4 0. y2b=1. yk .9 0.2). Initial value problem solution to ﬁnd y (xb ) ˆ 3. 3. Estimate of the second (and further) initial conditions: y (xa ) 2. function yd=f(x. · · · . · · · .1 Evaluate symbolic solution of a boundary value problem for ordinary diﬀerential equations using the shooting method 20. 1/h (yk+1 − yk − 1). % h=(xb-xa)/N. y .5 1 1. pretty(simplify(ys)).5 0. yk ) = 0 for k = 2.2 0. y(xa)=0.2: Ordinary Differential Equations . N and h = (xb − xa)/N 2.’Dy(4)=1’).6 0.Boundary Problem %%% Difference Method: y’’+2y’-3y=0.[xa xb]). Division of the range xa . ys=dsolve(’D2y+2*Dy-3*y=0’. ezplot(ys.1 (1/h^2-1/h) (-2/h^2-3) (1/h^2+1/h) yd=[-2*y(1)+3*y(2). ys=dsolve(’D2y+2*Dy-3*y=0’. y=[ya. 3. 1/h2 (yk−1 − 2 yk + yk + 1)) = 0 for k = 2.5 3 3..2 Diﬀerence Method 0 0 0. axis tight %%%%%% B. Application of diﬀerence approximation of derivatives in equation f (xk .5 3 3. delete(get(0. N 3.3 y(k)=0 N=4. y2a=0. ezplot(ys. Solution of the system of equations: f (xk .5 4 EXAMPLES 20 20. y(xa ) = ya . yb=1.. axis tight. h=(xb-xa)/N. hold off −.20.20.y) ya=0.[xa xb]).4 0.. Numeric Solution: Shooting Method %%%%%%%%%%%%% % Equation: y1’=-2*y1+3*y2. 0 (1/h^2-1/h) (-2/h^2-3) ]. N where y1 = ya and yN +1 = yb %%% Example 20.Boundary Problem %%% Shooting Method: y’’+2y’-3y=0. xb into N strips deﬁning xk = xa + (k − 1) h for k = 2.5 2 t 2.y.[xa xb]. grid on. axis tight.

y ) = 0 for y(xa ) = ya and a chosen value of y (xa ) in the range xa . xb 2. y(0)=10. y .y. hold on pause(0.5 5 Notes: 1.’Color’.95. 12 10 BLOCKS INTEGRATION SUMMATION CONSTANT CLOCK XYGRAPH TO WORKSPACE SHOOTING METHOD y1a Constant xo 1 s −1 Gain 5 x value 1 s y2’ >> y2 8 6 4 2 0 −2 −4 y1’ >> y1 XY Graph1 y To Workspace −6 −8 0 0.1 Evaluate solution of a boundary value problem for ordinary diﬀerential equations in the SIMULINK environment using the shooting method 21.values.21.values(end.2) end hold off %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function dy=ff(x. Depending upon the value of α the whole process can be stable or unstable.[1/M*i 0 0]). Construction of a MATLAB programme to use the SIMULINK model for the initial value problem to evaluate value y (xb ) and its use for estimation of the new value of ˆ the second initial condition: y y (xa ) = y (xa ) − α (ˆ(xb ) − y(xb )) for a chosen α 3. Iterative repetition %%% Example 21. grid on. The SIMULINK run is controlled by the MATLAB programme EXAMPLES 21 21.5 2 2.1: % Solution of the boundary problem by shooting method %%% Using simulation in the SIMULINK environment %%% Differential equation f(x. y(5)=5 clear all.signals.5 1 1. y. y(1)]. sim(’BoundaryProblem’) for i=1:M y1a=y1a-alpha*(y. y2b=5.1)-y2b).5 4 4. M=50. clc y1a=input(’The choice on initial condition (=-20): ’) % Simulation alpha=1.5 3 3.signals.y) dy=[-y(1)+x.y’’)=y’’+y’-x=0. close all. BoundaryProblem. BOUNDARY PROBLEM SIMULATION Principle: 1. monotonic or oscilating 2.2 Compare numeric solution obtained in the previous example with the symbolic one . sim(’BoundaryProblem’) plot(tout.y.y’. Construction of a SIMULINK model for solution of a diﬀerential equation f (x.

Applications • Interpolation of air pollution data observed by ground measuring stations in speciﬁed locations inside the Czech Republic • Satellite data of air pollution de-noising DUST PARTICLES CONCENTRATION NOISY SATELLITE IMAGE DE−NOISED IMAGE 50 0 −50 −100 51 50 Latitude 49 12 14 16 Longitude 18 • Optimization of neural networks • Prediction od gas consumption data p(1) Nonlinear NN: Output and Target Signals 50 40 4 3 2 1 0 4 2 Gradient Method Genetic Algorithm p(2) 30 20 10 0 4 3 2 1 0 4 10 5 0 0 DILATION BIAS 2 0 0 10 5 DILATION p(3) 1995.5 1996 1996.5 1997 Learning Part Error: 6 % Validation part Error: 10 % BIAS • Visualization of biomedical data • Image enhancement • Three-dimensional modelling (a) ORIGINAL IMAGE (b) SUB−IMAGE (c) DE−NOISING (d) ENHANCEMENT .22.5 1996 1996.5 1997 5 Error Signal The First Layer A1=F1(W1*P+B1) The Second Layer A2=F2(W2*A1+B2) p(4) 0 −5 −10 p(5) 1995.