├── DA_Toolbox ├── da │ ├── Data Assimilation Models │ │ └── @dam_D │ │ │ ├── dam_D.m │ │ │ └── private │ │ │ ├── checkArgs.m │ │ │ └── getVars.m │ ├── Noise Models │ │ ├── @nm_ │ │ │ └── nm_.m │ │ ├── @nm_gauss │ │ │ └── nm_gauss.m │ │ ├── @nm_gauss_handle │ │ │ ├── nm_gauss_handle.m │ │ │ └── private │ │ │ │ └── checkArgs.m │ │ ├── @nm_gauss_lti │ │ │ ├── nm_gauss_lti.m │ │ │ └── private │ │ │ │ └── checkArgs.m │ │ ├── @nm_gauss_ltv │ │ │ ├── nm_gauss_ltv.m │ │ │ └── private │ │ │ │ └── checkArgs.m │ │ └── nmAutoSel.m │ ├── Simulation Models │ │ └── @sim_D │ │ │ ├── private │ │ │ └── checkArgs.m │ │ │ └── sim_D.m │ └── State Space Models │ │ ├── @ss_ │ │ └── ss_.m │ │ ├── @ss_D │ │ ├── DEnKF.m │ │ ├── EKF.m │ │ ├── ETKF.m │ │ ├── EnKF.m │ │ ├── EnSRF.m │ │ ├── KF.m │ │ ├── OI.m │ │ ├── PF_ASIR.m │ │ ├── PF_GEN.m │ │ ├── PF_SIR.m │ │ ├── UKF.m │ │ ├── private │ │ │ ├── getU.m │ │ │ └── simCheckArgs.m │ │ ├── sim.m │ │ └── ss_D.m │ │ ├── @ss_DL │ │ ├── private │ │ │ └── checkArgs.m │ │ └── ss_DL.m │ │ ├── @ss_DNL │ │ ├── private │ │ │ └── checkArgs.m │ │ └── ss_DNL.m │ │ └── @ss_DNL_AN │ │ ├── private │ │ └── checkArgs.m │ │ └── ss_DNL_AN.m ├── dademos │ ├── dademos.m │ ├── data │ │ └── contains sample data.txt │ └── demos │ │ ├── demos_da │ │ ├── demo_KF1.m │ │ ├── demo_L3.m │ │ ├── demo_L3_f.m │ │ ├── demo_L3_fjacx.m │ │ ├── demo_L3_h.m │ │ ├── demo_L3_hjacx.m │ │ ├── demo_L3f.m │ │ ├── demo_NLSC.m │ │ ├── demo_NLSC_f.m │ │ ├── demo_NLSC_fjacx.m │ │ ├── demo_NLSC_h.m │ │ ├── demo_NLSC_hjacx.m │ │ ├── demo_TRACK.m │ │ ├── demo_TRACK_R_h.m │ │ ├── demo_TRACK_T_h.m │ │ ├── demo_TRACK_f.m │ │ ├── demo_VDP.m │ │ ├── demo_VDP_f.m │ │ ├── demo_VDP_fjacx.m │ │ ├── demo_VDP_h.m │ │ └── demo_VDP_hjacx.m │ │ ├── demos_nm_ │ │ └── demos_nm_gauss │ │ │ ├── demo_handle_mu.m │ │ │ ├── demo_handle_sigma.m │ │ │ ├── demo_nm_gauss_handle1.m │ │ │ ├── demo_nm_gauss_lti1.m │ │ │ └── demo_nm_gauss_ltv1.m │ │ ├── demos_sim │ │ ├── demo_sim1.m │ │ ├── demo_sim1f.m │ │ └── demo_sim1h.m │ │ ├── demos_sim_D │ │ └── demo_sim_D1.m │ │ └── demos_ss_ │ │ └── demos_ss_D │ │ ├── demo_DNL_AN_f.m │ │ ├── demo_DNL_AN_fJacX.m │ │ ├── demo_DNL_AN_h.m │ │ ├── demo_DNL_AN_hJacX.m │ │ ├── demo_DNL_f.m │ │ ├── demo_DNL_fJacX.m │ │ ├── demo_DNL_h.m │ │ ├── demo_DNL_hJacX.m │ │ ├── demo_ss_DL1.m │ │ ├── demo_ss_DNL1.m │ │ └── demo_ss_DNL_AN1.m ├── dahelp │ ├── dahelp.m │ ├── figures │ │ └── contains figures to use in html help.txt │ └── html │ │ └── contains generated html files from m code.txt ├── daobsolete │ ├── @nm_empty │ │ ├── nm_empty.m │ │ └── private │ │ │ └── checkArgs.m │ └── contains obsolete files.txt ├── datemplates │ ├── nm_gauss_fun_handles │ │ ├── template_mu.m │ │ └── template_sigma.m │ └── ss_D_fun_handles │ │ ├── template_DNL_AN_f.m │ │ ├── template_DNL_AN_fJacX.m │ │ ├── template_DNL_AN_h.m │ │ ├── template_DNL_AN_hJacX.m │ │ ├── template_DNL_f.m │ │ ├── template_DNL_fJacW.m │ │ ├── template_DNL_fJacX.m │ │ ├── template_DNL_h.m │ │ ├── template_DNL_hJacV.m │ │ └── template_DNL_hJacX.m ├── dautils │ ├── DA_Save_Stats │ │ ├── PF_Save_Stats.m │ │ └── P_Save_Stats.m │ ├── DeriveEst │ │ ├── ReadMe.rtf │ │ ├── demo │ │ │ ├── derivest_demo.m │ │ │ ├── html │ │ │ │ ├── derivest_demo.html │ │ │ │ ├── derivest_demo.png │ │ │ │ ├── derivest_demo_01.png │ │ │ │ └── multivariable_calc_demo.html │ │ │ └── multivariable_calc_demo.m │ │ ├── derivest.m │ │ ├── directionaldiff.m │ │ ├── doc │ │ │ ├── DERIVEST.pdf │ │ │ └── DERIVEST.tex │ │ ├── gradest.m │ │ ├── hessdiag.m │ │ ├── hessian.m │ │ └── jacobianest.m │ ├── PF_Resampling │ │ ├── PFRS_multinom.m │ │ ├── PFRS_residual.m │ │ ├── PFRS_stratisfied.m │ │ └── PFRS_systematic.m │ ├── cholSigma.m │ ├── detSigma.m │ ├── diag3D.m │ ├── f2str.m │ ├── findArrayVal.m │ ├── findHighest.m │ ├── findLowest.m │ ├── findNearest.m │ ├── interpolate3D.m │ ├── isdiag.m │ ├── ismonotonic.m │ ├── issym.m │ ├── iszeromean.m │ ├── jacest.m │ ├── numSubplots.m │ └── sqrtmSchur.m └── init.m ├── LICENSE ├── README.md └── manuscript_user_guide.pdf /DA_Toolbox/da/Data Assimilation Models/@dam_D/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | switch strCode 17 | 18 | case 'alg' 19 | 20 | alg=arg; 21 | 22 | if isempty(alg) 23 | error('DA:DataAssimilationModels:dam_D:checkArgs:AlgEmpty','%s must be must be non-empty.',strCode) 24 | end 25 | if ~isa(alg,'char') 26 | error('DA:DataAssimilationModels:dam_D:checkArgs:AlgClassMismatch','%s must be a string value.',strCode) 27 | end 28 | 29 | arg=alg; 30 | 31 | case {'y','xf','u'} 32 | 33 | if ~isa(arg,'double') 34 | error('DA:DataAssimilationModels:dam_D:checkArgs:ClassMismatch','%s must be of class ''double''.',strCode) 35 | end 36 | 37 | N=size(arg); 38 | 39 | if length(N)>2 %check dimensions 40 | error('DA:DataAssimilationModels:dam_D:checkArgs:DimMismatch','%s must be a two-dimensional matrix.',strCode) 41 | end 42 | 43 | case {'xa'} 44 | 45 | if isempty(arg) 46 | error('DA:DataAssimilationModels:dam_D:checkArgs:xaEmpty','%s must be non-empty.',strCode) 47 | end 48 | 49 | if ~isa(arg,'double') 50 | error('DA:DataAssimilationModels:dam_D:checkArgs:xaClassMismatch','%s must be of class ''double''.',strCode) 51 | end 52 | 53 | N=size(arg); 54 | 55 | if length(N)>2 %check dimensions 56 | error('DA:DataAssimilationModels:dam_D:checkArgs:xaDimMismatch','%s must be a two-dimensional matrix.',strCode) 57 | end 58 | 59 | case {'Pf','Pa','K'} 60 | 61 | if ~isa(arg,'double') 62 | error('DA:DataAssimilationModels:dam_D:checkArgs:ClassMismatch','%s must be of class ''double''.',strCode) 63 | end 64 | 65 | N=size(arg); 66 | 67 | if length(N)>3 %check dimensions 68 | error('DA:DataAssimilationModels:dam_D:checkArgs:DimMismatch','%s must be maximum a three-dimensional matrix.',strCode) 69 | end 70 | 71 | case {'k'} 72 | 73 | k=arg; 74 | if isempty(k) 75 | error('DA:SimulatorModels:dam_D:checkArgs:tEmpty','%s must be non-empty.',strCode) 76 | end 77 | 78 | if ~isa(k,'double') 79 | error('DA:SimulatorModels:dam_D:checkArgs:tClassMismatch','%s must be of class ''double''.',strCode) 80 | end 81 | 82 | N=size(k); 83 | 84 | if length(N)>2 %check dimensions 85 | error('DA:SimulatorModels:dam_D:checkArgs:tDimMismatch','%s must be a vector.',strCode) 86 | end 87 | 88 | if min(N(1),N(2))>1 %check dimensions 89 | error('DA:SimulatorModels:dam_D:checkArgs:tDimMismatch','%s must be a vector.',strCode) 90 | end 91 | 92 | if ~ismonotonic(k,1,'INCREASING') 93 | error('DA:SimulatorModels:dam_D:checkArgs:tIndexMono','%s must be strictly monotonically increasing.',strCode) 94 | end 95 | 96 | if ~isempty(find(k<0,1)) 97 | error('DA:SimulatorModels:dam_D:checkArgs:tIndexNeg','%s can not contain negative time values.',strCode) 98 | end 99 | arg=k; 100 | 101 | case 'Ts' 102 | 103 | Ts=arg; 104 | 105 | if isempty(Ts) 106 | error('DA:DataAssimilationModels:dam_D:checkArgs:TsEmpty','%s must be non-empty.',strCode) 107 | end 108 | if ~isa(Ts,'double') 109 | error('DA:DataAssimilationModels:dam_D:checkArgs:TsClassMismatch','%s must be of class ''double''.',strCode) 110 | end 111 | 112 | if length(Ts)>1 113 | error('DA:DataAssimilationModels:dam_D:checkArgs:TsScal','%s must be a scalar.',strCode) 114 | end 115 | if Ts<=0 116 | error('DA:DataAssimilationModels:dam_D:checkArgs:TsNeg','%s must be strictly positive.',strCode) 117 | end 118 | arg=Ts; 119 | 120 | case 'covFull' 121 | 122 | covFull=arg; 123 | 124 | if isempty(covFull) 125 | error('DA:DataAssimilationModels:dam_D:checkArgs:covFullEmpty','%s must be non-empty.',strCode) 126 | end 127 | 128 | if ~isa(covFull,'double') 129 | error('DA:DataAssimilationModels:dam_D:checkArgs:covFullClassMismatch','%s must be of class ''double''.',strCode) 130 | end 131 | 132 | arg=covFull; 133 | 134 | case 'TimeUnit' 135 | 136 | TimeUnit=arg; 137 | 138 | if isempty(TimeUnit) 139 | error('DA:DataAssimilationModels:dam_D:checkArgs:TimeUnitEmpty','%s must be must be non-empty.',strCode) 140 | end 141 | if ~isa(TimeUnit,'char') 142 | error('DA:DataAssimilationModels:dam_D:checkArgs:TsClassMismatch','%s must be a string value.',strCode) 143 | end 144 | 145 | if ~strcmp(TimeUnit,'nanoseconds')&&~strcmp(TimeUnit,'microseconds')&&... 146 | ~strcmp(TimeUnit,'milliseconds')&&~strcmp(TimeUnit,'seconds')&&... 147 | ~strcmp(TimeUnit,'minutes')&&~strcmp(TimeUnit,'hours')&&... 148 | ~strcmp(TimeUnit,'days')&&~strcmp(TimeUnit,'weeks')&&... 149 | ~strcmp(TimeUnit,'months')&&~strcmp(TimeUnit,'years') 150 | 151 | warning('DA:DataAssimilationModels:dam_D:checkArgs:TimeUnitStringErr','%s contained wrong time unit. Set to default ''seconds''.',strCode) 152 | TimeUnit='seconds'; 153 | end 154 | arg=TimeUnit; 155 | 156 | end 157 | 158 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Data Assimilation Models/@dam_D/private/getVars.m: -------------------------------------------------------------------------------- 1 | function vars=getVars(cov,full,ind,dataind) 2 | %GETVARS obtains variances from variance matrix or covariance 3D array 3 | % 4 | % - Input variable(s) - 5 | % COV: variance matrix or covariance 3D array 6 | % FULL: inidication variance matrix (=0) or covariance 3D array (=1) 7 | % IND: index of required states 8 | % DATAINDEX: index of required points 9 | % 10 | % - Output variable(s) - 11 | % VARS: matrix of variances 12 | % 13 | % - Construction - 14 | % VARS=GETVARS(COV,FULL,IND) obtains variances from variance matrix 15 | % or covariance 3D array as specified in IND 16 | 17 | if full 18 | vars=diag3D(cov); 19 | vars=vars(ind,dataind); 20 | else 21 | vars=cov(ind,dataind); 22 | end 23 | 24 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/@nm_/nm_.m: -------------------------------------------------------------------------------- 1 | classdef nm_ 2 | %NM Noise model object 3 | % 4 | % NM has the following child objects: 5 | % * nm_gauss - has the following child objects: 6 | % * nm_gauss_lti 7 | % * nm_gauss_ltv 8 | % * nm_gauss_handle 9 | 10 | 11 | end %classdef -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/@nm_gauss/nm_gauss.m: -------------------------------------------------------------------------------- 1 | classdef nm_gauss < nm_ 2 | %NM_GAUSS Gaussian Noise Model 3 | % 4 | % NM_GAUSS - has the following child objects: 5 | % * nm_gauss_lti 6 | % * nm_gauss_ltv 7 | % * nm_gauss_handle 8 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/@nm_gauss_handle/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | if strcmp(strCode,'mu') 17 | mu=arg; 18 | if ~isa(mu,'function_handle') 19 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:muClassMismatch','Mu must be of class ''function handle''.') 20 | end 21 | 22 | try 23 | tmpMu = mu(1,1); 24 | catch err 25 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:muErr','Error in function mu: %s',err.message) 26 | end 27 | 28 | N=size(tmpMu); 29 | 30 | if length(N)>2||(N(1)>1&&N(2)>1) %check dimensions 31 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:muDimMismatch','Mu must be a one dimensional column vector.') 32 | end 33 | 34 | if N(2)>1 %check if column vector 35 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:muColVec','Mu should be a column vector.') 36 | end 37 | 38 | arg=mu; 39 | 40 | elseif strcmp(strCode,'Sigma') 41 | Sigma=arg; 42 | if ~isa(Sigma,'function_handle') 43 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:sigmaClassMismatch','Sigma must be of class ''function handle''.') 44 | end 45 | 46 | try 47 | tmpSigma = Sigma(1,1); 48 | catch err 49 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:SigmaErr','Error in function Sigma: %s',err.message) 50 | end 51 | 52 | N=size(tmpSigma); 53 | 54 | if length(N)>2 %check dimensions 55 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:sigmaDimMismatch','Sigma must be a two-dimensional matrix.') 56 | end 57 | 58 | if min(N)>1 && ~issym(tmpSigma) %if matrix, must be symmetric 59 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:sigmaSym','Sigma must be square and symmetric') 60 | end 61 | 62 | if min(N)==1 && N(2)>1 %check if column vector 63 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:sigmaColVec','Sigma must be a column vector.') 64 | end 65 | 66 | arg=Sigma; 67 | 68 | elseif strcmp(strCode,'Ts') 69 | 70 | Ts=arg; 71 | 72 | if isempty(Ts) 73 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:TsEmpty','%s must be non-empty.',strCode) 74 | end 75 | if ~isa(Ts,'double') 76 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:TsClassMismatch','%s must be of class ''double''.',strCode) 77 | end 78 | n=size(Ts); 79 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 80 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:TsScal','%s must be a scalar.',strCode) 81 | end 82 | if Ts<=0 83 | error('DA:NoiseModels:nm_gauss_handle:checkArgs:TsNeg','%s must be strictly positive.',strCode) 84 | end 85 | arg=Ts; 86 | 87 | end 88 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/@nm_gauss_lti/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | if strcmp(strCode,'mu') 17 | mu=arg; 18 | 19 | if isempty(mu) 20 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:muEmpty','Mu must be non-empty.') 21 | end 22 | 23 | if ~isa(mu,'double') 24 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:muClassMismatch','Mu must be of class ''double''.') 25 | end 26 | 27 | N=size(mu); 28 | 29 | if length(N)>2||(N(1)>1&&N(2)>1) %check dimensions 30 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:muDimMismatch','Mu must be a one dimensional column vector.') 31 | end 32 | 33 | if N(2)>1 %check if column vector 34 | warning('DA:NoiseModels:nm_gauss_lti:checkArgs:muColVec','Mu is transposed to a column vector.') 35 | mu=mu'; 36 | end 37 | 38 | arg=mu; 39 | 40 | elseif strcmp(strCode,'Sigma') 41 | Sigma=arg; 42 | if isempty(Sigma) 43 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:sigmaEmpty','Sigma must be non-empty.') 44 | end 45 | 46 | if ~isa(Sigma,'double') 47 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:sigmaClassMismatch','Sigma must be of class ''double''.') 48 | end 49 | 50 | N=size(Sigma); 51 | 52 | if length(N)>2 %check dimensions 53 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:sigmaDimMismatch','Sigma must be a column vector or a two-dimensional matrix.') 54 | end 55 | 56 | if min(N)>1 && ~issym(Sigma) %if matrix, must be symmetric 57 | error('DA:NoiseModels:nm_gauss_lti:checkArgs:sigmaSym','Sigma must be square and symmetric') 58 | end 59 | 60 | if min(N)==1 && N(2)>1 %check if column vector 61 | warning('DA:NoiseModels:nm_gauss_lti:checkArgs:sigmaColVec','Sigma is transposed to a column vector.') 62 | Sigma=Sigma'; 63 | end 64 | 65 | arg=Sigma; 66 | end 67 | 68 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/@nm_gauss_ltv/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | if strcmp(strCode,'mu') 17 | mu=arg; 18 | if isempty(mu) 19 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:muEmpty','Mu must be non-empty.') 20 | end 21 | 22 | if ~isa(mu,'double') 23 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:muClassMismatch','Mu must be of class ''double''.') 24 | end 25 | 26 | N=size(mu); 27 | 28 | if length(N)>3||(N(1)>1&&N(2)>1) %check dimensions 29 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:muDimMismatch','Mu must be a 3D column vector.') 30 | end 31 | 32 | if N(2)>1 %check if column vector 33 | warning('DA:NoiseModels:nm_gauss_ltv:checkArgs:muColVec','Mu is transposed to a column vector.') 34 | mu=permute(mu,[2 1 3]); 35 | end 36 | 37 | arg=mu; 38 | 39 | elseif strcmp(strCode,'Sigma') 40 | Sigma=arg; 41 | if isempty(Sigma) 42 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:sigmaEmpty','Sigma must be non-empty.') 43 | end 44 | 45 | if ~isa(Sigma,'double') 46 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:sigmaClassMismatch','Sigma must be of class ''double''.') 47 | end 48 | 49 | N=size(Sigma); 50 | 51 | if length(N)>3 %check dimensions 52 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:sigmaDimMismatch','Sigma must be maximum a three-dimensional matrix.') 53 | end 54 | 55 | if min( [N(1) N(2)] ) > 1 && ~issym(Sigma) 56 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:sigmaSym','Sigma must be square and symmetric') 57 | end 58 | 59 | if min( [N(1) N(2)] ) == 1 && N(2)>1 %check if column vector 60 | warning('DA:NoiseModels:nm_gauss_ltv:checkArgs:sigmaColVec','Sigma is transposed to a column vector.') 61 | Sigma=permute(Sigma,[2 1 3]); 62 | end 63 | 64 | arg=Sigma; 65 | 66 | elseif strcmp(strCode,'kIndex') 67 | kIndex=arg; 68 | if isempty(kIndex) 69 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kIndexEmpty','kIndex must be non-empty.') 70 | end 71 | if ~isa(kIndex,'double') 72 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kIndexClassMismatch','kIndex must be of class ''double''.') 73 | end 74 | 75 | N=size(kIndex); 76 | 77 | if length(N)>2||(N(1)>1&&N(2)>1) %check dimensions 78 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kIndexVec','kIndex must be a one dimensional vector.') 79 | end 80 | 81 | if ~ismonotonic(kIndex,1,'INCREASING') 82 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kIndexMono','kIndex must be strictly monotonically increasing.') 83 | end 84 | 85 | if ~isempty(find(kIndex<0,1)) 86 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kIndexNeg','kIndex can not contain negative values.') 87 | end 88 | 89 | arg=kIndex; 90 | 91 | elseif strcmp(strCode,'kMethod') 92 | kMethod=arg; 93 | if isempty(kMethod) 94 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kMethodEmpty','kMethod must be non-empty.') 95 | end 96 | 97 | if ~isa(kMethod,'char') 98 | error('DA:NoiseModels:nm_gauss_ltv:checkArgs:kMethodClassMismatch','kMethod must be of class ''char''.') 99 | end 100 | 101 | if ~strcmp(kMethod,'low')&&~strcmp(kMethod,'high')&&~strcmp(kMethod,'near') 102 | warning('DA:NoiseModels:nm_gauss_ltv:checkArgs:kMethodStringMismatch','kMethod has wrong string value. Reversed to ''low''.') 103 | kMethod='low'; 104 | end 105 | 106 | arg=kMethod; 107 | end 108 | 109 | 110 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/Noise Models/nmAutoSel.m: -------------------------------------------------------------------------------- 1 | function obj = nmAutoSel(varargin) 2 | %NMAUTOSEL Automatic selection of noise model 3 | % 4 | % - Input variable(s) - 5 | % TYPE: a string that defines the type of distribution. Possible values 6 | % are 'gauss' and 'empty'. 7 | % 8 | % SUBTYPE: a cell or double that defines the subtype of the distribution. 9 | % For type 'gauss': 10 | % * double: when a 3D array is provided a LTV model is created, when a 11 | % regular array is provided a LTI model is created. 12 | % * cell: contains the input arguments of the noise model to create. For 13 | % example: {@funmu,@funSigma} creates nm_gauss_handle(@funmu,@funSigma). 14 | % 15 | % - Output variable(s) - 16 | % OBJ: noise model object. 17 | % 18 | % - Construction - 19 | % OBJ=NMAUTOSEL(TYPE,SUBTYPE) returns a noise model object as specified in 20 | % TYPE and SUBTYPE. 21 | % 22 | % OBJ=NMAUTOSEL(SUBTYPE) returns a gaussian noise model object with 23 | % subtype SUBTYPE. 24 | % 25 | % OBJ=NMAUTOSEL() returns the univariate standard normal distribution. 26 | % ( nm_gauss_lti( ) ) 27 | 28 | subType=0; 29 | narginchk(0,2); 30 | ni=nargin; 31 | 32 | switch ni 33 | case 0 %no arguments: LTI univariate standard normal distribution 34 | args={}; 35 | type='gauss'; 36 | case 1 %one argument: subtype defined by args, type: normal distribution 37 | args=varargin{1}; 38 | type='gauss'; 39 | 40 | case 2 %two arguments: args + type from arguments 41 | args=varargin{1}; 42 | type=varargin{2}; 43 | end 44 | 45 | if isa(args,'cell') %if cell: check content to define subtype 46 | 47 | for i=1:length(args) 48 | 49 | if isa(args{i},'function_handle') %nm_xxx_handle 50 | subType=3; 51 | break; 52 | end 53 | if isa(args{i},'double') 54 | if length(size(args{i}))>2 %nm_xxx_LTV 55 | subType=2; 56 | break; 57 | else %nm_xxx_LTI 58 | subType=1; 59 | end 60 | end 61 | 62 | end %for 63 | 64 | if isempty(args) %empty cell: LTI univariate standard normal distribution 65 | subType=4; 66 | end 67 | 68 | %note: function handle is only possible in cell because two handles are 69 | %required. 70 | 71 | elseif isa(args,'double') 72 | if length(size(args))>2 %nm_xxx_LTV 73 | subType=2; 74 | else %nm_xxx_LTI 75 | subType=1; 76 | end 77 | 78 | end %if 79 | 80 | if strcmp(type,'gauss') %nm_gauss_xxx 81 | 82 | switch subType 83 | case 1 84 | obj=nm_gauss_lti(args); 85 | case 2 86 | obj=nm_gauss_ltv(args); 87 | case 3 88 | obj=nm_gauss_handle(args); 89 | case 4 90 | obj=nm_gauss_lti(); 91 | otherwise 92 | error('DA:NoiseModels:nm:argsErr','Invalid nm arguments.') 93 | end 94 | 95 | else 96 | error('DA:NoiseModels:nm:typeErr','Distribution type unknown.') 97 | end 98 | 99 | end %nmAutoSel -------------------------------------------------------------------------------- /DA_Toolbox/da/Simulation Models/@sim_D/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | switch strCode 17 | 18 | case {'y'} 19 | 20 | if isempty(arg) 21 | error('DA:SimulatorModels:sim_D:checkArgs:yEmpty','%s must be non-empty.',strCode) 22 | end 23 | 24 | if ~isa(arg,'double') 25 | error('DA:SimulatorModels:sim_D:checkArgs:yClassMismatch','%s must be of class ''double''.',strCode) 26 | end 27 | 28 | N=size(arg); 29 | 30 | if length(N)>2 %check dimensions 31 | error('DA:SimulatorModels:sim_D:checkArgs:yDimMismatch','%s must be a two-dimensional matrix.',strCode) 32 | end 33 | 34 | case {'x','u'} 35 | 36 | if ~isa(arg,'double') 37 | error('DA:SimulatorModels:sim_D:checkArgs:ClassMismatch','%s must be of class ''double''.',strCode) 38 | end 39 | 40 | N=size(arg); 41 | 42 | if length(N)>2 %check dimensions 43 | error('DA:SimulatorModels:sim_D:checkArgs:DimMismatch','%s must be a two-dimensional matrix.',strCode) 44 | end 45 | 46 | case {'k'} 47 | 48 | k=arg; 49 | if isempty(k) 50 | error('DA:SimulatorModels:sim_D:checkArgs:tEmpty','%s must be non-empty.',strCode) 51 | end 52 | 53 | if ~isa(k,'double') 54 | error('DA:SimulatorModels:sim_D:checkArgs:tClassMismatch','%s must be of class ''double''.',strCode) 55 | end 56 | 57 | N=size(k); 58 | 59 | if length(N)>2 %check dimensions 60 | error('DA:SimulatorModels:sim_D:checkArgs:tDimMismatch','%s must be a vector.',strCode) 61 | end 62 | 63 | if min(N(1),N(2))>1 %check dimensions 64 | error('DA:SimulatorModels:sim_D:checkArgs:tDimMismatch','%s must be a vector.',strCode) 65 | end 66 | 67 | if ~ismonotonic(k,1,'INCREASING') 68 | error('DA:SimulatorModels:sim_D:checkArgs:tIndexMono','%s must be strictly monotonically increasing.',strCode) 69 | end 70 | 71 | if ~isempty(find(k<0,1)) 72 | error('DA:SimulatorModels:sim_D:checkArgs:tIndexNeg','%s can not contain negative time values.',strCode) 73 | end 74 | arg=k; 75 | 76 | case 'Ts' 77 | 78 | Ts=arg; 79 | 80 | if isempty(Ts) 81 | error('DA:SimulatorModels:sim_D:checkArgs:TsEmpty','%s must be non-empty.',strCode) 82 | end 83 | if ~isa(Ts,'double') 84 | error('DA:SimulatorModels:sim_D:checkArgs:TsClassMismatch','%s must be of class ''double''.',strCode) 85 | end 86 | n=size(Ts); 87 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 88 | error('DA:SimulatorModels:sim_D:checkArgs:TsScal','%s must be a scalar.',strCode) 89 | end 90 | if Ts<=0 91 | error('DA:SimulatorModels:sim_D:checkArgs:TsNeg','%s must be strictly positive.',strCode) 92 | end 93 | arg=Ts; 94 | 95 | case 'TimeUnit' 96 | 97 | TimeUnit=arg; 98 | 99 | if isempty(TimeUnit) 100 | error('DA:SimulatorModels:sim_D:checkArgs:TimeUnitEmpty','%s must be must be non-empty.',strCode) 101 | end 102 | if ~isa(TimeUnit,'char') 103 | error('DA:SimulatorModels:sim_D:checkArgs:TsClassMismatch','%s must be a string value.',strCode) 104 | end 105 | 106 | if ~strcmp(TimeUnit,'nanoseconds')&&~strcmp(TimeUnit,'microseconds')&&... 107 | ~strcmp(TimeUnit,'milliseconds')&&~strcmp(TimeUnit,'seconds')&&... 108 | ~strcmp(TimeUnit,'minutes')&&~strcmp(TimeUnit,'hours')&&... 109 | ~strcmp(TimeUnit,'days')&&~strcmp(TimeUnit,'weeks')&&... 110 | ~strcmp(TimeUnit,'months')&&~strcmp(TimeUnit,'years') 111 | 112 | warning('DA:SimulatorModels:sim_D:checkArgs:TimeUnitStringErr','%s contained wrong time unit. Set to default ''seconds''.',strCode) 113 | TimeUnit='seconds'; 114 | end 115 | arg=TimeUnit; 116 | 117 | end 118 | 119 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_/ss_.m: -------------------------------------------------------------------------------- 1 | classdef ss_ 2 | %SS_ State Space Model object 3 | 4 | end %classdef -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_D/DEnKF.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/da/State Space Models/@ss_D/DEnKF.m -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_D/private/getU.m: -------------------------------------------------------------------------------- 1 | function newU=getU(u,index) 2 | %GETU obtains input column vector from matrix 3 | % 4 | % - Input variable(s) - 5 | % U: matrix of input column vectors 6 | % 7 | % INDEX: selected column number 8 | % 9 | % - Output variable(s) - 10 | % NEWU: selected input column vector 11 | % 12 | % - Construction - 13 | % NEWU=GETU(U,INDEX) obtains the input column vector defined by INDEX value 14 | % from matrix U. If U is empty returns empty matrix []. 15 | 16 | if isempty(u) 17 | newU=[]; 18 | else 19 | newU=u(:,index); 20 | end 21 | 22 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_D/private/simCheckArgs.m: -------------------------------------------------------------------------------- 1 | function arg=simCheckArgs(arg,strCode) 2 | %SIMCHECKARGS Check simulation methods arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = SIMCHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | switch strCode 17 | 18 | case {'u'} 19 | 20 | if ~isa(arg,'double') 21 | error('DA:StateSpaceModels:ss_D:checkArgs:ClassMismatch','%s must be of class ''double''.',strCode) 22 | end 23 | 24 | N=size(arg); 25 | 26 | if length(N)>2 %check dimensions 27 | error('DA:StateSpaceModels:ss_D:checkArgs:DimMismatch','%s must be a two-dimensional matrix.',strCode) 28 | end 29 | 30 | case 'x0' 31 | 32 | x0=arg; 33 | 34 | if ~isa(x0,'double') 35 | error('DA:StateSpaceModels:ss_D:checkArgs:x0ClassMismatch','x0 must be of class ''double''.') 36 | end 37 | 38 | N=size(x0); 39 | 40 | if length(N)>2||(N(1)>1&&N(2)>1) %check dimensions 41 | error('DA:StateSpaceModels:ss_D:checkArgs:x0DimMismatch','x0 must be a one dimensional column vector.') 42 | end 43 | 44 | if N(2)>1 %check if column vector 45 | warning('DA:StateSpaceModels:ss_D:checkArgs:x0ColVec','x0 is transposed to a column vector.') 46 | x0=x0'; 47 | end 48 | 49 | arg=x0; 50 | 51 | case 'samples' 52 | 53 | samples=arg; 54 | 55 | if isempty(samples) 56 | error('DA:StateSpaceModels:ss_D:checkArgs:samplesEmpty','%s must be non-empty.',strCode) 57 | end 58 | if ~isa(samples,'double') 59 | error('DA:StateSpaceModels:ss_D:checkArgs:samplesClassMismatch','%s must be of class ''double''.',strCode) 60 | end 61 | n=size(samples); 62 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 63 | error('DA:StateSpaceModels:ss_D:checkArgs:samplesScal','%s must be a scalar.',strCode) 64 | end 65 | if samples<=0 66 | error('DA:StateSpaceModels:ss_D:checkArgs:samplesNeg','%s must be strictly positive.',strCode) 67 | end 68 | arg=samples; 69 | 70 | 71 | case 'noise' 72 | 73 | noise=arg; 74 | 75 | if isempty(noise) 76 | error('DA:StateSpaceModels:ss_D:checkArgs:noiseEmpty','%s must be non-empty.',strCode) 77 | end 78 | 79 | if ~isa(noise,'double') 80 | error('DA:StateSpaceModels:ss_D:checkArgs:noiseClassMismatch','%s must be of class ''double''.',strCode) 81 | end 82 | 83 | n=size(noise); 84 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 85 | error('DA:StateSpaceModels:ss_D:checkArgs:noiseScal','%s must be a scalar.',strCode) 86 | end 87 | if noise~=0 && noise~=1 && noise~=2 && noise~=3 88 | warning('DA:StateSpaceModels:ss_D:checkArgs:noiseNeg','%s must have value 0, 1, 2 or 3: %s set to default 3',strCode,strCode) 89 | noise=3; 90 | end 91 | arg=noise; 92 | 93 | 94 | case {'conf'} 95 | 96 | conf=arg; 97 | 98 | if ~isa(conf,'char') 99 | error('DA:StateSpaceModels:ss_D:checkArgs:ClassMismatch','%s must be of class ''char''.',strCode) 100 | end 101 | 102 | arg=conf; 103 | 104 | end 105 | 106 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_D/sim.m: -------------------------------------------------------------------------------- 1 | function simModel=sim(ssObj,samples,u,conf,x0,noise) 2 | %SIM simulates any discrete State-Space object 3 | % 4 | % - Input variable(s) - 5 | % SSOBJ: any discrete State-Space object. (type 'help ss_D') 6 | % 7 | % SAMPLES: amount of samples/iterations to be simulated. Default=1. 8 | % 9 | % U: matrix of inputs where every column is a input vector. Can be left 10 | % empty ([ ]) if desired. 11 | % 12 | % CONF: string that indicates which variables must reside in the 13 | % simulation model. Possible string values contain 'x', 'u' and 'y'. 14 | % For example: 'x y' retains x and y matrices. 15 | % Default: 'x u y' 16 | % 17 | % X0: initial state column vector. If left empty ([ ]), a sample will be 18 | % taken from the x0 noise model of the State-Space model. 19 | % 20 | % NOISE: option to specify noise behaviour during simulation. 21 | % * 0: no noise 22 | % * 1: only simulate process noise 23 | % * 2: only simulate measurement noise 24 | % * 3: simulate process and measurement noise (Default) 25 | % 26 | % - Output variable(s) - 27 | % SIMMODEL: a discrete time State-Space simulation model of type sim_D. 28 | % 29 | % - Construction - 30 | % SIMMODEL=SIM(SSOBJ,SAMPLES,U,CONF,X0,NOISE) simulates the discrete 31 | % State-Space object SSOBJ. 32 | % 33 | % SIMMODEL=SIM(SSOBJ,SAMPLES,U,CONF,X0) simulates the discrete State-Space 34 | % object SSOBJ with both process and measurement noise. 35 | % 36 | % SIMMODEL=SIM(SSOBJ,SAMPLES,U,CONF) simulates the discrete State-Space 37 | % object SSOBJ with both process and measurement noise and with the 38 | % initial state X0 a sample from the x0 noise model of SSOBJ. 39 | % 40 | % SIMMODEL=SIM(SSOBJ,SAMPLES,U) simulates the discrete State-Space 41 | % object SSOBJ with both process and measurement noise and with the 42 | % initial state X0 a sample from the x0 noise model of SSOBJ, CONF='x u y'. 43 | % 44 | % SIMMODEL=SIM(SSOBJ,SAMPLES) simulates the discrete State-Space 45 | % object SSOBJ with both process and measurement noise and with the 46 | % initial state X0 a sample from the x0 noise model of SSOBJ, CONF='x u y' 47 | % and no inputs (U=[]). 48 | % 49 | % SIMMODEL=SIM(SSOBJ) simulates one sample of the discrete State-Space 50 | % object SSOBJ with both process and measurement noise and with the 51 | % initial state X0 a sample from the x0 noise model of SSOBJ, CONF='x u y' 52 | % and no inputs (U=[]). 53 | % 54 | 55 | % check amount of input arguments 56 | narginchk(1,6); 57 | ni = nargin; 58 | 59 | if ni<2;samples=1;end; 60 | if ni<3;u=[];end; 61 | if ni<4;conf='x u y';end; 62 | if ni<5;x0=[];end; 63 | if ni<6;noise=3;end; 64 | 65 | if isempty(samples);samples=1;end; 66 | if isempty(conf);conf='x u y';end; 67 | if isempty(noise);noise=3;end; 68 | 69 | % check content of input arguments 70 | samples=simCheckArgs(samples,'samples'); 71 | u=simCheckArgs(u,'u'); 72 | conf=simCheckArgs(conf,'conf'); 73 | x0=simCheckArgs(x0,'x0'); 74 | noise=simCheckArgs(noise,'noise'); 75 | 76 | % determine which variables must be retained 77 | xRet = ~isempty(strfind(conf,'x')); 78 | uRet = ~isempty(strfind(conf,'u')); 79 | 80 | %samples can not be larger than amount of u's 81 | if ~isempty(u) && (samples > size(u,2)) 82 | samples=size(u,2); 83 | end 84 | %obtain k0 and Ts from ssobj 85 | k0=ssObj.k0; 86 | Ts=ssObj.Ts; 87 | kIndex=k0:1:(k0+samples-1); 88 | 89 | %acquire x0 from input of from noise model within ssobj 90 | if isempty(x0) 91 | x = sample(ssObj.x0,1,k0); 92 | else 93 | x=x0; 94 | end 95 | 96 | %noise int to bits 97 | switch noise 98 | case 0 99 | pn=0;mn=0; 100 | case 1 101 | pn=1;mn=0; 102 | case 2 103 | pn=0;mn=1; 104 | case 3 105 | pn=1;mn=1; 106 | end 107 | 108 | %already evaluate first vars to determine sizes 109 | if mn==1;y = eval_htot(ssObj,x,k0,getU(u,1),[]);end %y0 110 | if mn==0;y = eval_htot(ssObj,x,k0,getU(u,1),0);end %y0 111 | 112 | %pre-allocate memory & fill in first sample for x and y 113 | if xRet; xSim = zeros(size(x,1),samples);xSim(:,1)=x;end; 114 | ySim = zeros(size(y,1),samples);ySim(:,1)=y; 115 | 116 | %obtain other samples 117 | for i=2:samples 118 | %states 119 | if pn==1;x = eval_ftot(ssObj,x,kIndex(i-1),getU(u,i-1),[]);end 120 | if pn==0;x = eval_ftot(ssObj,x,kIndex(i-1),getU(u,i-1),0);end 121 | %measurements 122 | if mn==1;y = eval_htot(ssObj,x,kIndex(i),getU(u,i),[]);end; 123 | if mn==0;y = eval_htot(ssObj,x,kIndex(i),getU(u,i),0);end; 124 | 125 | if xRet; xSim(:,i)=x;end; 126 | ySim(:,i)=y; 127 | 128 | end 129 | 130 | %make simModel 131 | if ~uRet;u=[];end; 132 | if ~xRet;xSim=[];end; 133 | 134 | if ~isempty(u);u=u(:,1:samples);end; 135 | 136 | simModel = sim_D(xSim,u,ySim,kIndex,Ts,ssObj.TimeUnit); 137 | 138 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_D/ss_D.m: -------------------------------------------------------------------------------- 1 | classdef ss_D < ss_ 2 | %SS_D Discrete State Space Model object with child objects: 3 | % * SS_DL 4 | % * SS_DNL_AN 5 | % * SS_DNL_AMN 6 | % * SS_DNL 7 | % 8 | % SS_D Properties: 9 | % 10 | % SS_D Construction: 11 | % 12 | % SS_D Methods: 13 | % sim - simulates any discrete State-Space object 14 | % da - data assimulates any discrete State-Space object 15 | % KF - Kalman Filter for any discrete State-Space object 16 | % EKF - Extended Kalman Filter for any discrete State-Space object 17 | % OI - Optimal Interpolation Technique 18 | % UKF - Unscented Kalman Filter for any discrete State-Space object 19 | % EnKF - Ensemble Kalman Filter for any discrete State-Space object 20 | % DEnKF - Deterministic Ensemble Kalman Filter for any discrete State-Space object 21 | % PF_GEN - Generic Particle filter for any discrete State-Space object 22 | % PF_SIR - Sampling Importance Resampling (SIR) Filter algorithm for any discrete State-Space object 23 | % PF_ASIR - Auxiliary SIR Particle Filter algorithm for any discrete State-Space object 24 | 25 | methods 26 | 27 | %SIMULATION 28 | simModel=sim(ssObj,samples,u,conf,x0,noise) 29 | 30 | %KALMAN FILTER 31 | daModel=KF(ssObj,varargin) 32 | 33 | %EXTENDED KALMAN FILTER 34 | daModel=EKF(ssObj,varargin) 35 | 36 | %OPTIMAL INTERPOLATION TECHNIQUE 37 | daModel=OI(ssObj,varargin) 38 | 39 | %UNSCENTED KALMAN FILTER 40 | daModel=UKF(ssObj,varargin) 41 | 42 | %ENSEMBLE KALMAN FILTER 43 | daModel=EnKF(ssObj,varargin) 44 | 45 | %DETERMINISTIC ENSEMBLE KALMAN FILTER 46 | daModel=DEnKF(ssObj,varargin) 47 | 48 | %ENSEMBLE TRANSFORM KALMAN FILTER 49 | daModel=ETKF(ssObj,varargin) 50 | 51 | %ENSEMBLE SQUARE ROOT FILTER 52 | daModel=EnSRF(ssObj,varargin) 53 | 54 | %GENERIC PARTICLE FILTER 55 | daModel=PF_GEN(ssObj,varargin) 56 | 57 | %SIR PARTICLE FILTER 58 | daModel=PF_SIR(ssObj,varargin) 59 | 60 | %ASIR PARTICLE FILTER 61 | daModel=PF_ASIR(ssObj,varargin) 62 | 63 | %GENERAL data assimilation 64 | function daModel=da(ssObj,alg,varargin) 65 | %DA performs data assimilation on a discrete State-Space object 66 | % 67 | % - Input variable(s) - 68 | % SSOBJ: any discrete State-Space object. (type 'help ss_D') 69 | % 70 | % ALG: string that contains desired data assimilation algorithm. 71 | % Possible algorithms are: 72 | % * 'KF' Kalman Filter 73 | % * 'EKF' Extended Kalman Filter 74 | % * 'OI' Optimal Interpolation Technique 75 | % * 'UKF' Unscented Kalman Filter 76 | % * 'EnKF' Ensemble Kalman Filter 77 | % * 'DEnKF' Deterministic Ensemble Kalman Filter 78 | % * 'ETKF' Ensemble Transform Kalman Filter 79 | % * 'EnSRF' Ensemble Square Root Filter 80 | % * 'PF_GEN' Generic Particle filter 81 | % * 'PF_SIR' SIR Particle filter 82 | % * 'PF_ASIR' Auxiliary SIR Particle filter 83 | % 84 | % VARARGIN: variable amount of arguments specific to the data 85 | % assimilation algorithm. Look at the help of the desired 86 | % algorithm for more details. 87 | % 88 | % - Output variable(s) - 89 | % DAMODEL: a discrete time State-Space data assimilation model of 90 | % type dam_D. 91 | % 92 | % - Construction - 93 | % DAMODEL=SIM(SSOBJ,ALG,VARARGIN) data assimilates the discrete 94 | % State-Space object SSOBJ with algorithm ALG and arguments 95 | % VARARGIN. 96 | 97 | narginchk(3, inf); %varargin must contain measurements 98 | 99 | if ~isa(alg,'char') 100 | error('DA:StateSpaceModels:ss_D:ss_D:methodClassMismatch','alg must be of class ''char''.') 101 | end 102 | 103 | switch alg 104 | case'KF' 105 | daModel = KF(ssObj,varargin); 106 | case'EKF' 107 | daModel = EKF(ssObj,varargin); 108 | case'OI' 109 | daModel = OI(ssObj,varargin); 110 | case'UKF' 111 | daModel = UKF(ssObj,varargin); 112 | case'EnKF' 113 | daModel = EnKF(ssObj,varargin); 114 | case'DEnKF' 115 | daModel = DEnKF(ssObj,varargin); 116 | case'ETKF' 117 | daModel = ETKF(ssObj,varargin); 118 | case'EnSRF' 119 | daModel = EnSRF(ssObj,varargin); 120 | case'PF_GEN' 121 | daModel = PF_GEN(ssObj,varargin); 122 | case'PF_SIR' 123 | daModel = PF_SIR(ssObj,varargin); 124 | case'PF_ASIR' 125 | daModel = PF_ASIR(ssObj,varargin); 126 | otherwise 127 | error('DA:StateSpaceModels:ss_D:ss_D:ssobjUnknown','Specified algorithm unknown. Please check string.') 128 | end 129 | 130 | end 131 | 132 | end %methods 133 | 134 | methods (Access=private) 135 | 136 | 137 | 138 | end %methods 139 | 140 | end %classdef -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_DNL/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | switch strCode 17 | 18 | case {'f','h'} 19 | 20 | if isempty(arg) 21 | error('DA:SystemModels:ss_DNL:checkArgs:argEmpty','%s must be non-empty.',strCode) 22 | end 23 | 24 | if ~isa(arg,'function_handle') 25 | error('DA:SystemModels:ss_DNL:checkArgs:ClassMismatch','%s must be of class ''function_handle''.',strCode) 26 | end 27 | 28 | case {'fJacX','fJacW','hJacX','hJacV'} 29 | 30 | if ~isa(arg,'function_handle') && ~isempty(arg) 31 | error('DA:SystemModels:ss_DNL:checkArgs:ClassMismatch','%s must be of class ''function_handle''.',strCode) 32 | end 33 | 34 | case 'x0' 35 | 36 | x0=arg; 37 | 38 | if isempty(x0) 39 | error('DA:SystemModels:ss_DNL:checkArgs:x0Empty','%s must be non-empty.',strCode) 40 | end 41 | if ~isa(x0,'double')&&~isa(x0,'nm_')&&~isa(x0,'cell') 42 | error('DA:SystemModels:ss_DNL:checkArgs:x0ClassMismatch','Class mismatch %s.',strCode) 43 | end 44 | arg=x0; 45 | 46 | case 'w' 47 | 48 | w=arg; 49 | 50 | if isempty(w) 51 | error('DA:SystemModels:ss_DNL:checkArgs:wEmpty','%s must be non-empty.',strCode) 52 | end 53 | if ~isa(w,'double')&&~isa(w,'nm_')&&~isa(w,'cell') 54 | error('DA:SystemModels:ss_DNL:checkArgs:wClassMismatch','Class mismatch %s.',strCode) 55 | end 56 | arg=w; 57 | 58 | case 'v' 59 | 60 | v=arg; 61 | 62 | if isempty(v) 63 | error('DA:SystemModels:ss_DNL:checkArgs:vEmpty','%s must be non-empty.',strCode) 64 | end 65 | if ~isa(v,'double')&&~isa(v,'nm_')&&~isa(v,'cell') 66 | error('DA:SystemModels:ss_DNL:checkArgs:vClassMismatch','Class mismatch %s.',strCode) 67 | end 68 | arg=v; 69 | 70 | case 'Ts' 71 | 72 | Ts=arg; 73 | 74 | if isempty(Ts) 75 | error('DA:SystemModels:ss_DNL:checkArgs:TsEmpty','%s must be non-empty.',strCode) 76 | end 77 | if ~isa(Ts,'double') 78 | error('DA:SystemModels:ss_DNL:checkArgs:TsClassMismatch','%s must be of class ''double''.',strCode) 79 | end 80 | n=size(Ts); 81 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 82 | error('DA:SystemModels:ss_DNL:checkArgs:TsScal','%s must be a scalar.',strCode) 83 | end 84 | if Ts<=0 85 | error('DA:SystemModels:ss_DNL:checkArgs:TsNeg','%s must be strictly positive.',strCode) 86 | end 87 | arg=Ts; 88 | 89 | case 'k0' 90 | 91 | k0=arg; 92 | 93 | if isempty(k0) 94 | error('DA:SystemModels:ss_DNL:checkArgs:k0Empty','%s must be non-empty.',strCode) 95 | end 96 | if ~isa(k0,'double') 97 | error('DA:SystemModels:ss_DNL:checkArgs:k0ClassMismatch','%s must be of class ''double''.',strCode) 98 | end 99 | 100 | if length(k0)>1 %check dimensions 101 | error('DA:SystemModels:ss_DNL:checkArgs:k0Scal','%s must be a scalar.',strCode) 102 | end 103 | 104 | if rem(k0,1) ~= 0 %remainder after division 105 | error('DA:SystemModels:ss_DNL:checkArgs:k0Int','%s must be an integer.',strCode) 106 | end 107 | 108 | if k0<0 109 | error('DA:SystemModels:ss_DNL:checkArgs:k0Neg','%s must be non-negative.',strCode) 110 | end 111 | arg=k0; 112 | 113 | case 'TimeUnit' 114 | 115 | TimeUnit=arg; 116 | 117 | if isempty(TimeUnit) 118 | error('DA:SystemModels:ss_DNL:checkArgs:TimeUnitEmpty','%s must be must be non-empty.',strCode) 119 | end 120 | if ~isa(TimeUnit,'char') 121 | error('DA:SystemModels:ss_DNL:checkArgs:TsClassMismatch','%s must be a string value.',strCode) 122 | end 123 | 124 | if ~strcmp(TimeUnit,'nanoseconds')&&~strcmp(TimeUnit,'microseconds')&&... 125 | ~strcmp(TimeUnit,'milliseconds')&&~strcmp(TimeUnit,'seconds')&&... 126 | ~strcmp(TimeUnit,'minutes')&&~strcmp(TimeUnit,'hours')&&... 127 | ~strcmp(TimeUnit,'days')&&~strcmp(TimeUnit,'weeks')&&... 128 | ~strcmp(TimeUnit,'months')&&~strcmp(TimeUnit,'years') 129 | 130 | warning('DA:SystemModels:ss_DNL:checkArgs:TimeUnitStringErr','%s contained wrong time unit. Set to default ''seconds''.',strCode) 131 | TimeUnit='seconds'; 132 | 133 | end 134 | arg=TimeUnit; 135 | 136 | end 137 | 138 | end -------------------------------------------------------------------------------- /DA_Toolbox/da/State Space Models/@ss_DNL_AN/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | %CHECKARGS Check arguments 3 | % 4 | % - Input variable(s) - 5 | % ARG: argument to be checked 6 | % STRCODE: string code that specifies the type of argument. 7 | % 8 | % - Output variable(s) - 9 | % ARG: checked and possibly adjusted argument 10 | % 11 | % - Construction - 12 | % ARG = CHECKARGS(ARG,STRCODE) checks the argument provided in ARG based 13 | % on the string code specified by STRCODE. If ARG does not contain a valid 14 | % format or value an error is generated. 15 | 16 | switch strCode 17 | 18 | case {'f','h'} 19 | 20 | if isempty(arg) 21 | error('DA:SystemModels:ss_DNL_AN:checkArgs:argEmpty','%s must be non-empty.',strCode) 22 | end 23 | 24 | if ~isa(arg,'function_handle') 25 | error('DA:SystemModels:ss_DNL_AN:checkArgs:ClassMismatch','%s must be of class ''function_handle''.',strCode) 26 | end 27 | 28 | case {'fJacX','hJacX'} 29 | 30 | if ~isa(arg,'function_handle') && ~isempty(arg) 31 | error('DA:SystemModels:ss_DNL_AN:checkArgs:ClassMismatch','%s must be of class ''function_handle''.',strCode) 32 | end 33 | 34 | case 'x0' 35 | 36 | x0=arg; 37 | if isempty(x0) 38 | error('DA:SystemModels:ss_DNL_AN:checkArgs:x0Empty','%s must be non-empty.',strCode) 39 | end 40 | if ~isa(x0,'double')&&~isa(x0,'nm_')&&~isa(x0,'cell') 41 | error('DA:SystemModels:ss_DNL_AN:checkArgs:x0ClassMismatch','Class mismatch %s.',strCode) 42 | end 43 | arg=x0; 44 | 45 | case 'w' 46 | 47 | w=arg; 48 | 49 | if isempty(w) 50 | error('DA:SystemModels:ss_DNL_AN:checkArgs:wEmpty','%s must be non-empty.',strCode) 51 | end 52 | if ~isa(w,'double')&&~isa(w,'nm_')&&~isa(w,'cell') 53 | error('DA:SystemModels:ss_DNL_AN:checkArgs:wClassMismatch','Class mismatch %s.',strCode) 54 | end 55 | arg=w; 56 | 57 | case 'v' 58 | 59 | v=arg; 60 | 61 | if isempty(v) 62 | error('DA:SystemModels:ss_DNL_AN:checkArgs:vEmpty','%s must be non-empty.',strCode) 63 | end 64 | if ~isa(v,'double')&&~isa(v,'nm_')&&~isa(v,'cell') 65 | error('DA:SystemModels:ss_DNL_AN:checkArgs:vClassMismatch','Class mismatch %s.',strCode) 66 | end 67 | arg=v; 68 | 69 | case 'k0' 70 | 71 | k0=arg; 72 | 73 | if isempty(k0) 74 | error('DA:SystemModels:ss_DNL_AN:checkArgs:k0Empty','%s must be non-empty.',strCode) 75 | end 76 | if ~isa(k0,'double') 77 | error('DA:SystemModels:ss_DNL_AN:checkArgs:k0ClassMismatch','%s must be of class ''double''.',strCode) 78 | end 79 | 80 | if length(k0)>1 %check dimensions 81 | error('DA:SystemModels:ss_DNL_AN:checkArgs:k0Scal','%s must be a scalar.',strCode) 82 | end 83 | 84 | if rem(k0,1) ~= 0 %remainder after division 85 | error('DA:SystemModels:ss_DNL_AN:checkArgs:k0Int','%s must be an integer.',strCode) 86 | end 87 | 88 | if k0<0 89 | error('DA:SystemModels:ss_DNL_AN:checkArgs:k0Neg','%s must be non-negative.',strCode) 90 | end 91 | arg=k0; 92 | 93 | case 'Ts' 94 | 95 | Ts=arg; 96 | 97 | if isempty(Ts) 98 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TsEmpty','%s must be non-empty.',strCode) 99 | end 100 | if ~isa(Ts,'double') 101 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TsClassMismatch','%s must be of class ''double''.',strCode) 102 | end 103 | n=size(Ts); 104 | if length(n)>2||(n(1)>1||n(2)>1) %check dimensions 105 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TsScal','%s must be a scalar.',strCode) 106 | end 107 | if Ts<=0 108 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TsNeg','%s must be strictly positive.',strCode) 109 | end 110 | arg=Ts; 111 | 112 | case 'TimeUnit' 113 | 114 | TimeUnit=arg; 115 | 116 | if isempty(TimeUnit) 117 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TimeUnitEmpty','%s must be must be non-empty.',strCode) 118 | end 119 | if ~isa(TimeUnit,'char') 120 | error('DA:SystemModels:ss_DNL_AN:checkArgs:TsClassMismatch','%s must be a string value.',strCode) 121 | end 122 | 123 | if ~strcmp(TimeUnit,'nanoseconds')&&~strcmp(TimeUnit,'microseconds')&&... 124 | ~strcmp(TimeUnit,'milliseconds')&&~strcmp(TimeUnit,'seconds')&&... 125 | ~strcmp(TimeUnit,'minutes')&&~strcmp(TimeUnit,'hours')&&... 126 | ~strcmp(TimeUnit,'days')&&~strcmp(TimeUnit,'weeks')&&... 127 | ~strcmp(TimeUnit,'months')&&~strcmp(TimeUnit,'years') 128 | 129 | warning('DA:SystemModels:ss_DNL_AN:checkArgs:TimeUnitStringErr','%s contained wrong time unit. Set to default ''seconds''.',strCode) 130 | TimeUnit='seconds'; 131 | 132 | end 133 | arg=TimeUnit; 134 | 135 | end 136 | 137 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/dademos.m: -------------------------------------------------------------------------------- 1 | % =================================================================== % 2 | % Interactively shows a list of the data assimilation toolbox demos % 3 | % =================================================================== % 4 | % 5 | % demos_nm_ 6 | % 7 | % demos_nm_gauss 8 | % - demo_nm_gauss_lti1: Demo about creating and manimulating a linear 9 | % time invariant noise model. 10 | % - demo_nm_gauss_ltv1: Demo about creating and manimulating a linear 11 | % time variant noise model. 12 | % - demo_nm_gauss_handle1: Demo about creating and manimulating a 13 | % function handle noise model. 14 | % 15 | % demos_ss_ 16 | % 17 | % demos_ss_D 18 | % - demo_ss_DL1: Demo about creating and manimulating a linear 19 | % time variant state space model. 20 | % - demo_ss_DNL_AN1: Demo about creating and manimulating a nonlinear 21 | % state space model with additive noise. 22 | % - demo_ss_DNL1: Demo about creating and manimulating a nonlinear 23 | % state space model. 24 | % 25 | % demos_sim 26 | % 27 | % demos_sim 28 | % - demo_sim1: Demo about using the simulation method to create 29 | % simulation objects. 30 | % 31 | % demos_sim_D 32 | % - demo_sim_D1: Demo about using the methods of the simulation object. 33 | % 34 | % demos_da 35 | % 36 | % - demo_KF1: Demo about applying the Kalman filter on a vehicle 37 | % navigation model. 38 | % - demo_L3: Demo about applying several Ensemble filters on the 39 | % Lorenz equations. 40 | % - demo_VDP: Demo about applying the EKF, UKF and EnKF filters on 41 | % the Van Der Pol oscillator. 42 | % - demo_TRACK: Demo about applying the EnKF, the UKF and the PF_ASIR 43 | % filters on a tracking problem with linear state equations and 44 | % highly nonlinear measurements. 45 | % - demo_NLSC: Demo about applying the EKF, UKF, EnKF and PF_ASIR on 46 | % a highly nonlinear scalar model. 47 | % 48 | 49 | clc; 50 | 51 | disp('To run the demos, enter the name of the demo in the command line.'); 52 | disp('(e.g. demo_nm_gauss_lti1)'); 53 | disp('The following demos are currently present in the toolbox:'); 54 | disp(' '); 55 | disp(' Demos about noise models:'); 56 | disp(' '); 57 | disp(' The Gaussian noise models'); 58 | disp(' -------------------------'); 59 | disp(' - demo_nm_gauss_lti1 -'); 60 | disp(' Demo about creating and manimulating a linear time invariant'); 61 | disp(' noise model.'); 62 | disp(' '); 63 | disp(' - demo_nm_gauss_ltv1 -'); 64 | disp(' Demo about creating and manimulating a linear time variant'); 65 | disp(' noise model.'); 66 | disp(' '); 67 | disp(' - demo_nm_gauss_handle1 -'); 68 | disp(' Demo about creating and manimulating a function handle noise'); 69 | disp(' model.'); 70 | disp(' '); 71 | disp('press key'); pause 72 | 73 | clc; 74 | disp('To run the demos, enter the name of the demo in the command line.'); 75 | disp('(e.g. demo_ss_DL1)'); 76 | disp('The following demos are currently present in the toolbox:'); 77 | disp(' '); 78 | disp(' Demos about state space models:'); 79 | disp(' '); 80 | disp(' The discrete-time state space models'); 81 | disp(' ------------------------------------'); 82 | disp(' - demo_ss_DL1 -'); 83 | disp(' Demo about creating and manimulating a linear time variant'); 84 | disp(' state space model.'); 85 | disp(' '); 86 | disp(' - demo_ss_DNL_AN1 -'); 87 | disp(' Demo about creating and manimulating a nonlinear state space'); 88 | disp(' model with additive noise.'); 89 | disp(' '); 90 | disp(' - demo_ss_DNL1 -'); 91 | disp(' Demo about creating and manimulating a nonlinear state space'); 92 | disp(' model.'); 93 | disp(' '); 94 | disp('press key'); pause 95 | 96 | clc; 97 | disp('To run the demos, enter the name of the demo in the command line.'); 98 | disp('(e.g. demo_ss_DL1)'); 99 | disp('The following demos are currently present in the toolbox:'); 100 | disp(' '); 101 | disp(' Demos about simulation models:'); 102 | disp(' '); 103 | disp(' The discrete-time simulation method'); 104 | disp(' ------------------------------------'); 105 | disp(' - demo_sim1 -'); 106 | disp(' Demo about using the simulation method to create simulation'); 107 | disp(' objects.'); 108 | disp(' '); 109 | disp(' The discrete-time simulation model'); 110 | disp(' ------------------------------------'); 111 | disp(' - demo_sim_D1 -'); 112 | disp(' Demo about using the methods of the simulation object.'); 113 | disp(' '); 114 | disp('press key'); pause 115 | 116 | clc; 117 | disp('To run the demos, enter the name of the demo in the command line.'); 118 | disp('(e.g. demo_ss_DL1)'); 119 | disp('The following demos are currently present in the toolbox:'); 120 | disp(' '); 121 | disp(' Demos about data assimilation methods and models:'); 122 | disp(' '); 123 | disp(' The discrete-time data assimilation methods'); 124 | disp(' -------------------------------------------'); 125 | disp(' - demo_KF1 -'); 126 | disp(' Demo about applying the Kalman filter on a vehicle navigation'); 127 | disp(' model.'); 128 | disp(' '); 129 | disp(' - demo_L3 -'); 130 | disp(' Demo about applying several Ensemble filters on the Lorenz '); 131 | disp(' equations.'); 132 | disp(' '); 133 | disp(' - demo_VDP -'); 134 | disp(' Demo about applying the EKF, UKF and EnKF filters on the Van Der'); 135 | disp(' Pol oscillator.'); 136 | disp(' '); 137 | disp(' - demo_TRACK -'); 138 | disp(' Demo about applying the EnKF, the UKF and the PF_ASIR filters '); 139 | disp(' on a tracking problem with a linear state equation and a highly'); 140 | disp(' nonlinear measurement equation.'); 141 | disp(' '); 142 | disp(' - demo_NLSC -'); 143 | disp(' Demo about applying the EKF, UKF, EnKF and PF_ASIR on a highly'); 144 | disp(' nonlinear scalar model.'); 145 | disp(' '); 146 | 147 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/data/contains sample data.txt: -------------------------------------------------------------------------------- 1 | 0 -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_KF1.m: -------------------------------------------------------------------------------- 1 | % Demo about using the KF da technique 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to apply the Kalman filter KF on a discrete-time '); 6 | disp('state space model to obtain estimated states.'); 7 | disp('The example considers a vehicle accelaration and is based on the paper:'); 8 | disp(' ''Kalman filtering'' from Dan Simon in Embedded Systems Programming.'); 9 | disp(' '); 10 | disp('First a ss_DL state space model is created using the following steps:'); 11 | disp(' '); 12 | disp('1) Create the noise models '); 13 | disp('>> Ts=0.1;'); 14 | disp('>> mu_x0=[0 0]'';sigma_x0=10^2 * [Ts^4/4 Ts^3/2; Ts^3/2 Ts^2+0.001];'); 15 | disp('>> mu_w =[0 0]'';sigma_w= 10^2 * [Ts^4/4 Ts^3/2; Ts^3/2 Ts^2+0.001];'); 16 | disp('>> mu_v=0;sigma_v=0.2^2;'); 17 | disp('>> x0={mu_x0,sigma_x0};w={mu_w,sigma_w};v={mu_v,sigma_v};'); 18 | disp(' '); 19 | disp('2) Set the other parameters '); 20 | disp('>> k0 = 0; samples=600; TimeUnit=''seconds'';'); 21 | disp(' '); 22 | disp('3) Set the system matrices and input '); 23 | disp('>> A = [1 Ts; 0 1]; '); 24 | disp('>> B = [Ts^2/2; Ts]; '); 25 | disp('>> C = [1 0]; D = 0;'); 26 | disp('>> u = ones(1,samples);'); 27 | disp(' '); 28 | disp('4) Create the state space model '); 29 | disp('ssObj=ss_DL(A,B,C,D,x0,w,v,k0,Ts,TimeUnit);'); 30 | disp(' '); 31 | disp('Note that all variables are set explicitly, to make it easier to'); 32 | disp('follow what is happening. Shorter notations are of course possible.'); 33 | Ts=0.1; 34 | mu_x0=[0 0]';sigma_x0=0.2^2 * [Ts^4/4 Ts^3/2; Ts^3/2 Ts^2+0.001]; 35 | mu_w =[0 0]';sigma_w= 0.2^2 * [Ts^4/4 Ts^3/2; Ts^3/2 Ts^2+0.001]; 36 | mu_v=0;sigma_v=10^2; 37 | x0={mu_x0,sigma_x0}; 38 | w={mu_w,sigma_w}; 39 | v={mu_v,sigma_v}; 40 | k0 = 0; 41 | samples=600; 42 | TimeUnit='seconds'; 43 | A = [1 Ts; 0 1]; 44 | B = [Ts^2/2; Ts]; 45 | C = [1 0]; 46 | D = 0; 47 | u = ones(1,samples); 48 | ssObj=ss_DL(A,B,C,D,x0,w,v,k0,Ts,TimeUnit); 49 | disp(' '); 50 | disp('press key'); pause 51 | 52 | clc; 53 | disp('Now that the state-space model is ready, we can simulate its states'); 54 | disp('and measurements.'); 55 | disp(' '); 56 | disp('>> simObj=sim(ssObj,samples,u);'); 57 | simObj=sim(ssObj,samples,u); 58 | disp(' '); 59 | disp('Now that virtual measuremtents are available we can apply the'); 60 | disp('da method to estimate the states with KF. Note that the true states'); 61 | disp('and the measurements are saved for later usage.'); 62 | disp(' '); 63 | disp('>> damObj=da(ssObj,''KF'',simObj,''x y'');'); 64 | damObj=da(ssObj,'KF',simObj,'x y'); 65 | disp(' '); 66 | disp('Lets see how the results look like. We can easily make a plot of the'); 67 | disp('analysis states by using the plot command:'); 68 | disp(' '); 69 | disp('>> plot(damObj,''xa'',(1:2));'); 70 | plot(damObj,'xa',(1:2)); 71 | disp(' '); 72 | disp('press key'); pause 73 | 74 | clc;close all; 75 | disp('Another possible plot is to compare the true states, analysis states'); 76 | disp('and measurements in one plot. To this end, the required numbers of'); 77 | disp('measurement values needs to be added since they can differ from the'); 78 | disp('state numbers. In this case only the first state is measured so the'); 79 | disp('syntax becomes:'); 80 | disp(' '); 81 | disp('>> plot(damObj,''xaxy'',1,1);'); 82 | plot(damObj,'xaxy',1,1); 83 | 84 | disp(' '); 85 | disp('This concludes this demo. '); 86 | disp('press key'); pause 87 | close all; -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3.m: -------------------------------------------------------------------------------- 1 | % Demo about applying several da techniques on Lorenz equation 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to apply several data assimilation techniques '); 6 | disp('on the three states Lorenz eqution (L3) to obtain estimated states.'); 7 | disp('For more information on the VDP equations see the accompanying thesis.'); 8 | disp(' '); 9 | disp('First the ss_DNL_AN state space model is created using the following steps:'); 10 | disp(' '); 11 | disp('1) Create the noise models '); 12 | disp('>> x0={diag([1 1 1])};w={diag([16 16 16])};v={diag([4 4 4])};'); 13 | disp(' '); 14 | disp('2) Set the other parameters '); 15 | disp('>> k0 = 0; Ts=0.01;TimeUnit=''seconds'';'); 16 | disp(' '); 17 | disp('3) Create the state space model '); 18 | disp('ssObj=ss_DNL_AN(@demo_L3_f,@demo_L3_h,x0,w,v,k0,Ts,TimeUnit,...'); 19 | disp(' @demo_L3_fjacx,@demo_L3_hjacx);'); 20 | disp(' '); 21 | disp('Note that all variables are set explicitly, to make it easier to'); 22 | disp('follow what is happening. Shorter notations are of course possible.'); 23 | mySeed = 10; 24 | rng(mySeed); % Set the seed 25 | 26 | %x0={diag([1 1 1])}; 27 | x0=nm_gauss_lti([-5.8;-5.7;20.5],diag([1 1 1])); 28 | 29 | w={diag([1e-6 1e-6 1e-6])};v={diag([2])}; 30 | k0 = 0; Ts=0.01;TimeUnit='seconds'; 31 | ssObj=ss_DNL_AN(@demo_L3_f,@demo_L3_h,x0,w,v,k0,Ts,TimeUnit,@demo_L3_fjacx,@demo_L3_hjacx); 32 | disp(' '); 33 | disp('press key'); pause 34 | 35 | clc; 36 | disp('Now that the L3 model is ready, we can simulate its states'); 37 | disp('and measurements and save both of them in the sim. object.'); 38 | disp(' '); 39 | disp('>> x0init=[3;-3;12];samples=300; conf=''x y'';u=[];'); 40 | disp('>> simObj=sim(ssObj,samples,u,conf,x0init);'); 41 | %x0init=[3;-3;12];samples=300; conf='x y';u=[]; 42 | x0init=[-6;-6;20];samples=600; conf='x y';u=[]; 43 | 44 | simObj=sim(ssObj,samples,u,conf,x0init); 45 | disp(' '); 46 | disp('We can always quickly check if everything went as expected by making'); 47 | disp('a quick plot of the measurements and true states. Lets plot the states'); 48 | disp('together with the measurements using the time scale for the x-axis:'); 49 | disp(' '); 50 | disp('>> plot(simObj,''xy t'',(1:3),(1:3));'); 51 | %plot(simObj,'xy t',(1:3),(1:3)); 52 | plot(simObj,'x t',(1:3)); 53 | plot(simObj,'y t',(1)); 54 | 55 | disp(' '); 56 | disp('press key'); pause 57 | 58 | clc;close all; 59 | disp('Now that virtual measuremtents are available we can apply the'); 60 | disp('da method to estimate the states with DEnKF. Note that x, Pa and y'); 61 | disp('are saved for later usage.'); 62 | disp(' '); 63 | disp('>> damDEnKF=da(ssObj,''DEnKF'',simObj,''x y Pa'');'); 64 | damDEnKF=da(ssObj,'DEnKF',simObj,'x y Pa'); 65 | disp(' '); 66 | disp('Lets see how the results look like. We can easily make a plot of the'); 67 | disp('true states and the analysis states by using the plot command:'); 68 | disp(' '); 69 | disp('>> plot(damDEnKF,''xax'',(1:3));'); 70 | plot(damDEnKF,'xax',(1:3)); 71 | disp(' '); 72 | disp(' '); 73 | disp('press key'); pause 74 | 75 | clc;close all; 76 | disp('This looked almost perfect. To have a cleare picture of the errors,'); 77 | disp('it might be better to see the errors between the true states and'); 78 | disp('the analysis states as follows:'); 79 | disp(' '); 80 | disp('>> plot(damDEnKF,''xa-x t'',(1:3));'); 81 | plot(damDEnKF,'xa-x t',(1:3)); 82 | disp(' '); 83 | disp('Next, to compare the performance of different Ensemble filters'); 84 | disp('using the L3 model, the RMSE will be used.'); 85 | disp(' '); 86 | disp('press key'); pause 87 | 88 | clc;close all; 89 | disp('Lets apply the EnKF, the ETKF and the DEnKF '); 90 | disp('with 10 ensemble members, each 3 times on the L3 model to attain'); 91 | disp('the average RMSE value as follows:'); 92 | disp(' '); 93 | disp('its=3;rmseArray=zeros(3,3); '); 94 | disp('for i=1:its '); 95 | disp(' damEnKF=da(ssObj,''EnKF'',simObj,''x y'',{10}); '); 96 | disp(' damETKF=da(ssObj,''ETKF'',simObj,''x y'',{10}); '); 97 | disp(' damDEnKF=da(ssObj,''DEnKF'',simObj,''x y'',{10}); '); 98 | disp(' '); 99 | disp(' rmseArray(:,1)=rmseArray(:,1)+rmse(damEnKF,(1:3));'); 100 | disp(' rmseArray(:,2)=rmseArray(:,2)+rmse(damETKF,(1:3));'); 101 | disp(' rmseArray(:,3)=rmseArray(:,3)+rmse(damDEnKF,(1:3));'); 102 | disp('end '); 103 | disp('rmseArray=rmseArray./its;rmseArray'); 104 | disp(' '); 105 | disp('press key to start iterations. This will take approx. 1 minute'); 106 | disp('since the ODE45 solver is slowing down the progress.'); pause 107 | 108 | clc;close all; 109 | disp('Please wait...'); 110 | its=3;rmseArray=zeros(3,3); 111 | for i=1:its 112 | 113 | damEnKF=da(ssObj,'EnKF',simObj,'x y',{10}); 114 | damETKF=da(ssObj,'ETKF',simObj,'x y',{10}); 115 | damDEnKF=da(ssObj,'DEnKF',simObj,'x y',{10}); 116 | rmseArray(:,1)=rmseArray(:,1)+rmse(damEnKF,(1:3)); 117 | rmseArray(:,2)=rmseArray(:,2)+rmse(damETKF,(1:3)); 118 | rmseArray(:,3)=rmseArray(:,3)+rmse(damDEnKF,(1:3)); 119 | disp('iteration:');i 120 | end 121 | rmseArray=rmseArray./its; 122 | rmseArray 123 | disp(' EnKF, ETKF, DEnKF'); 124 | disp(' '); 125 | disp('As you can see the all algorithms provide very similar estimations'); 126 | disp('in this case. '); 127 | disp(' '); 128 | disp('This concludes this demo. '); 129 | disp('press key'); pause 130 | close all; -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_L3_DNL_ANf(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_F function handle template for f for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: process noise w (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_AN_F(X,K,U,~,TS) returns the new state X1 without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | %[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) 20 | [TOUT,x1] = ode45(@demo_L3f,[k*Ts (k+1)*Ts],x); 21 | x1=x1(end,:)'; 22 | 23 | end 24 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3_fjacx.m: -------------------------------------------------------------------------------- 1 | function fJacX = demo_fbodyfJacX(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_FJACX function handle template for the Jacobian of f 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: process noise w (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % FJACX = TEMPLATE_DNL_AN_FJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | sigma=10; 20 | ro=28; 21 | B=8/3; 22 | 23 | fJacX = [-sigma 0 0 24 | ro-x(3) -1 x(1) 25 | x(2) x(1) -B]; 26 | 27 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_L3_DNL_ANh(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | C = [1 0 0 20 | 0 1 0 21 | 0 0 1]; 22 | C=[1 0 0]; 23 | 24 | 25 | y=C*x; 26 | 27 | end 28 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3_hjacx.m: -------------------------------------------------------------------------------- 1 | function hJacX = demo_L3_DNL_ANhJacX(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_HJACX function handle template for the Jacobian of h 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: meas. noise v (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % HJACX = TEMPLATE_DNL_AN_HJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | 20 | hJacX = [1 0 0 21 | 0 1 0 22 | 0 0 1]; 23 | 24 | 25 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_L3f.m: -------------------------------------------------------------------------------- 1 | function xdot = demo_L3f(t,x) 2 | sigma=10; 3 | ro=28; 4 | B=8/3; 5 | 6 | xdot(1,1) = -sigma*(x(1) - x(2)); 7 | xdot(2,1) = x(1)*(ro - x(3)) - x(2); 8 | xdot(3,1) = x(1)*x(2) - B*x(3); 9 | 10 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_NLSC.m: -------------------------------------------------------------------------------- 1 | % Demo about applying several da techniques on a highly nonlinear scalar 2 | % system 3 | 4 | clc; 5 | 6 | disp('This is a demo of how to apply several data assimilation techniques '); 7 | disp('on a highly nonlinear set of scalar state and measurent eqations'); 8 | disp('to obtain estimated states. Although the equations are scalar,'); 9 | disp('they are considered as a benchmark for nonlinear filtering due to'); 10 | disp('there high nonlinearity.'); 11 | disp('For more information about this model see the accompanying thesis.'); 12 | disp(' '); 13 | disp('First the ss_DNL_AN state space model is created using the following steps:'); 14 | disp(' '); 15 | disp('1) Create the noise models '); 16 | disp('>> x0={0.1,2};w={0,1};v={0,1};'); 17 | disp(' '); 18 | disp('2) Set the other parameters '); 19 | disp('>> k0 = 0; Ts=1;TimeUnit=''seconds'';'); 20 | disp(' '); 21 | disp('3) Create the state space model '); 22 | disp('ssObj=ss_DNL_AN(@demo_NLSC_f,@demo_NLSC_h,x0,w,v,k0,Ts,TimeUnit,...'); 23 | disp(' @demo_NLSC_fjacx,@demo_NLSC_hjacx);'); 24 | disp(' '); 25 | disp('Note that all variables are set explicitly, to make it easier to'); 26 | disp('follow what is happening. Shorter notations are of course possible.'); 27 | mySeed = 10; 28 | rng(mySeed); % Set the seed 29 | x0={0.1,2};w={0,1};v={0,1}; 30 | k0 = 0; Ts=1;TimeUnit='seconds'; 31 | ssObj=ss_DNL_AN(@demo_NLSC_f,@demo_NLSC_h,x0,w,v,k0,Ts,TimeUnit,@demo_NLSC_fjacx,@demo_NLSC_hjacx); 32 | disp(' '); 33 | disp('press key'); pause 34 | 35 | clc; 36 | disp('Now that the L3 model is ready, we can simulate its states'); 37 | disp('and measurements and save both of them in the sim. object.'); 38 | disp(' '); 39 | disp('>> x0init=0.1;samples=50; conf=''x y'';u=[];'); 40 | disp('>> simObj=sim(ssObj,samples,u,conf,x0init);'); 41 | x0init=0.1;samples=50; conf='x y';u=[]; 42 | simObj=sim(ssObj,samples,u,conf,x0init); 43 | disp(' '); 44 | disp('We can always quickly check if everything went as expected by making'); 45 | disp('a quick plot of the measurements and true states. Lets plot the state'); 46 | disp('together with the measurement:'); 47 | disp(' '); 48 | disp('>> plot(simObj,''xy t'',1,1);'); 49 | plot(simObj,'xy t',1,1); 50 | disp(' '); 51 | disp('The highly nonlinear behaviour of both state and measurement are apparent.'); 52 | disp(' '); 53 | disp('press key'); pause 54 | 55 | clc;close all; 56 | disp('Now that virtual measuremtents are available we can apply the'); 57 | disp('da method to estimate the states with EKF. Note that x, Pa and y'); 58 | disp('are saved for later usage.'); 59 | disp(' '); 60 | disp('>> damEKF=da(ssObj,''EKF'',simObj,''x y Pa'');'); 61 | damEKF=da(ssObj,'EKF',simObj,'x y Pa'); 62 | disp(' '); 63 | disp('Lets see how the results look like. We can easily make a plot of the'); 64 | disp('true states and the analysis states with their 95% confidence intervals'); 65 | disp('by using the plot command as follows:'); 66 | disp(' '); 67 | disp('>> plot(damEKF,''xaxPa'',1);'); 68 | plot(damEKF,'xaxPa',1); 69 | disp(' '); 70 | disp('Notice how the estimation fails and in addition the true states are '); 71 | disp('even outside of the confidence intervals at several steps.'); 72 | disp('This is an expected behaviour for the EKF when applied on this model.'); 73 | disp('Now lets do the same for the EnKF.'); 74 | disp(' '); 75 | disp('press key'); pause 76 | 77 | clc;close all; 78 | disp('For the EnKF, with ensemble size 50, the command is:'); 79 | disp(' '); 80 | disp('>> damEnKF=da(ssObj,''EnKF'',simObj,''x y Pa'',{50});'); 81 | damEnKF=da(ssObj,'EnKF',simObj,'x y Pa',{50}); 82 | disp(' '); 83 | disp('Lets look at the result.'); 84 | disp(' '); 85 | disp('>> plot(damEnKF,''xaxPa'',1);'); 86 | plot(damEnKF,'xaxPa',1); 87 | disp(' '); 88 | disp('Notice how the estimation has improved a lot and that now the confidence'); 89 | disp('intervals are correct.'); 90 | disp('Lets see if this is also the case for the PF_ASIR.'); 91 | disp(' '); 92 | disp('press key'); pause 93 | 94 | clc;close all; 95 | disp('For the PF_ASIR, with 50 particles, the command is:'); 96 | disp(' '); 97 | disp('>> damASIR=da(ssObj,''PF_ASIR'',simObj,''x y Pa'',{50});'); 98 | damASIR=da(ssObj,'PF_ASIR',simObj,'x y Pa',{50}); 99 | disp(' '); 100 | disp('Lets look at the result.'); 101 | disp(' '); 102 | disp('>> plot(damASIR,''xaxPa'',1);'); 103 | plot(damASIR,'xaxPa',1); 104 | disp(' '); 105 | disp('The result looks quite similar as the one of the EnKF, and again the'); 106 | disp('confidence intervals are correct.'); 107 | 108 | disp(' '); 109 | disp('press key'); pause 110 | 111 | clc;close all; 112 | disp('Now, lets check the RMSE values of the three algorithms plus the UKF. '); 113 | disp('To obtain a representation of the result, the RMSE experiment is '); 114 | disp('repeated 10 times and only the average result is indicated.'); 115 | disp(' '); 116 | disp('its=10;rmseArray=zeros(1,4); '); 117 | disp('for i=1:its '); 118 | disp(' damEKF=da(ssObj,''EKF'',simObj,''x y''); '); 119 | disp(' damEnKF=da(ssObj,''EnKF'',simObj,''x y'',{50}); '); 120 | disp(' damPFASIR=da(ssObj,''PF_ASIR'',simObj,''x y'',{50}); '); 121 | disp(' damUKF=da(ssObj,''UKF'',simObj,''x y''); '); 122 | disp(' '); 123 | disp(' rmseArray(:,1)=rmseArray(:,1)+rmse(damEKF,1);'); 124 | disp(' rmseArray(:,2)=rmseArray(:,2)+rmse(damEnKF,1);'); 125 | disp(' rmseArray(:,3)=rmseArray(:,3)+rmse(damPFASIR,1);'); 126 | disp(' rmseArray(:,4)=rmseArray(:,4)+rmse(damUKF,1);'); 127 | disp('end '); 128 | disp('rmseArray=rmseArray./its;rmseArray'); 129 | disp(' '); 130 | disp('press key to start iterations.'); pause 131 | 132 | clc;close all; 133 | disp('Please wait...'); 134 | its=10;rmseArray=zeros(1,4); 135 | for i=1:its 136 | 137 | damEKF=da(ssObj,'EKF',simObj,'x y'); 138 | damEnKF=da(ssObj,'EnKF',simObj,'x y',{50}); 139 | damPFASIR=da(ssObj,'PF_ASIR',simObj,'x y',{50}); 140 | damUKF=da(ssObj,'UKF',simObj,'x y'); 141 | rmseArray(:,1)=rmseArray(:,1)+rmse(damEKF,1); 142 | rmseArray(:,2)=rmseArray(:,2)+rmse(damEnKF,1); 143 | rmseArray(:,3)=rmseArray(:,3)+rmse(damPFASIR,1); 144 | rmseArray(:,4)=rmseArray(:,4)+rmse(damUKF,1); 145 | disp('iteration:');i 146 | end 147 | rmseArray=rmseArray./its; 148 | rmseArray 149 | disp(' EKF EnKF PF_ASIR UKF'); 150 | disp(' '); 151 | disp('As expected, the EKF has the bad performance. The UKF has the worst'); 152 | disp('performance in this cas. Still, remind that it only uses 3 sigma points.'); 153 | disp('The particle filter provides the best performance in this case. '); 154 | disp(' '); 155 | disp('This concludes this demo. '); 156 | close all; -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_NLSC_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_NLSC_f(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_F function handle template for f for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: process noise w (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_AN_F(X,K,U,~,TS) returns the new state X1 without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | x1=0.5*x + 25*x/(1+x^2) + 8*cos(1.2*(k-1)); 20 | 21 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_NLSC_fjacx.m: -------------------------------------------------------------------------------- 1 | function Jac = demo_NLSC_fjacx(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_FJACX function handle template for the Jacobian of f 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: process noise w (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % FJACX = TEMPLATE_DNL_AN_FJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | Jac = 0.5 + 25* (1-x^2)/(1+x^2)^2; 20 | 21 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_NLSC_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_NLSC_h(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | y= x^2/20; 19 | 20 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_NLSC_hjacx.m: -------------------------------------------------------------------------------- 1 | function Jac = demo_NLSC_hjacx(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_HJACX function handle template for the Jacobian of h 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: meas. noise v (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % HJACX = TEMPLATE_DNL_AN_HJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | Jac = x/10; 20 | 21 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_TRACK.m: -------------------------------------------------------------------------------- 1 | % Demo about applying several da techniques on a tracking problem 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to apply several data assimilation techniques '); 6 | disp('on the four states tracking problem to obtain estimated states.'); 7 | disp('The tracking problem is interesting for its highly nonlinear measurements.'); 8 | disp('In fact two variants are taken under consideration: one measurement'); 9 | disp('with a radar which yields distance and angle and one using triangulation'); 10 | disp('which provides two different distances. The four states are respectively'); 11 | disp('the x position, the y position and the velocities vx and vy. The state'); 12 | disp('equation for this model is identical for both radar and triangulation.'); 13 | disp('It is important to note that the state equation is linear.'); 14 | disp('For more information on the equations see the accompanying thesis.'); 15 | disp(' '); 16 | disp('First the ss_DNL_AN state space model is created using the following steps:'); 17 | disp(' '); 18 | disp('1) Create the noise models '); 19 | disp('>> x0={[-200;200;4;0]};'); 20 | disp('>> w={diag([0.0000001;0.0000001;0.5;0.5])};'); 21 | disp('>> vR={diag([200;0.003])};vT={diag([200;200])};'); 22 | disp(' '); 23 | disp('2) Set the other parameters '); 24 | disp('>> k0 = 0; Ts=1;TimeUnit=''seconds'';'); 25 | disp(' '); 26 | disp('3) Create the state space model with radar measurement'); 27 | disp('ssObjR=ss_DNL_AN(@demo_TRACK_f,@demo_TRACK_R_h,x0,w,vR,k0,Ts,TimeUnit'); 28 | disp('4) Create the state space model with triangulation measurement'); 29 | disp('ssObjT=ss_DNL_AN(@demo_TRACK_f,@demo_TRACK_T_h,x0,w,vT,k0,Ts,TimeUnit'); 30 | disp(' '); 31 | disp('Note that all variables are set explicitly, to make it easier to'); 32 | disp('follow what is happening. Shorter notations are of course possible.'); 33 | mySeed = 10; 34 | rng(mySeed); % Set the seed 35 | x0={[-200;200;4;0]};w={diag([0.0000001;0.0000001;0.5;0.5])}; 36 | vR={diag([200;0.003])};vT={diag([200;200])}; 37 | k0 = 0; Ts=0.01;TimeUnit='seconds'; 38 | ssObjR=ss_DNL_AN(@demo_TRACK_f,@demo_TRACK_R_h,x0,w,vR,k0,Ts,TimeUnit); 39 | ssObjT=ss_DNL_AN(@demo_TRACK_f,@demo_TRACK_T_h,x0,w,vT,k0,Ts,TimeUnit); 40 | disp(' '); 41 | disp('press key'); pause 42 | 43 | clc; 44 | disp('Now that the tracking models are ready, we can simulate their states'); 45 | disp('and measurements and save both of them in the sim. object.'); 46 | disp(' '); 47 | disp('>> x0init=[-200;200;4;0];samples=50; conf=''x y'';u=[];'); 48 | disp('>> simObjR=sim(ssObjR,samples,u,conf,x0init);'); 49 | disp('>> simObjT=sim(ssObjT,samples,u,conf,x0init);'); 50 | x0init=[-200;200;4;0];samples=50; conf='x y';u=[]; 51 | simObjR=sim(ssObjR,samples,u,conf,x0init); 52 | simObjT=sim(ssObjT,samples,u,conf,x0init); 53 | disp(' '); 54 | disp('We can check if everything went as expected by making a quick'); 55 | disp('plot of the true states of either one of the models:'); 56 | disp(' '); 57 | disp('>> plot(simObjR,''x t'',(1:4));'); 58 | plot(simObjR,'x t',(1:4)); 59 | disp(' '); 60 | disp('press key'); pause 61 | 62 | clc;close all; 63 | disp('Now that virtual measuremtents are available we can apply the'); 64 | disp('da method to estimate the states with the UKF. Note that x and y'); 65 | disp('are saved for later usage. So, for the radar model:'); 66 | disp(' '); 67 | disp('>> damRUKF=da(ssObjR,''UKF'',simObjR,''x y'');'); 68 | damRUKF=da(ssObjR,'UKF',simObjR,'x y'); 69 | disp(' '); 70 | disp('Lets see how the results look like. We can easily make a plot of the'); 71 | disp('true states and the analysis states by using the plot command:'); 72 | disp(' '); 73 | disp('>> plot(damRUKF,''xax'',(1:4));'); 74 | plot(damRUKF,'xax',(1:4)); 75 | disp(' '); 76 | disp('It looks like it is difficult to get good estimations. Lets check how'); 77 | disp('the estimation is for the triangulation model.'); 78 | disp(' '); 79 | disp('press key'); pause 80 | 81 | clc;close all; 82 | disp('Now that virtual measuremtents are available we can apply the'); 83 | disp('da method to estimate the states with the UKF. Note that x and y'); 84 | disp('are saved for later usage. So, for the radar model:'); 85 | disp(' '); 86 | disp('>> damTUKF=da(ssObjT,''UKF'',simObjT,''x y'');'); 87 | damTUKF=da(ssObjT,'UKF',simObjT,'x y'); 88 | disp(' '); 89 | disp('Lets see how the results look like. We can easily make a plot of the'); 90 | disp('true states and the analysis states by using the plot command:'); 91 | disp(' '); 92 | disp('>> plot(damTUKF,''xax'',(1:4));'); 93 | plot(damTUKF,'xax',(1:4)); 94 | disp(' '); 95 | disp('Still not perfect, but it seems better. Lets now check the accuracy of'); 96 | disp('other possible filters using the RMSE.'); 97 | disp(' '); 98 | disp('press key'); pause 99 | %% 100 | clc;close all; 101 | disp('The following filters are compared for both the radar as the triangulation'); 102 | disp('model: UKF, EnKF with 50 ensemble members and PF_ASIR with 200 particles.'); 103 | disp(' '); 104 | disp('Lets start with the radar problem. The average RMSE value of three '); 105 | disp('experiments is checked:'); 106 | disp(' '); 107 | disp('its=3;rmseArray=zeros(3,3); '); 108 | disp('for i=1:its '); 109 | disp(' damUKF=da(ssObjR,''UKF'',simObjR,''x y''); '); 110 | disp(' damEnKF=da(ssObjR,''EnKF'',simObjR,''x y'',{50}); '); 111 | disp(' damPFASIR=da(ssObjR,''PF_ASIR'',simObjR,''x y'',{200}); '); 112 | disp(' '); 113 | disp(' rmseArray(:,1)=rmseArray(:,1)+rmse(damUKF,(1:3));'); 114 | disp(' rmseArray(:,2)=rmseArray(:,2)+rmse(damEnKF,(1:3));'); 115 | disp(' rmseArray(:,3)=rmseArray(:,3)+rmse(damPFASIR,(1:3));'); 116 | disp('end '); 117 | disp('rmseArray=rmseArray./its;rmseArray'); 118 | disp(' '); 119 | disp('press key to start iterations.');pause 120 | 121 | clc;close all; 122 | disp('Please wait...'); 123 | its=3;rmseArray=zeros(3,3); 124 | for i=1:its 125 | 126 | damUKF=da(ssObjR,'UKF',simObjR,'x y'); 127 | damEnKF=da(ssObjR,'EnKF',simObjR,'x y',{50}); 128 | damPFASIR=da(ssObjR,'PF_ASIR',simObjR,'x y',{200}); 129 | rmseArray(:,1)=rmseArray(:,1)+rmse(damUKF,(1:3)); 130 | rmseArray(:,2)=rmseArray(:,2)+rmse(damEnKF,(1:3)); 131 | rmseArray(:,3)=rmseArray(:,3)+rmse(damPFASIR,(1:3)); 132 | disp('iteration:');i 133 | end 134 | rmseArray=rmseArray./its; 135 | rmseArray 136 | disp(' UKF, EnKF, PF_ASIR'); 137 | disp(' '); 138 | disp('As you can see the UKF algorithm provides the best estimations with,'); 139 | disp('only 9 sigma points which are selected deterministicly. The EnKF'); 140 | disp('performs slightly less good but needs 50 ensemble members. The '); 141 | disp('particle filter clearly performs the worst, even with 200 Particles.'); 142 | disp('Note however, that the UKF is not suited for large-scale problems.'); 143 | disp(' '); 144 | disp('Lets try the same setup for the traingulation model. '); 145 | disp(' '); 146 | disp('press key to start iterations.');pause 147 | 148 | clc;close all; 149 | disp('Please wait...'); 150 | its=3;rmseArray=zeros(3,3); 151 | for i=1:its 152 | 153 | damUKF=da(ssObjT,'UKF',simObjT,'x y'); 154 | damEnKF=da(ssObjT,'EnKF',simObjT,'x y',{50}); 155 | damPFASIR=da(ssObjT,'PF_ASIR',simObjT,'x y',{200}); 156 | rmseArray(:,1)=rmseArray(:,1)+rmse(damUKF,(1:3)); 157 | rmseArray(:,2)=rmseArray(:,2)+rmse(damEnKF,(1:3)); 158 | rmseArray(:,3)=rmseArray(:,3)+rmse(damPFASIR,(1:3)); 159 | disp('iteration:');i 160 | end 161 | rmseArray=rmseArray./its; 162 | rmseArray 163 | disp(' UKF, EnKF, PF_ASIR'); 164 | disp(' '); 165 | disp('For this model, the estimations of the algorithms are similar.'); 166 | disp(' '); 167 | disp('This concludes this demo. '); 168 | disp('press key'); pause 169 | close all; -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_TRACK_R_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_TRACK_R_h(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | y=zeros(2,1); 19 | y(1) = sqrt( x(1)^2 + x(2)^2); 20 | y(2) = atan(x(2)/x(1))*360/2*pi; 21 | 22 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_TRACK_T_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_TRACK_T_h(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | y=zeros(2,1); 19 | p1x= -300; p1y= 0; 20 | p2x= 300; p2y= 0; 21 | y(1) = sqrt((x(1)-p1x)^2+(x(2)-p1y)^2); 22 | y(2) = sqrt((x(1)-p2x)^2+(x(2)-p2y)^2); 23 | 24 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_TRACK_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_TRACK_f(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_F function handle template for f for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: process noise w (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_AN_F(X,K,U,~,TS) returns the new state X1 without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | A=[1 0 1 0 20 | 0 1 0 1 21 | 0 0 1 0 22 | 0 0 0 1]; 23 | 24 | x1=A*x; 25 | 26 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_VDP.m: -------------------------------------------------------------------------------- 1 | % Demo about applying several da techniques on Van der Pol Oscillator 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to apply several data assimilation techniques '); 6 | disp('on the Van Der Pol Oscillator (VDP) to obtain estimated states.'); 7 | disp('For more information on the VDP equations see the accompanying thesis.'); 8 | disp(' '); 9 | disp('First the ss_DNL_AN state space model is created using the following steps:'); 10 | disp(' '); 11 | disp('1) Create the noise models '); 12 | disp('>> mu_x0=[0 5]'';sigma_x0=diag([5, 5]);'); 13 | disp('>> mu_w=[0 0]'';sigma_w=diag([10e-3 10e-3]);'); 14 | disp('>> mu_v=[0 0]'';sigma_v=diag([10e-3 10e-3]);'); 15 | disp('>> x0={mu_x0,sigma_x0};w={mu_w,sigma_w};v={mu_v,sigma_v};'); 16 | disp(' '); 17 | disp('2) Set the other parameters '); 18 | disp('>> k0 = 0; Ts=0.1;TimeUnit=''seconds'';'); 19 | disp(' '); 20 | disp('3) Create the state space model '); 21 | disp('ssObj=ss_DNL_AN(@demo_VDP_f,@demo_VDP_h,x0,w,v,k0,Ts,TimeUnit,...'); 22 | disp(' @demo_VDP_fjacx,@demo_VDP_hjacx);'); 23 | disp(' '); 24 | disp('Note that all variables are set explicitly, to make it easier to'); 25 | disp('follow what is happening. Shorter notations are of course possible.'); 26 | mySeed = 10; 27 | rng(mySeed); % Set the seed 28 | mu_x0=[0 5]';sigma_x0=diag([5, 5]); 29 | mu_w=[0 0]';sigma_w=diag([10e-2 10e-2]); 30 | mu_v=[0 0]';sigma_v=diag([10e-2 10e-2]); 31 | x0={mu_x0,sigma_x0};w={mu_w,sigma_w};v={mu_v,sigma_v}; 32 | k0 = 0; Ts=0.1;TimeUnit='seconds'; 33 | ssObj=ss_DNL_AN(@demo_VDP_f,@demo_VDP_h,x0,w,v,k0,Ts,TimeUnit,@demo_VDP_fjacx,@demo_VDP_hjacx); 34 | disp(' '); 35 | disp('press key'); pause 36 | 37 | clc; 38 | disp('Now that the VDP model is ready, we can simulate its states'); 39 | disp('and measurements and save both of them in the sim. object.'); 40 | disp(' '); 41 | disp('>> x0init=[1.4 0]'';samples=500; conf=''x y'';u=[];'); 42 | disp('>> simObj=sim(ssObj,samples,u,conf,x0init);'); 43 | x0init=[1.4 0]';samples=500; conf='x y';u=[]; 44 | simObj=sim(ssObj,samples,u,conf,x0init); 45 | disp(' '); 46 | disp('We can always quickly check if everything went as expected by making'); 47 | disp('a quick plot of the measurements and true states as follows:'); 48 | disp(' '); 49 | disp('>> plot(simObj,''xy'',(1:2),(1:2));'); 50 | plot(simObj,'xy',(1:2),(1:2)); 51 | disp(' '); 52 | disp('press key'); pause 53 | 54 | clc;close all; 55 | disp('Now that virtual measuremtents are available we can apply the'); 56 | disp('da method to estimate the states with EKF. Note that x, Pa and y'); 57 | disp('are saved for later usage.'); 58 | disp(' '); 59 | disp('>> damEKF=da(ssObj,''EKF'',simObj,''x y Pa'');'); 60 | damEKF=da(ssObj,'EKF',simObj,'x y Pa'); 61 | disp(' '); 62 | disp('Lets see how the results look like. We can easily make a plot of the'); 63 | disp('true states and the analysis states by using the plot command:'); 64 | disp(' '); 65 | disp('>> plot(damEKF,''xax'',(1:2));'); 66 | plot(damEKF,'xax',(1:2)); 67 | disp(' '); 68 | disp('This looks almost perfect. To compare the performance of diffent filters'); 69 | disp('in this case, the RMSE will be used.'); 70 | disp(' '); 71 | disp('press key'); pause 72 | 73 | clc;close all; 74 | disp('Lets apply the EKF, the UKF (has 5 sigma points) and the EnKF '); 75 | disp('with 10 ensemble members each 5 times on the VDP model to attain'); 76 | disp('the average RMSE value as follows:'); 77 | disp(' '); 78 | disp('its=5;rmseArray=zeros(2,3); '); 79 | disp('for i=1:its '); 80 | disp(' damEKF=da(ssObj,''EKF'',simObj,''x y Pa''); '); 81 | disp(' damUKF=da(ssObj,''UKF'',simObj,''x y Pa''); '); 82 | disp(' damEnKF=da(ssObj,''EnKF'',simObj,''x y Pa'',{10}); '); 83 | disp(' '); 84 | disp(' rmseArray(:,1)=rmseArray(:,1)+rmse(damEKF,(1:2));'); 85 | disp(' rmseArray(:,2)=rmseArray(:,2)+rmse(damUKF,(1:2));'); 86 | disp(' rmseArray(:,3)=rmseArray(:,3)+rmse(damEnKF,(1:2));'); 87 | disp('end '); 88 | disp('rmseArray=rmseArray./its;rmseArray'); 89 | disp(' '); 90 | disp('press key to start iterations'); pause 91 | 92 | clc;close all; 93 | its=5;rmseArray=zeros(2,3); 94 | for i=1:its 95 | 96 | damEKF=da(ssObj,'EKF',simObj,'x y Pa'); 97 | damUKF=da(ssObj,'UKF',simObj,'x y Pa'); 98 | damEnKF=da(ssObj,'EnKF',simObj,'x y Pa',{10}); 99 | 100 | rmseArray(:,1)=rmseArray(:,1)+rmse(damEKF,(1:2)); 101 | rmseArray(:,2)=rmseArray(:,2)+rmse(damUKF,(1:2)); 102 | rmseArray(:,3)=rmseArray(:,3)+rmse(damEnKF,(1:2)); 103 | disp('iteration:');i 104 | end 105 | rmseArray=rmseArray./its; 106 | rmseArray 107 | disp(' EKF, UKF, EnKF'); 108 | disp(' '); 109 | disp('As you can see the UKF algorithm provides the best estimation in this '); 110 | disp('case. Of course, only when increasing the ensemble size of the EnKF'); 111 | disp('its estimation would improve.'); 112 | disp(' '); 113 | disp('This concludes this demo. '); 114 | disp('press key'); pause 115 | close all; -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_VDP_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_VDP_f(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_F function handle template for f for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: process noise w (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_AN_F(X,K,U,~,TS) returns the new state X1 without noise 16 | % at step K with input U and sample time TS 17 | % 18 | alpha=0.2; 19 | 20 | x1(1,1) = x(1) + Ts * x(2); 21 | x1(2,1) = x(2) + Ts * ( (alpha * (1 - x(1)^2) * x(2) ) - x(1)); 22 | 23 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_VDP_fjacx.m: -------------------------------------------------------------------------------- 1 | function Jac = demo_VDP_fjacx(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_FJACX function handle template for the Jacobian of f 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: process noise w (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % FJACX = TEMPLATE_DNL_AN_FJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | alpha=0.2; 20 | 21 | Jac = [1 Ts; 22 | Ts*((-2*alpha*x(2)*(1 - x(1)))-1) 1+Ts*(alpha * (1 - x(1)^2))]; 23 | 24 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_VDP_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_VDP_h(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | y = [x(1) ;x(2)]; 19 | 20 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_da/demo_VDP_hjacx.m: -------------------------------------------------------------------------------- 1 | function Jac = demo_VDP_hjacx(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_HJACX function handle template for the Jacobian of h 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: meas. noise v (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % HJACX = TEMPLATE_DNL_AN_HJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | Jac = [1 0;0 1]; 20 | 21 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_nm_/demos_nm_gauss/demo_handle_mu.m: -------------------------------------------------------------------------------- 1 | function mu = demo_handle_mu(k,Ts) 2 | 3 | mu=[k k*Ts]'; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_nm_/demos_nm_gauss/demo_handle_sigma.m: -------------------------------------------------------------------------------- 1 | function sigma = demo_handle_sigma(k,Ts) 2 | 3 | sigma=[k 0 ; 0 k]; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_nm_/demos_nm_gauss/demo_nm_gauss_handle1.m: -------------------------------------------------------------------------------- 1 | % Demo about creating and manimulating a Gaussian noise model: nm_gauss_handle 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to create and manipulate a Gaussian noise model'); 6 | disp('with function handles (nm_gauss_handle). This demo is conducted using '); 7 | disp('a bivariate distribution. It is assumed that the ''demo_nm_gauss_lti1'' '); 8 | disp('demo has already been studied by the user.'); 9 | disp('To configure a multivariate Gaussian distribution two parameters are'); 10 | disp('required: a mean column vector mu and a covariance matrix Sigma.'); 11 | disp('In case of a function handle model, both parameters have to be '); 12 | disp('configured with a function handle.'); 13 | disp(' '); 14 | disp('press key'); pause 15 | 16 | clc; 17 | disp('The function handle for mu is @demo_handle_mu with configuration:'); 18 | disp(' '); 19 | disp('function mu = demo_handle_mu(k,Ts)'); 20 | disp(' '); 21 | disp(' mu=[k k*Ts]'';'); 22 | disp(' '); 23 | disp('end'); 24 | disp(' '); 25 | disp('The function handle for Sigma is @demo_handle_sigma with configuration:'); 26 | disp(' '); 27 | disp('function sigma = demo_handle_sigma(k,Ts)'); 28 | disp(' '); 29 | disp(' sigma=[k 0 ; 0 k];'); 30 | disp(' '); 31 | disp('end'); 32 | disp(' '); 33 | disp('Note that, although not required, the models are time variant with'); 34 | disp('step number k and the sample time Ts as input parameters. Hence, the '); 35 | disp('sample time is a required parameter for this kind of noise model as '); 36 | disp('will be clear when configuring the model. A second note is that it is'); 37 | disp('allowed for the function handle to return a column vector for Sigma'); 38 | disp('if Sigma is uncorrelated.'); 39 | disp(' '); 40 | disp('press key'); pause 41 | 42 | clc; 43 | disp('Now that the mean and the covariance are defined, a Gaussian function'); 44 | disp('handle noise model is constructed as follows:'); 45 | disp(' '); 46 | disp('>> Ts=2;'); 47 | disp('>> nmObj = nm_gauss_handle(@demo_handle_mu,@demo_handle_sigma,Ts);'); 48 | Ts=2; 49 | nmObj = nm_gauss_handle(@demo_handle_mu,@demo_handle_sigma,Ts); 50 | disp(' '); 51 | disp('The Gaussian noise model is displayed as follows:'); 52 | disp(' '); 53 | disp('>> nmObj'); 54 | nmObj 55 | disp(' '); 56 | disp('press key'); pause 57 | 58 | clc; 59 | disp('The regularly used methods are now presented in their most general'); 60 | disp('form. For their reduced possibilities use the help instruction.'); 61 | disp('The method mean returns the mean vector at step k'); 62 | disp('>> k=10;'); 63 | disp('>> mean(nmObj,k)'); 64 | k=10; 65 | mean(nmObj,k) 66 | disp('The method cov returns the covariance matrix at step k'); 67 | disp('>> cov(nmObj,k)'); 68 | cov(nmObj,k) 69 | disp('The method var returns a colum vector that contains the variances'); 70 | disp('of the covariance matrix at step k:'); 71 | disp('>> var(nmObj,k)'); 72 | var(nmObj,k) 73 | disp(' '); 74 | disp('press key'); pause 75 | 76 | clc; 77 | disp('The method sample draws n samples from the distribution at step k:'); 78 | disp(' '); 79 | disp('>> n=3;'); 80 | disp('>> sample(nmObj,n,k)'); 81 | n=3; 82 | sample(nmObj,n,k) 83 | disp('The method pdf calculates the pdf of the three vectors in x at step k'); 84 | disp(' '); 85 | disp('>> x=[10 11 12;10 11 12];'); 86 | disp('>> pdf(nmObj,x,k)'); 87 | x=[10 11 12;10 11 12]; 88 | pdf(nmObj,x,k) 89 | disp('where the first value is the pdf of vector [1;1] and so on.'); 90 | disp(' '); 91 | disp('press key'); pause 92 | 93 | clc; 94 | disp('The method cholcov returns the upper triangular square root'); 95 | disp('matrix of the positive semi-definite matrix Sigma at step k. '); 96 | disp('(When semi-definite the matrix is not triangular)'); 97 | disp('>> cholcov(nmObj,k)'); 98 | cholcov(nmObj,k) 99 | disp('This concludes this demo.'); -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_nm_/demos_nm_gauss/demo_nm_gauss_lti1.m: -------------------------------------------------------------------------------- 1 | % Demo about creating and manimulating a Gaussian noise model: nm_gauss_lti 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to create and manipulate an LTI Gaussian '); 6 | disp('noise model (nm_gauss_lti). This demo is conducted using a bivariate '); 7 | disp('distribution. To configure a multivariate Gaussian distribution two '); 8 | disp('parameters are required: a mean column vector mu and a covariance'); 9 | disp('matrix Sigma.'); 10 | disp(' '); 11 | disp('>> mu = [1 2]'';'); 12 | mu = [1 2]'; 13 | disp('>> mu'); 14 | mu 15 | disp('>> Sigma = [2 1;1 2];'); 16 | Sigma = [2 1;1 2]; 17 | disp('>> Sigma'); 18 | Sigma 19 | disp(' '); 20 | disp('press key'); pause 21 | 22 | clc; 23 | disp('Now that the mean and covariance are defined, a Gaussian LTI noise '); 24 | disp('model is constructed as follows:'); 25 | disp(' '); 26 | disp('>> nmObj = nm_gauss_lti(mu,Sigma);'); 27 | nmObj = nm_gauss_lti(mu,Sigma); 28 | disp(' '); 29 | disp('The LTI Gaussian noise is displayed as follows:'); 30 | disp(' '); 31 | disp('>> nmObj'); 32 | nmObj 33 | disp(' '); 34 | disp('press key'); pause 35 | 36 | clc; 37 | disp('In some cases the Gaussian LTI noise model can be created faster.'); 38 | disp('For example, in case of a zero mean distribution the mean does not'); 39 | disp('have to be specified, i.e. '); 40 | disp(' '); 41 | disp('>> nmObj = nm_gauss_lti(Sigma);'); 42 | nmObj = nm_gauss_lti(Sigma); 43 | disp(' '); 44 | disp('The mean has been automatically set to zero.'); 45 | disp(' '); 46 | disp('>> nmObj.mu'); 47 | nmObj.mu 48 | disp(' '); 49 | disp('The same can be applied if a unit covariance is desired. Now, only'); 50 | disp('the mean has to be specified, i.e. '); 51 | disp(' '); 52 | disp('>> nmObj = nm_gauss_lti(mu);'); 53 | nmObj = nm_gauss_lti(mu); 54 | disp(' '); 55 | disp('Sigma has been automatically set to the unit covariance matrix.'); 56 | disp(' '); 57 | disp('>> nmObj.Sigma'); 58 | nmObj.Sigma 59 | disp(' '); 60 | disp('Note that Sigma is allowed to be a column vector when Sigma is '); 61 | disp('an uncorrelated covariance matrix (Diagonal matrix). This will be'); 62 | disp('threated next.'); 63 | disp('press key'); pause 64 | 65 | clc; 66 | disp('Sigma is allowed to be defined as a column vector when Sigma is'); 67 | disp('an uncorrelated covariance matrix (Diagonal matrix). But, to avoid'); 68 | disp('ambiguity in the parameters, both mu and Sigma need to be defined.'); 69 | disp('For example:'); 70 | disp(' '); 71 | disp('>> nmObj = nm_gauss_lti([1 1]'',[2 3]'');'); 72 | nmObj = nm_gauss_lti([1 1]',[2 3]'); 73 | disp(' '); 74 | disp('>> nmObj.Sigma'); 75 | nmObj.Sigma 76 | disp(' '); 77 | disp('Of course, in a practical situation you do not want a vector for '); 78 | disp('the covariance matrix. This is why it is better to use the method'); 79 | disp('cov() to obtain the covariance matrix (also since it is obligated for'); 80 | disp('time-variant noise models. For example'); 81 | disp(' '); 82 | disp('>> cov(nmObj)'); 83 | cov(nmObj) 84 | disp('press key'); pause 85 | 86 | clc; 87 | disp('Still, it is recommended to use the most general form. Suppose the '); 88 | disp('current step k is 10, then it is better to use'); 89 | disp('>> k=10;'); 90 | disp('>> cov(nmObj,k)'); 91 | k=10; 92 | cov(nmObj,k) 93 | disp('since this structure is also valid if nmObj is a time variant '); 94 | disp('noise model.'); 95 | disp(' '); 96 | disp('The same reasoning counts for the mean, that is '); 97 | disp('>> mean(nmObj)'); 98 | mean(nmObj) 99 | disp('or in the most general form'); 100 | disp('>> mean(nmObj,k)'); 101 | mean(nmObj,k) 102 | disp('press key'); pause 103 | 104 | clc; 105 | disp('Other regularly used methods are now presented in their most general'); 106 | disp('form. For their reduced possibilities, please use the help'); 107 | disp('instruction. The method var returns a colum vector that contains'); 108 | disp('the variances:'); 109 | disp(' '); 110 | disp('>> var(nmObj,k)'); 111 | var(nmObj,k) 112 | disp(' '); 113 | disp('The method sample draws n samples from the distribution:'); 114 | disp(' '); 115 | disp('>> n=3;'); 116 | disp('>> sample(nmObj,n,k)'); 117 | n=3; 118 | sample(nmObj,n,k) 119 | disp(' '); 120 | disp('press key'); pause 121 | 122 | clc; 123 | disp('The method pdf calculates the pdf of the three vectors in x'); 124 | disp(' '); 125 | disp('>> x=[1 2 3;1 2 3];'); 126 | disp('>> pdf(nmObj,x,k)'); 127 | x=[1 2 3;1 2 3]; 128 | pdf(nmObj,x,k) 129 | disp('where the first value is the pdf of vector [1;1] and so on.'); 130 | disp(' '); 131 | disp('The method cholcov returns the upper triangular square root'); 132 | disp('matrix of the positive semi-definite matrix Sigma. (When semi-'); 133 | disp('definite the matrix is not triangular)'); 134 | disp('>> cholcov(nmObj,k)'); 135 | cholcov(nmObj,k) 136 | disp('This concludes this demo.'); -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_nm_/demos_nm_gauss/demo_nm_gauss_ltv1.m: -------------------------------------------------------------------------------- 1 | % Demo about creating and manimulating a Gaussian noise model: nm_gauss_ltv 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to create and manipulate an LTV Gaussian '); 6 | disp('noise model (nm_gauss_ltv). This demo is conducted using a '); 7 | disp('bivariate distribution. It is assumed that the d''emo_nm_gauss_lti1'' '); 8 | disp('demo has already been studied by the user.'); 9 | disp('To configure a multivariate Gaussian distribution two parameters are '); 10 | disp('required: a mean column vector mu and a covariance matrix Sigma.'); 11 | disp('In case of an LTV model, at least one of these parameters has to'); 12 | disp('be a 3D-array. For ease of notation only the mean is assumed to be'); 13 | disp('time variant. The extension for Sigma is trivial.'); 14 | disp(' '); 15 | disp('press key'); pause 16 | 17 | clc; 18 | disp('Suppose mu changes at three step numbers'); 19 | disp(' '); 20 | disp('>> mu = zeros(2,1,3);'); 21 | disp('>> mu(:,:,1) = [1 1]'';'); 22 | disp('>> mu(:,:,2) = [2 2]'';'); 23 | disp('>> mu(:,:,3) = [3 3]'';'); 24 | mu = zeros(2,1,3); 25 | mu(:,:,1) = [1 1]; 26 | mu(:,:,2) = [2 2]; 27 | mu(:,:,3) = [3 3]; 28 | disp('>> mu'); 29 | mu 30 | disp('press key'); pause 31 | 32 | clc; 33 | disp('and Sigma is defined as time invariant'); 34 | disp('>> Sigma = [2 1;1 2];'); 35 | Sigma = [2 1;1 2]; 36 | disp('>> Sigma'); 37 | Sigma 38 | disp(' '); 39 | disp('press key'); pause 40 | 41 | clc; 42 | disp('Now that the mean and the covariance are defined, a Gaussian LTV'); 43 | disp('noise model is constructed as follows:'); 44 | disp(' '); 45 | disp('>> nmObj = nm_gauss_ltv(mu,Sigma);'); 46 | nmObj = nm_gauss_ltv(mu,Sigma); 47 | disp(' '); 48 | disp('The LTV Gaussian noise is displayed as follows:'); 49 | disp(' '); 50 | disp('>> nmObj'); 51 | nmObj 52 | disp(' '); 53 | disp('press key'); pause 54 | 55 | clc; 56 | disp('In some cases the Gaussian LTV noise model can be created faster.'); 57 | disp('For example, when a unit covariance is desired.'); 58 | disp(' '); 59 | disp('>> nmObj = nm_gauss_ltv(mu);'); 60 | nmObj = nm_gauss_ltv(mu); 61 | disp(' '); 62 | disp('Sigma has been automatically set to the unit covariance matrix.'); 63 | disp(' '); 64 | disp('>> nmObj.Sigma'); 65 | nmObj.Sigma 66 | disp(' '); 67 | disp('press key'); pause 68 | 69 | clc; 70 | disp('In the previous creations of the LTV noise model, two parameters'); 71 | disp('have been omitted: kIndex and kMethod. Let us first concentrate on'); 72 | disp('kIndex which specifies the step number related to each array in'); 73 | disp('a 3D-array. At this moment kIndex is'); 74 | disp('>> nmObj.kIndex'); 75 | nmObj.kIndex 76 | disp('Hence, the first array corresponds to step number 0, the second to 1'); 77 | disp('and so on. This is the default setting of kIndex. To make the demo'); 78 | disp('a bit more interesting, lets redefine kIndex to'); 79 | disp('>> nmObj.kIndex=[10 20 30];'); 80 | disp('>> nmObj.kIndex'); 81 | nmObj.kIndex=[10 20 30]; 82 | nmObj.kIndex 83 | disp('If we now call for the mean at step 14'); 84 | disp('>> mean(nmObj,14)'); 85 | mean(nmObj,14) 86 | disp('we get the mean of step 10. We will see why at the next page'); 87 | disp('press key'); pause 88 | 89 | clc; 90 | disp('The paramater kMethod is currently'); 91 | disp('>> nmObj.kMethod'); 92 | nmObj.kMethod 93 | disp('which is the default setting. Now, the reason why the mean of step 10'); 94 | disp('was provided when asking for the mean step 14 is as follows: Since '); 95 | disp('step 14 is is not defined in kIndex, but 10 and 20 are, kMethod'); 96 | disp('defines how to look for the mean. In case of ''low'' the lower index'); 97 | disp('number 10 is used. In case of ''high'' the higher index 20 is used.'); 98 | disp('In case of ''near'' the closest index is used, in this case 10:'); 99 | disp('>> nmObj.kMethod=''near'''); 100 | disp('>> mean(nmObj,14)'); 101 | nmObj.kMethod='near'; 102 | mean(nmObj,14) 103 | disp(' '); 104 | disp('press key'); pause 105 | 106 | clc; 107 | disp('Other regularly used methods are now presented in their most general'); 108 | disp('form. For their reduced possibilities, please use the help'); 109 | disp('instruction. Let us reconfigure the noise model with covariance'); 110 | disp('matrix Sigma.'); 111 | disp('>> nmObj = nm_gauss_ltv(mu,Sigma,[10 20 30],''high'');'); 112 | nmObj = nm_gauss_ltv(mu,Sigma,[10 20 30],'high'); 113 | disp('The method cov returns the covariance matrix at step k'); 114 | disp('>> k=15;'); 115 | disp('>> cov(nmObj,k)'); 116 | k=15; 117 | cov(nmObj,k) 118 | disp('The method var returns a colum vector that contains the variances'); 119 | disp('of the covariance matrix at step k:'); 120 | disp('>> var(nmObj,k)'); 121 | var(nmObj,k) 122 | disp(' '); 123 | disp('press key'); pause 124 | 125 | clc; 126 | disp('The method sample draws n samples from the distribution at step k:'); 127 | disp(' '); 128 | disp('>> n=3;'); 129 | disp('>> sample(nmObj,n,k)'); 130 | n=3; 131 | sample(nmObj,n,k) 132 | disp('The method pdf calculates the pdf of the three vectors in x at step k'); 133 | disp(' '); 134 | disp('>> x=[1 2 3;1 2 3];'); 135 | disp('>> pdf(nmObj,x,k)'); 136 | x=[1 2 3;1 2 3]; 137 | pdf(nmObj,x,k) 138 | disp('where the first value is the pdf of vector [1;1] and so on.'); 139 | disp(' '); 140 | disp('press key'); pause 141 | 142 | clc; 143 | disp('The method cholcov returns the upper triangular square root'); 144 | disp('matrix of the positive semi-definite matrix Sigma at step k. '); 145 | disp('(When semi-definite the matrix is not triangular)'); 146 | disp('>> cholcov(nmObj,k)'); 147 | cholcov(nmObj,k) 148 | disp('This concludes this demo.'); -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_sim/demo_sim1.m: -------------------------------------------------------------------------------- 1 | % Demo about using the simulation method to create simulation objects. 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to apply the simulation model on a discrete-time '); 6 | disp('state space model to obtain a simulation model.'); 7 | disp('This demo uses the preconfigured functions ''demo_sim1f'' and ''demo_sim1h'''); 8 | disp('which represent the Van Der Pol oscillator.'); 9 | disp(' '); 10 | disp('First a ss_DNL_AN state space model is created using the following steps:'); 11 | disp(' '); 12 | disp('1) Create the noise models '); 13 | disp('>> mu_x0=[0 5]'';sigma_x0=diag([5 5]);'); 14 | disp('>> mu_w=[0 0]'';sigma_w=diag([10e-3 10e-3]);'); 15 | disp('>> mu_v=[0 0]'';sigma_v=diag([10e-3 10e-3]);'); 16 | disp(' '); 17 | disp('>> x0={mu_x0,sigma_x0};'); 18 | disp('>> w={mu_w,sigma_w};'); 19 | disp('>> v={mu_v,sigma_v};'); 20 | disp(' '); 21 | disp('2) Set the other parameters '); 22 | disp('>> k0 = 0; '); 23 | disp('>> Ts = 0.1; '); 24 | disp('>> TimeUnit=''seconds''; '); 25 | disp(' '); 26 | disp('3) Create the state space model '); 27 | disp('ssObj=ss_DNL_AN(@demo_sim1f,@demo_sim1h,x0,w,v,k0,Ts,TimeUnit);'); 28 | disp(' '); 29 | disp('Note that all variables are set explicitly, to make it easier to'); 30 | disp('see what is happening. Shorter notations are of course possible.'); 31 | mu_x0=[0 5]'; 32 | sigma_x0=diag([5 5]); 33 | mu_w=[0 0]'; 34 | sigma_w=diag([10e-3 10e-3]); 35 | mu_v=[0 0]'; 36 | sigma_v=diag([10e-3 10e-3]); 37 | x0={mu_x0,sigma_x0}; 38 | w={mu_w,sigma_w}; 39 | v={mu_v,sigma_v}; 40 | k0 = 0; 41 | Ts = 0.1; 42 | TimeUnit='seconds'; 43 | ssObj=ss_DNL_AN(@demo_sim1f,@demo_sim1h,x0,w,v,k0,Ts,TimeUnit); 44 | disp(' '); 45 | disp('press key'); pause 46 | 47 | clc; 48 | disp('Now that the state-space model is ready, we can simulate its states'); 49 | disp('and measurements in different ways.'); 50 | disp('The fastes way is as follows:'); 51 | disp(' '); 52 | disp('>> simObj = sim(ssObj);'); 53 | simObj = sim(ssObj); 54 | disp(' '); 55 | disp('This fast calling created the following simulation object:'); 56 | disp(' '); 57 | disp('>> simObj'); 58 | simObj 59 | disp(' '); 60 | disp('It only contains one sample: one state and one measurement.'); 61 | disp('This is the default behaviour if the amount of sample is not'); 62 | disp('specified.'); 63 | disp(' '); 64 | disp('press key'); pause 65 | 66 | clc; 67 | disp('Now lets try some more samples, for example 10:'); 68 | disp(' '); 69 | disp('>> simObj = sim(ssObj,10)'); 70 | simObj = sim(ssObj,10) 71 | disp(' '); 72 | disp('The step number index has the following content:'); 73 | disp(' '); 74 | disp('>> simObj.k'); 75 | simObj.k 76 | disp('It starts from 0 to 9, which are the values of k for each sample.'); 77 | disp(' '); 78 | disp('press key'); pause 79 | 80 | clc; 81 | disp('If you would like to start from step 5, just change it in the '); 82 | disp('state space model and run the simulation again:'); 83 | disp('>> ssObj.k0=5;simObj = sim(ssObj,10);'); 84 | disp('>> simObj.k'); 85 | ssObj.k0=5; 86 | simObj = sim(ssObj,10); 87 | simObj.k 88 | disp(' '); 89 | disp('Suppose you are not intrested in saving the simulated states. '); 90 | disp('You can change this setting by adding the conf settings: '); 91 | disp('>> simObj = sim(ssObj,10,[],''y'');'); 92 | disp('>> simObj'); 93 | simObj = sim(ssObj,10,[],'y'); 94 | simObj 95 | disp(' '); 96 | disp('Now, the states are not saved. Also note that when calling the method,'); 97 | disp('an empty matrix was used to indicate that there are no inputs. '); 98 | disp(' '); 99 | disp('press key'); pause 100 | 101 | clc; 102 | disp('Another setting that can be useful is to specify the initial state.'); 103 | disp('Until now, the simulation model has simulated the initial state by'); 104 | disp('taking a sample from the initial state noise model. For example:'); 105 | disp(' '); 106 | disp('>> simObj = sim(ssObj,10);'); 107 | disp('>> simObj.x(1,:)'); 108 | simObj = sim(ssObj,10); 109 | simObj.x(:,1) 110 | disp(' '); 111 | disp('To specify the inital state manually do the following:'); 112 | disp(' '); 113 | disp('>> simObj = sim(ssObj,10,[],'''',[1.4 0]'');'); 114 | simObj = sim(ssObj,10,[],'',[1.4 0]'); 115 | disp(' '); 116 | disp('Lets see if that was really applied.'); 117 | disp(' '); 118 | disp('>> simObj.x(1,:)'); 119 | simObj.x(:,1) 120 | disp(' '); 121 | disp('press key'); pause 122 | 123 | clc; 124 | disp('The last possible setting of the simulation method is the noise'); 125 | disp('parameter. When setting this value equal to 0, no noise is added'); 126 | disp('during the simulation, when set to 1 only process noise is added,'); 127 | disp('when set to 2 only measurement noise is addded and finally, when'); 128 | disp('the noise is set to 3 both noise sources are addded. This is of '); 129 | disp('course the default setting.'); 130 | disp(' '); 131 | disp('As a final example, lets set the noise to 2 while leaving the other'); 132 | disp('at their default values:'); 133 | disp(' '); 134 | disp('>> simObj = sim(ssObj,10,[],'''',[]'',2);'); 135 | simObj = sim(ssObj,10,[],'',[],2); 136 | disp(' '); 137 | disp('This concludes this demo. '); -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_sim/demo_sim1f.m: -------------------------------------------------------------------------------- 1 | function x1 = vdp1_f(x,t,u,~,Ts) 2 | 3 | alpha=0.2; 4 | 5 | x1(1,1) = x(1) + Ts * x(2); 6 | x1(2,1) = x(2) + Ts * ( (alpha * (1 - x(1)^2) * x(2) ) - x(1)); 7 | 8 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_sim/demo_sim1h.m: -------------------------------------------------------------------------------- 1 | function y = vdp1_h(x,t,u,~,Ts) 2 | 3 | y = [x(1) ;x(2)]; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_sim_D/demo_sim_D1.m: -------------------------------------------------------------------------------- 1 | % Demo about using the methods of the simulation object 2 | 3 | clc; 4 | 5 | disp('This is a demo of how to use the simulation methods of the discrete-'); 6 | disp('time simulation object.'); 7 | disp('To have a fast start on using the methods we will configure a simulation'); 8 | disp('model manually. Lets make an object that contains 200 samples of a '); 9 | disp('state space with 10 states and 5 inputs where only the even states'); 10 | disp('are measured as follows'); 11 | disp(' '); 12 | disp('1) create 10 states of values 5 to 45 with added noise'); 13 | disp(' '); 14 | disp('>> X=randn(10,200); X=X+repmat((0:5:45)'',1,200);'); 15 | disp(' '); 16 | X=randn(10,200); X=X+repmat((0:5:45)',1,200); 17 | disp('2) create 5 inputs of values -5 to -20 with added noise'); 18 | disp(' '); 19 | disp('>> U=0.1*randn(5,200); U=U-repmat((0:5:20)'',1,200);'); 20 | disp(' '); 21 | U=0.1*randn(5,200); U=U-repmat((0:5:20)',1,200); 22 | disp('3) set measurements equal to even state and add noise'); 23 | disp(' '); 24 | disp('>> Y=X((2:2:10),:)+0.1*randn(5,200);'); 25 | disp(' '); 26 | Y=X((2:2:10),:)+0.1*randn(5,200); 27 | disp('4) create the simulation object with sample time of 2 minutes and an'); 28 | disp(' index that starts from 5 till 204'); 29 | disp(' '); 30 | disp('>> simObj=sim_D(X,U,Y,(5:204),2,''minutes'')'); 31 | disp(' '); 32 | simObj=sim_D(X,U,Y,(5:204),2,'minutes'); 33 | disp(' '); 34 | disp('press key'); pause 35 | 36 | clc; 37 | disp('Now that we have a simulation object, we can make plots of it using'); 38 | disp('the plot method. The plot method always requires at least three'); 39 | disp('input arguments: the simulation object, the configuration string that'); 40 | disp('specifies whether the state, input or meas.needs to be shown and'); 41 | disp('the specification of which state/input/meas. are requested.'); 42 | disp(' '); 43 | disp('As a first example lets plot the states 2-5 and 7-8 using the default '); 44 | disp('plot style (subplots)'); 45 | disp(' '); 46 | disp('>> plot(simObj,''x'',[2:5 7:8]) '); 47 | plot(simObj,'x',[2:5 7:8]) 48 | disp(' '); 49 | disp('Notice how the plot method automatically arranges the subplots and '); 50 | disp('indicates the different states.'); 51 | disp(' '); 52 | disp('press key'); pause 53 | 54 | clc;close all; 55 | disp('As a second example lets plot all the inputs using the single '); 56 | disp('plot style, which is indicated by adding a 1 in the configuration string'); 57 | disp(' '); 58 | disp('>> plot(simObj,''u 1'',(1:5) '); 59 | plot(simObj,'u 1',(1:5)) 60 | disp(' '); 61 | disp('Notice how the plot method automatically adds a legend and '); 62 | disp('and a title.'); 63 | disp(' '); 64 | disp('press key'); pause 65 | 66 | clc;close all; 67 | disp('As a third example lets plot all the measurements using the subplot'); 68 | disp('plot style while using the time scale for the x-axis by adding a t'); 69 | disp('in the configuration string as follows:'); 70 | disp(' '); 71 | disp('>> plot(simObj,''y 2 t'',(1:5) '); 72 | plot(simObj,'y 2 t',(1:5)) 73 | disp(' '); 74 | disp('Notice how the plot method properly changed the x -axis scale.'); 75 | disp(' '); 76 | disp('press key'); pause 77 | 78 | clc;close all; 79 | disp('As a final example lets plot the states and measurements using the '); 80 | disp('subplot plot style. Remember that only the even states were measured.'); 81 | disp('Hence the command becomes:'); 82 | disp(' '); 83 | disp('>> plot(simObj,''xy 2 t'',(2:2:10),(1:5) )'); 84 | plot(simObj,'xy 2 t',(2:2:10),(1:5)) 85 | disp(' '); 86 | disp('This concludes this demo.'); 87 | disp('press key'); pause 88 | close all; 89 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_AN_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_DNL_AN_f(x,k,u,~,Ts) 2 | 3 | x1(1,1) = x(1)+2*x(2) + u; 4 | x1(2,1) = x(1)+x(2); 5 | 6 | end 7 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_AN_fJacX.m: -------------------------------------------------------------------------------- 1 | function JacX = demo_DNL_AN_fJacX(x,k,u,~,Ts) 2 | 3 | JacX = [1 2 ; 1 1] ; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_AN_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_DNL_AN_h(x,k,u,~,Ts) 2 | 3 | y(1,1) = x(1); 4 | y(2,1) = x(2); 5 | 6 | end 7 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_AN_hJacX.m: -------------------------------------------------------------------------------- 1 | function JacX = demo_DNL_AN_hJacX(x,k,u,~,Ts) 2 | 3 | JacX = [1 0 ; 0 1] ; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_f.m: -------------------------------------------------------------------------------- 1 | function x1 = demo_DNL_f(x,k,u,w,Ts) 2 | 3 | x1(1,1) = x(1)+2*x(2) + u + w(1); 4 | x1(2,1) = x(1)+x(2) + w(2); 5 | 6 | end 7 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_fJacX.m: -------------------------------------------------------------------------------- 1 | function JacX = demo_DNL_fJacX(x,k,u,w,Ts) 2 | 3 | JacX = [1 2 ; 1 1] ; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_h.m: -------------------------------------------------------------------------------- 1 | function y = demo_DNL_h(x,k,u,v,Ts) 2 | 3 | y(1,1) = x(1) + v(1); 4 | y(2,1) = x(2) + v(2); 5 | 6 | end 7 | -------------------------------------------------------------------------------- /DA_Toolbox/dademos/demos/demos_ss_/demos_ss_D/demo_DNL_hJacX.m: -------------------------------------------------------------------------------- 1 | function JacX = demo_DNL_hJacX(x,k,u,v,Ts) 2 | 3 | JacX = [1 0 ; 0 1] ; 4 | 5 | end -------------------------------------------------------------------------------- /DA_Toolbox/dahelp/dahelp.m: -------------------------------------------------------------------------------- 1 | % ============================================================= % 2 | % Interactively shows a list of the most important help files % 3 | % ============================================================= % 4 | % 5 | % Noise models: 6 | % - nm_gauss_lti1: properties, construction and methods 7 | % - nm_gauss_ltv1: properties, construction and methods 8 | % - nm_gauss_handle1: properties, construction and methods 9 | % 10 | % State space models: 11 | % 12 | % - ss_D: da and sim methods 13 | % - ss_DL: properties, construction and methods 14 | % - ss_DNL_AN: properties, construction and methods 15 | % - ss_DNL: properties, construction and methods 16 | % 17 | % Simulation models: 18 | % 19 | % - sim_D: properties, construction and methods 20 | % 21 | % Data assimilation models: 22 | % 23 | % - dam_D: properties, construction and methods 24 | % 25 | 26 | clc; 27 | 28 | disp('To see the help files, enter ''help'' and the name of the file in the '); 29 | disp('command line (e.g. help nm_gauss_lti).'); 30 | disp('All the help files explain the different properties, construction techniques'); 31 | disp('and methods of the corresponding class. To have more detailed information'); 32 | disp('about a method, click on its highlighted name. The help files of methods,'); 33 | disp('or functions in general, explain the in- and output parameters of the '); 34 | disp('function and the syntax to call it.'); 35 | disp(' '); 36 | disp(' Help files for noise models:'); 37 | disp(' '); 38 | disp(' The Gaussian noise models'); 39 | disp(' -------------------------'); 40 | disp(' - nm_gauss_lti - The linear time invariant Gaussian noise model'); 41 | disp(' - nm_gauss_ltv - The linear time variant Gaussian noise model'); 42 | disp(' - nm_gauss_handle - The Gaussian noise model with function handles'); 43 | disp(' '); 44 | disp(' Help files for state space models:'); 45 | disp(' '); 46 | disp(' The discrete-time state space models'); 47 | disp(' ------------------------------------'); 48 | disp(' - ss_DL - The linear discrete-time state space model'); 49 | disp(' - ss_DNL_AN - The nonlinear discrete-time state space model'); 50 | disp(' with additive noise'); 51 | disp(' - ss_DNL - The nonlinear discrete-time state space model'); 52 | disp(' '); 53 | disp(' - ss_D - The superclass of all discrete-time state space models.'); 54 | disp(' Its methods are the simulation method and all data '); 55 | disp(' assimilation techniques. '); 56 | disp(' '); 57 | disp('press key'); pause 58 | 59 | clc; 60 | disp('To see the help files, enter ''help'' and the name of the file in the '); 61 | disp('command line (e.g. help nm_gauss_lti).'); 62 | disp('All the help files explain the different properties, construction techniques'); 63 | disp('and methods of the corresponding class. To have more detailed information'); 64 | disp('about a method, click on its highlighted name. The help files of methods,'); 65 | disp('or functions in general, explain the in- and output parameters of the '); 66 | disp('function and the syntax to call it.'); 67 | disp(' '); 68 | disp(' Help files for simultation models:'); 69 | disp(' '); 70 | disp(' The discrete-time simulation models'); 71 | disp(' -----------------------------------'); 72 | disp(' - sim_D - The discrete-time simulation model.'); 73 | disp(' '); 74 | disp(' Help files for data assimilation models:'); 75 | disp(' '); 76 | disp(' The discrete-time data assimilation models'); 77 | disp(' ------------------------------------------'); 78 | disp(' - dam_D - The discrete-timecdata assimilation model.'); 79 | disp(' '); 80 | 81 | -------------------------------------------------------------------------------- /DA_Toolbox/dahelp/figures/contains figures to use in html help.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/dahelp/figures/contains figures to use in html help.txt -------------------------------------------------------------------------------- /DA_Toolbox/dahelp/html/contains generated html files from m code.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/dahelp/html/contains generated html files from m code.txt -------------------------------------------------------------------------------- /DA_Toolbox/daobsolete/@nm_empty/nm_empty.m: -------------------------------------------------------------------------------- 1 | classdef nm_empty < nm_ 2 | 3 | properties (Access = public) 4 | p; %Amount of variables 5 | end 6 | 7 | properties (Dependent = true, SetAccess = private) 8 | mu; 9 | Sigma; 10 | end 11 | 12 | methods 13 | 14 | function obj = nm_empty(size) 15 | 16 | narginchk(1,1); 17 | 18 | obj.p = size; 19 | 20 | end 21 | 22 | function obj = set.p(obj,value) 23 | 24 | value=checkArgs(value,'p'); %check p 25 | obj.p=value; 26 | obj=checkConsist(obj); 27 | 28 | end 29 | 30 | function value = get.mu(obj) 31 | 32 | value=zeros(obj.p,1); 33 | 34 | end 35 | 36 | function value = get.Sigma(obj) 37 | 38 | value=zeros(obj.p); 39 | 40 | end 41 | 42 | function display(obj) 43 | 44 | disp(' '); 45 | disp([inputname(1),' = ']) 46 | disp(' '); 47 | if ~isempty(obj) 48 | disp('Empty Noise Model.') 49 | disp('------------------') 50 | disp('Properties:'); 51 | if obj.p < 10 52 | fprintf('Mean \t\t(.mu) \t = \n'); 53 | disp(obj.mu); 54 | fprintf('Covariance \t(.Sigma) = \n'); 55 | disp(obj.Sigma); 56 | else 57 | fprintf('Mean \t\t(.mu) \t = \t[%i x %i %s] \n',size(obj.mu,1),size(obj.mu,2),class(obj.mu)) 58 | fprintf('Covariance \t(.Sigma) = \t[%i x %i %s]\n',size(obj.Sigma,1),size(obj.Sigma,2),class(obj.Sigma)) 59 | end 60 | fprintf('Nr variables(.p) \t = \t%i\n',obj.p); 61 | disp(' '); 62 | else 63 | disp('Empty Empty-Noise Model.') 64 | end 65 | 66 | end 67 | 68 | function value = cov(obj,~) 69 | 70 | value=obj.Sigma; 71 | 72 | end 73 | 74 | function value = mean(obj,~) 75 | 76 | value=obj.mu; 77 | 78 | end 79 | 80 | function samples = sample(obj,n,~) 81 | 82 | if nargin==1; n = 1; 83 | elseif nargin==2; 84 | elseif nargin==3; 85 | else error('DA:NoiseModels:nm_empty:nm_empty:argMismatch','Incorrect number of input arguments.'); 86 | end 87 | 88 | samples = repmat(obj.mu,1, n); 89 | end 90 | 91 | function densEst = pdf(obj, x,~) 92 | 93 | if nargin<2; 94 | error('DA:NoiseModels:nm_empty:nm_empty:argMismatch','Incorrect number of input arguments.'); 95 | end 96 | 97 | [p, n] = size(x); 98 | 99 | if(p ~= obj.p) 100 | error('DA:NoiseModels:nm_empty:nm_empty:xSize','X has wrong variable(row) size for density calculation.') 101 | end 102 | 103 | densEst = repmat(obj.mu,1, n); 104 | 105 | end 106 | 107 | function value=isempty(obj) 108 | 109 | if (~isempty(obj.mu)&& ~isempty(obj.Sigma)) 110 | value=0; 111 | else 112 | value=1; 113 | end 114 | 115 | end 116 | 117 | end %methods 118 | 119 | methods (Access = private) 120 | 121 | function obj=checkConsist(obj) 122 | 123 | if ~isempty(obj) 124 | 125 | if length(obj.mu)~=length(obj.Sigma) 126 | error('DA:NoiseModels:nm_empty:nm_empty:sigmaMuDimMismatch','The dimensions of Sigma and mu must agree') 127 | end 128 | 129 | end 130 | 131 | end 132 | 133 | end %methods 134 | end %classdef -------------------------------------------------------------------------------- /DA_Toolbox/daobsolete/@nm_empty/private/checkArgs.m: -------------------------------------------------------------------------------- 1 | function arg=checkArgs(arg,strCode) 2 | 3 | if strcmp(strCode,'p') 4 | p=arg; 5 | 6 | if isempty(p) 7 | error('DA:NoiseModels:nm_empty:checkArgs:muEmpty','p must be non-empty.') 8 | end 9 | 10 | if ~isa(p,'double') 11 | error('DA:NoiseModels:nm_empty:checkArgs:muClassMismatch','p must be of class ''double''.') 12 | end 13 | 14 | N=size(p); 15 | 16 | if (N(1)>1||N(2)>1) %check dimensions 17 | error('DA:NoiseModels:nm_empty:checkArgs:muDimMismatch','p must be a scalar.') 18 | end 19 | 20 | arg=p; 21 | 22 | end 23 | 24 | end -------------------------------------------------------------------------------- /DA_Toolbox/daobsolete/contains obsolete files.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/daobsolete/contains obsolete files.txt -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/nm_gauss_fun_handles/template_mu.m: -------------------------------------------------------------------------------- 1 | function mu = template_mu(k,Ts) 2 | %TEMPLATE_MU function handle template to retrieve mu for class nm_gauss_handle 3 | % 4 | % - Input variable(s) - 5 | % K: the step number 6 | % TS: the sample time 7 | % 8 | % - Output variable(s) - 9 | % MU: the calculated mean 10 | % 11 | % - Construction - 12 | % MU = TEMPLATE_MU(K,TS) returns the mean MU of the noise model with 13 | % sample time TS at step k 14 | % 15 | 16 | % This function can be configured as desired, but keep its syntax!! 17 | % 18 | % EXAMPLES: 19 | % 20 | % a bivariate mean where the second variable increases proportionaly with k 21 | % mu=[0 ;0.01*k*Ts]; 22 | % 23 | % a LTI bivariate zero mean 24 | % mu=[0 ; 0]; 25 | 26 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/nm_gauss_fun_handles/template_sigma.m: -------------------------------------------------------------------------------- 1 | function sigma = template_sigma(k,Ts) 2 | %TEMPLATE_SIGMA function handle template to retrieve sigma for class nm_gauss_handle 3 | % 4 | % - Input variable(s) - 5 | % K: the step number 6 | % TS: the sample time 7 | % 8 | % - Output variable(s) - 9 | % SIGMA: the calculated covariance matrix 10 | % 11 | % - Construction - 12 | % SIGMA = TEMPLATE_SIGMA(K,TS) returns the covariance matrix SIGMA of the 13 | % noise model with sample time TS at step k 14 | % 15 | 16 | % This function can be configured as desired, but keep its syntax!! 17 | % 18 | % EXAMPLES: 19 | % 20 | % a bivariate covariance matrix whos variances depend on the sampling time 21 | % sigma =[Ts 0 ; 0 Ts]; 22 | % 23 | % shorter syntax of previous example 24 | % sigma =[Ts ; Ts]; 25 | % 26 | % mixture with step number 27 | % sigma =[Ts+0.01*k ; Ts]; 28 | 29 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_AN_f.m: -------------------------------------------------------------------------------- 1 | function x1 = template_DNL_AN_f(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_F function handle template for f for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: process noise w (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_AN_F(X,K,U,~,TS) returns the new state X1 without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | % EXAMPLES: 20 | % linear model 21 | % x1(1,1) = x(1)+2*x(2) + u; 22 | % x1(2,1) = x(1)+x(2); 23 | % 24 | % non-linear model 25 | % x1(1,1) = x(2)*k; 26 | % x1(2,1) = -x(1) * x(2)^2 / 2 * x(3); 27 | % x1(3,1) = x(2); 28 | 29 | end 30 | -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_AN_fJacX.m: -------------------------------------------------------------------------------- 1 | function fJacX = template_DNL_AN_fJacX(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_FJACX function handle template for the Jacobian of f 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: process noise w (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % FJACX = TEMPLATE_DNL_AN_FJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % fJacX = [1 2 ; 1 1] ; 23 | % 24 | % non-linear model 25 | % fJacX = [0 k 0;-1 x(2) 1;0 0 0]; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_AN_h.m: -------------------------------------------------------------------------------- 1 | function y = template_DNL_AN_h(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_H function handle template for h for model of class ss_DNL_AN 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % ~: meas. noise v (does not apply here) 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_AN_H(X,K,U,~,TS) returns the new state Y without noise 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | % EXAMPLES: 20 | % linear model 21 | % y(1,1) = x(1); 22 | % y(2,1) = x(2); 23 | % 24 | % non-linear model 25 | % y = x(1)+x(2)*k; 26 | 27 | end 28 | -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_AN_hJacX.m: -------------------------------------------------------------------------------- 1 | function hJacX = template_DNL_AN_hJacX(x,k,u,~,Ts) 2 | %TEMPLATE_DNL_AN_HJACX function handle template for the Jacobian of h 3 | % with respect to X for model of class ss_DNL_AN 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % ~: meas. noise v (does not apply here) 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % HJACX = TEMPLATE_DNL_AN_HJACX(X,K,U,~,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % hJacX = [1 0 ; 0 1] ; 23 | % 24 | % non-linear model 25 | % hJacX = [1 k 0]; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_f.m: -------------------------------------------------------------------------------- 1 | function x1 = template_DNL_f(x,k,u,w,Ts) 2 | %TEMPLATE_DNL_F function handle template for f for model of class ss_DNL 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % W: process noise vector w 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % X1: new state without noise 13 | % 14 | % - Construction - 15 | % X1 = TEMPLATE_DNL_F(X,K,U,W,TS) returns the new state X1 with noise w 16 | % with input U and sample time TS 17 | % 18 | 19 | % EXAMPLES: 20 | % linear model 21 | % x1(1,1) = x(1)+2*x(2) + u + w(1); 22 | % x1(2,1) = x(1)+x(2) + w(2); 23 | % 24 | % non-linear model 25 | % x1(1,1) = x(2)*k + w(1); 26 | % x1(2,1) = -x(1) * x(2)^2 / 2 * x(3) + w(2); 27 | % x1(3,1) = x(2) + w(3); 28 | 29 | end 30 | -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_fJacW.m: -------------------------------------------------------------------------------- 1 | function fJacW = template_DNL_fJacW(x,k,u,w,Ts) 2 | %TEMPLATE_DNL_FJACW function handle template for the Jacobian of f 3 | % with respect to W for model of class ss_DNL 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % W: process noise vector w 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACW: Jacobian of f with respect to W 14 | % 15 | % - Construction - 16 | % FJACW = TEMPLATE_DNL_FJACW(X,K,U,W,TS) returns Jacobian of f with respect 17 | % to W at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % fJacW = [1 0 ; 0 1] ; 23 | % 24 | % non-linear model 25 | % fJacW = [1 0 0;0 1 0;0 0 1]; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_fJacX.m: -------------------------------------------------------------------------------- 1 | function fJacX = template_DNL_fJacX(x,k,u,w,Ts) 2 | %TEMPLATE_DNL_FJACX function handle template for the Jacobian of f 3 | % with respect to X for model of class ss_DNL 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % W: process noise vector w 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % FJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % FJACX = TEMPLATE_DNL_FJACX(X,K,U,W,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % fJacX = [1 2 ; 1 1] ; 23 | % 24 | % non-linear model 25 | % fJacX = [0 k 0;-1 x(2) 1;0 0 0]; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_h.m: -------------------------------------------------------------------------------- 1 | function y = template_DNL_h(x,k,u,v,Ts) 2 | %TEMPLATE_DNL_H function handle template for h for model of class ss_DNL 3 | % 4 | % - Input variable(s) - 5 | % X: current state vector 6 | % K: the step number 7 | % U: input vector 8 | % V: meas. noise vector v 9 | % TS: the sample time 10 | % 11 | % - Output variable(s) - 12 | % Y: measurement without noise 13 | % 14 | % - Construction - 15 | % Y = TEMPLATE_DNL_H(X,K,U,V,TS) returns the new state Y with noise v 16 | % at step K with input U and sample time TS 17 | % 18 | 19 | % EXAMPLES: 20 | % linear model 21 | % y(1,1) = x(1) + v(1); 22 | % y(2,1) = x(2) + v(2); 23 | % 24 | % non-linear model 25 | % y = x(1)+x(2)*k + v; 26 | 27 | end 28 | -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_hJacV.m: -------------------------------------------------------------------------------- 1 | function hJacV = template_DNL_hJacV(x,k,u,v,Ts) 2 | %TEMPLATE_DNL_HJACX function handle template for the Jacobian of h 3 | % with respect to V for model of class ss_DNL 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % V: meas. noise vector v 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACV: Jacobian of f with respect to V 14 | % 15 | % - Construction - 16 | % HJACV = TEMPLATE_DNL_HJACV(X,K,U,V,TS) returns Jacobian of f with respect 17 | % to V at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % hJacV = [1 0 ; 0 1] ; 23 | % 24 | % non-linear model 25 | % hJacV = 1; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/datemplates/ss_D_fun_handles/template_DNL_hJacX.m: -------------------------------------------------------------------------------- 1 | function hJacX = template_DNL_hJacX(x,k,u,v,Ts) 2 | %TEMPLATE_DNL_HJACX function handle template for the Jacobian of h 3 | % with respect to X for model of class ss_DNL 4 | % 5 | % - Input variable(s) - 6 | % X: current state vector 7 | % K: the step number 8 | % U: input vector 9 | % V: meas. noise vector v 10 | % TS: the sample time 11 | % 12 | % - Output variable(s) - 13 | % HJACX: Jacobian of f with respect to X 14 | % 15 | % - Construction - 16 | % HJACX = TEMPLATE_DNL_HJACX(X,K,U,V,TS) returns Jacobian of f with respect 17 | % to X at step K, with input U and sample time TS 18 | % 19 | 20 | % EXAMPLES: 21 | % linear model 22 | % hJacX = [1 0 ; 0 1] ; 23 | % 24 | % non-linear model 25 | % hJacX = [1 k 0]; 26 | 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DA_Save_Stats/PF_Save_Stats.m: -------------------------------------------------------------------------------- 1 | function [x,P] = PF_Save_Stats(x_par,wi,xRet,PRet,covFull,resampled) 2 | %PF_SAVE_STATS Returns statistics 3 | % 4 | % - Input variable(s) - 5 | % X: matrix of particles where each column is a state 6 | % 7 | % WI: column vector of weights 8 | % 9 | % XRET: determines whether X needs to be returned. (0: returns empty 10 | % matrix, 1 returns X) 11 | % 12 | % PRET: determines whether P needs to be returned. (0: returns empty 13 | % matrix, 1 returns P) 14 | % 15 | % COVFULL: if COVFULL is 1 the full covariance matrix P is returned, if 16 | % COVFULL is 0 only the diagonal elements are returned. (The variances) 17 | % 18 | % RESAMPLED: indicates whether particles have been resampled. 19 | % (1=resampled, 0= not resampled) 20 | % 21 | % - Output variable(s) - 22 | % X: column vector that contains the weighted mean of the particles. 23 | % 24 | % P: matrix that contains the estimated covariance matrix. 25 | % 26 | % - Construction - 27 | % [X,P] = PF_Save_Stats(X_PAR,WI,XRET,PRET,COVFULL,RESAMPLED) returns the 28 | % weighted mean of the particles X and (co)variance matrix P. 29 | 30 | nStates = size(x_par,1); 31 | nParticles = size(x_par,2); 32 | if ~covFull;P = zeros(nStates,1);end; 33 | 34 | if xRet||PRet 35 | if resampled 36 | x=mean(x_par,2); %estimate x: weighted mean of particles (weights are equal) 37 | else 38 | x=x_par*wi; %estimate x: weighted mean of particles 39 | end 40 | else 41 | x=[]; 42 | end 43 | 44 | if PRet %particle error covariance matrix 45 | Ex=x_par-repmat(x,1,nParticles); 46 | if covFull %full covariance matrix 47 | if resampled 48 | P = (Ex*Ex')/(nParticles-1); 49 | else 50 | P = Ex*(Ex*wi)'/(nParticles-1); 51 | end 52 | else %variance column vector 53 | for j=1:nStates 54 | if resampled 55 | P(j) = sum(Ex(j,:).^2)/(nParticles-1); 56 | else 57 | wi = wi * nParticles /(nParticles-1); 58 | P(j) = sum( (Ex(j,:).^2).*wi'); 59 | end 60 | end 61 | end 62 | else 63 | P=[]; 64 | end 65 | 66 | if ~xRet;x=[];end; 67 | 68 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DA_Save_Stats/P_Save_Stats.m: -------------------------------------------------------------------------------- 1 | function [P] = P_Save_Stats(Ex,covFull) 2 | %P_SAVE_STATS Returns statistics 3 | % 4 | % - Input variable(s) - 5 | % EX: ensemble error matrix 6 | % 7 | % COVFULL: if COVFULL is 1 the full covariance matrix P is returned, if 8 | % COVFULL is 0 only the diagonal elements are returned. (The variances) 9 | % 10 | % - Output variable(s) - 11 | % P: matrix that contains the estimated covariance matrix. 12 | % 13 | % - Construction - 14 | % [P] = P_Save_Stats(Ex,covFull) returns the (co)variance matrix P. 15 | 16 | nStates = size(Ex,1); 17 | nEnsembles = size(Ex,2); 18 | 19 | if covFull %full covariance matrix 20 | P = (Ex*Ex')/(nEnsembles-1); 21 | else %variance column vector 22 | P = zeros(nStates,1); 23 | for j=1:nStates 24 | P(j) = sum(Ex(j,:).^2)/(nEnsembles-1); 25 | end 26 | end 27 | 28 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/ReadMe.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\mac\ansicpg10000\cocoartf102 2 | {\fonttbl\f0\fswiss\fcharset77 Helvetica;} 3 | {\colortbl;\red255\green255\blue255;\red0\green0\blue0;} 4 | \margl1440\margr1440\vieww9000\viewh9000\viewkind0 5 | \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural 6 | 7 | \f0\fs24 \cf2 Numerical differentiation \ 8 | \ 9 | \pard\pardeftab720\ql\qnatural 10 | \cf2 Author: John D'Errico\ 11 | e-mail: woodchips@rochester.rr.com\ 12 | Release: 1.0\ 13 | Release date: 3/7/2007\ 14 | \ 15 | This is a suite of tools to solve automatic numerical differentiation problems in one or more variables. All of these methods also produce error estimates on the result.\ 16 | A pdf file is also provided to explain the theory behind these tools.\ 17 | \ 18 | \ 19 | DERIVEST.m\ 20 | A flexible tool for the computation of derivatives of order 1 through 4 on any scalar function. Finite differences are used in an adaptive manner, coupled with a Romberg extrapolation methodology to provide a maximally accurate result. The user can configure many of the options, changing the order of the method or the extrapolation, even allowing the user to specify whether central, forward or backward differences are used.\ 21 | \ 22 | GRADEST.m\ 23 | Computes the gradient vector of a scalar function of one or more variables at any location.\ 24 | \ 25 | JACOBIANEST.m\ 26 | Computes the Jacobian matrix of a vector (or array) valued function of one or more variables.\ 27 | \ 28 | DIRECTIONALDIFF.m\ 29 | Computes the directional derivative (along some line) of a scalar function of one or more variables.\ 30 | \ 31 | HESSIAN.m\ 32 | Computes the Hessian matrix of all 2nd partial derivatives of a scalar function of one or more variables.\ 33 | \ 34 | HESSDIAG.m\ 35 | The diagonal elements of the Hessian matrix are the pure second order partial derivatives. This function is called by HESSIAN.m, but since some users may need only the diagonal elements, I've provided HESSDIAG.\ 36 | \ 37 | \ 38 | } -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/demo/derivest_demo.m: -------------------------------------------------------------------------------- 1 | % DERIVEST demo script 2 | 3 | % This script file is designed to be used in cell mode 4 | % from the matlab editor, or best of all, use the publish 5 | % to HTML feature from the matlab editor. Older versions 6 | % of matlab can copy and paste entire blocks of code into 7 | % the Matlab command window. 8 | 9 | % DERIVEST is property/value is driven for its arguments. 10 | % Properties can be shortened to the 11 | 12 | %% derivative of exp(x), at x == 0 13 | [deriv,err] = derivest(@(x) exp(x),0) 14 | 15 | %% DERIVEST can also use an inline function 16 | [deriv,err] = derivest(inline('exp(x)'),0) 17 | 18 | %% Higher order derivatives (second derivative) 19 | % Truth: 0 20 | [deriv,err] = derivest(@(x) sin(x),pi,'deriv',2) 21 | 22 | %% Higher order derivatives (third derivative) 23 | % Truth: 1 24 | [deriv,err] = derivest(@(x) cos(x),pi/2,'der',3) 25 | 26 | %% Higher order derivatives (up to the fourth derivative) 27 | % Truth: sqrt(2)/2 = 0.707106781186548 28 | [deriv,err] = derivest(@(x) sin(x),pi/4,'d',4) 29 | 30 | %% Evaluate the indicated (default = first) derivative at multiple points 31 | [deriv,err] = derivest(@(x) sin(x),linspace(0,2*pi,13)) 32 | 33 | %% Specify the step size (default stepsize = 0.1) 34 | deriv = derivest(@(x) polyval(1:5,x),1,'deriv',4,'FixedStep',1) 35 | 36 | %% Provide other parameters via an anonymous function 37 | % At a minimizer of a function, its derivative should be 38 | % essentially zero. So, first, find a local minima of a 39 | % first kind bessel function of order nu. 40 | nu = 0; 41 | fun = @(t) besselj(nu,t); 42 | fplot(fun,[0,10]) 43 | x0 = fminbnd(fun,0,10,optimset('TolX',1.e-15)) 44 | hold on 45 | plot(x0,fun(x0),'ro') 46 | hold off 47 | 48 | deriv = derivest(fun,x0,'d',1) 49 | 50 | %% The second derivative should be positive at a minimizer. 51 | deriv = derivest(fun,x0,'d',2) 52 | 53 | %% Compute the numerical gradient vector of a 2-d function 54 | % Note: the gradient at this point should be [4 6] 55 | fun = @(x,y) x.^2 + y.^2; 56 | xy = [2 3]; 57 | gradvec = [derivest(@(x) fun(x,xy(2)),xy(1),'d',1), ... 58 | derivest(@(y) fun(xy(1),y),xy(2),'d',1)] 59 | 60 | %% Compute the numerical Laplacian function of a 2-d function 61 | % Note: The Laplacian of this function should be everywhere == 4 62 | fun = @(x,y) x.^2 + y.^2; 63 | xy = [2 3]; 64 | lapval = derivest(@(x) fun(x,xy(2)),xy(1),'d',2) + ... 65 | derivest(@(y) fun(xy(1),y),xy(2),'d',2) 66 | 67 | %% Compute the derivative of a function using a central difference scheme 68 | % Sometimes you may not want your function to be evaluated 69 | % above or below a given point. A 'central' difference scheme will 70 | % look in both directions equally. 71 | [deriv,err] = derivest(@(x) sinh(x),0,'Style','central') 72 | 73 | %% Compute the derivative of a function using a forward difference scheme 74 | % But a forward scheme will only look above x0. 75 | [deriv,err] = derivest(@(x) sinh(x),0,'Style','forward') 76 | 77 | %% Compute the derivative of a function using a backward difference scheme 78 | % And a backward scheme will only look below x0. 79 | [deriv,err] = derivest(@(x) sinh(x),0,'Style','backward') 80 | 81 | %% Although a central rule may put some samples in the wrong places, it may still succeed 82 | [d,e,del]=derivest(@(x) log(x),.001,'style','central') 83 | 84 | %% But forcing the use of a one-sided rule may be smart anyway 85 | [d,e,del]=derivest(@(x) log(x),.001,'style','forward') 86 | 87 | %% Control the behavior of DERIVEST - forward 2nd order method, with only 1 Romberg term 88 | % Compute the first derivative, also return the final stepsize chosen 89 | [deriv,err,fdelta] = derivest(@(x) tan(x),pi,'deriv',1,'Style','for','MethodOrder',2,'RombergTerms',1) 90 | 91 | %% Functions should be vectorized for speed, but its not always easy to do. 92 | [deriv,err] = derivest(@(x) x.^2,0:5,'deriv',1) 93 | [deriv,err] = derivest(@(x) x^2,0:5,'deriv',1,'vectorized','no') 94 | 95 | 96 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/demo/html/derivest_demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/dautils/DeriveEst/demo/html/derivest_demo.png -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/demo/html/derivest_demo_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/dautils/DeriveEst/demo/html/derivest_demo_01.png -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/demo/multivariable_calc_demo.m: -------------------------------------------------------------------------------- 1 | % Multivariate calculus demo script 2 | 3 | % This script file is designed to be used in cell mode 4 | % from the matlab editor, or best of all, use the publish 5 | % to HTML feature from the matlab editor. Older versions 6 | % of matlab can copy and paste entire blocks of code into 7 | % the Matlab command window. 8 | 9 | % Typical usage of the gradient and Hessian might be in 10 | % optimization problems, where one might compare an analytically 11 | % derived gradient for correctness, or use the Hessian matrix 12 | % to compute confidence interval estimates on parameters in a 13 | % maximum likelihood estimation. 14 | 15 | %% Gradient of the Rosenbrock function at [1,1], the global minimizer 16 | rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; 17 | % The gradient should be zero (within floating point noise) 18 | [grad,err] = gradest(rosen,[1 1]) 19 | 20 | %% The Hessian matrix at the minimizer should be positive definite 21 | H = hessian(rosen,[1 1]) 22 | % The eigenvalues of h should be positive 23 | eig(H) 24 | 25 | %% Gradient estimation using gradest - a function of 5 variables 26 | [grad,err] = gradest(@(x) sum(x.^2),[1 2 3 4 5]) 27 | 28 | %% Simple Hessian matrix of a problem with 3 independent variables 29 | [H,err] = hessian(@(x) x(1) + x(2)^2 + x(3)^3,[1 2 3]) 30 | 31 | %% A semi-definite Hessian matrix 32 | H = hessian(@(xy) cos(xy(1) - xy(2)),[0 0]) 33 | % one of these eigenvalues will be zero (approximately) 34 | eig(H) 35 | 36 | %% Directional derivative of the Rosenbrock function at the solution 37 | % This should be zero. Ok, its a trivial test case. 38 | [dd,err] = directionaldiff(rosen,[1 1],[1 2]) 39 | 40 | %% Directional derivative at other locations 41 | [dd,err] = directionaldiff(rosen,[2 3],[1 -1]) 42 | 43 | % We can test this example 44 | v = [1 -1]; 45 | v = v/norm(v); 46 | g = gradest(rosen,[2 3]); 47 | 48 | % The directional derivative will be the dot product of the gradient with 49 | % the (unit normalized) vector. So this difference will be (approx) zero. 50 | dot(g,v) - dd 51 | 52 | %% Jacobian matrix of a scalar function is just the gradient 53 | [jac,err] = jacobianest(rosen,[2 3]) 54 | 55 | grad = gradest(rosen,[2 3]) 56 | 57 | %% Jacobian matrix of a linear system will reduce to the design matrix 58 | A = rand(5,3); 59 | b = rand(5,1); 60 | fun = @(x) (A*x-b); 61 | 62 | x = rand(3,1); 63 | [jac,err] = jacobianest(fun,x) 64 | 65 | disp 'This should be essentially zero at any location x' 66 | jac - A 67 | 68 | %% The jacobian matrix of a nonlinear transformation of variables 69 | % evaluated at some arbitrary location [-2, -3] 70 | fun = @(xy) [xy(1).^2, cos(xy(1) - xy(2))]; 71 | [jac,err] = jacobianest(fun,[-2 -3]) 72 | 73 | 74 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/directionaldiff.m: -------------------------------------------------------------------------------- 1 | function [dd,err,finaldelta] = directionaldiff(fun,x0,vec) 2 | % directionaldiff: estimate of the directional derivative of a function of n variables 3 | % usage: [grad,err,finaldelta] = directionaldiff(fun,x0,vec) 4 | % 5 | % Uses derivest to provide both a directional derivative 6 | % estimates plus an error estimates. fun needs not be vectorized. 7 | % 8 | % arguments: (input) 9 | % fun - analytical function to differentiate. fun must 10 | % be a function of the vector or array x0. Fun needs 11 | % not be vectorized. 12 | % 13 | % x0 - vector location at which to differentiate fun 14 | % If x0 is an nxm array, then fun is assumed to be 15 | % a function of n*m variables. 16 | % 17 | % vec - vector defining the line along which to take the 18 | % derivative. Vec should be the same size as x0. It 19 | % need not be a vector of unit length. 20 | % 21 | % arguments: (output) 22 | % dd - scalar estimate of the first derivative of fun 23 | % in the SPECIFIED direction. 24 | % 25 | % err - error estimate of the directional derivative 26 | % 27 | % finaldelta - vector of final step sizes chosen for 28 | % each partial derivative. 29 | % 30 | % 31 | % Example: 32 | % At the global minimizer (1,1) of the Rosenbrock function, 33 | % compute the directional derivative in the direction [1 2] 34 | % It should be 0. 35 | % 36 | % rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; 37 | % [dd,err] = directionaldiff(rosen,[1 1]) 38 | % 39 | % dd = 40 | % 0 41 | % err = 42 | % 0 43 | % 44 | % 45 | % See also: derivest, gradest, gradient 46 | % 47 | % 48 | % Author: John D'Errico 49 | % e-mail: woodchips@rochester.rr.com 50 | % Release: 1.0 51 | % Release date: 3/5/2007 52 | 53 | % get the size of x0 so we can make sure vec is 54 | % the same shape. 55 | sx = size(x0); 56 | if numel(x0)~=numel(vec) 57 | error 'vec and x0 must be the same sizes' 58 | end 59 | vec = vec(:); 60 | vec = vec/norm(vec); 61 | vec = reshape(vec,sx); 62 | 63 | [dd,err,finaldelta] = derivest(@(t) fun(x0+t*vec), ... 64 | 0,'deriv',1,'vectorized','no'); 65 | 66 | end % mainline function end 67 | 68 | 69 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/doc/DERIVEST.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/DA_Toolbox/dautils/DeriveEst/doc/DERIVEST.pdf -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/gradest.m: -------------------------------------------------------------------------------- 1 | function [grad,err,finaldelta] = gradest(fun,x0) 2 | % gradest: estimate of the gradient vector of an analytical function of n variables 3 | % usage: [grad,err,finaldelta] = gradest(fun,x0) 4 | % 5 | % Uses derivest to provide both derivative estimates 6 | % and error estimates. fun needs not be vectorized. 7 | % 8 | % arguments: (input) 9 | % fun - analytical function to differentiate. fun must 10 | % be a function of the vector or array x0. 11 | % 12 | % x0 - vector location at which to differentiate fun 13 | % If x0 is an nxm array, then fun is assumed to be 14 | % a function of n*m variables. 15 | % 16 | % arguments: (output) 17 | % grad - vector of first partial derivatives of fun. 18 | % grad will be a row vector of length numel(x0). 19 | % 20 | % err - vector of error estimates corresponding to 21 | % each partial derivative in grad. 22 | % 23 | % finaldelta - vector of final step sizes chosen for 24 | % each partial derivative. 25 | % 26 | % 27 | % Example: 28 | % [grad,err] = gradest(@(x) sum(x.^2),[1 2 3]) 29 | % grad = 30 | % 2 4 6 31 | % err = 32 | % 5.8899e-15 1.178e-14 0 33 | % 34 | % 35 | % Example: 36 | % At [x,y] = [1,1], compute the numerical gradient 37 | % of the function sin(x-y) + y*exp(x) 38 | % 39 | % z = @(xy) sin(diff(xy)) + xy(2)*exp(xy(1)) 40 | % 41 | % [grad,err ] = gradest(z,[1 1]) 42 | % grad = 43 | % 1.7183 3.7183 44 | % err = 45 | % 7.537e-14 1.1846e-13 46 | % 47 | % 48 | % Example: 49 | % At the global minimizer (1,1) of the Rosenbrock function, 50 | % compute the gradient. It should be essentially zero. 51 | % 52 | % rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; 53 | % [g,err] = gradest(rosen,[1 1]) 54 | % g = 55 | % 1.0843e-20 0 56 | % err = 57 | % 1.9075e-18 0 58 | % 59 | % 60 | % See also: derivest, gradient 61 | % 62 | % 63 | % Author: John D'Errico 64 | % e-mail: woodchips@rochester.rr.com 65 | % Release: 1.0 66 | % Release date: 2/9/2007 67 | 68 | % get the size of x0 so we can reshape 69 | % later. 70 | sx = size(x0); 71 | 72 | % total number of derivatives we will need to take 73 | nx = numel(x0); 74 | 75 | grad = zeros(1,nx); 76 | err = grad; 77 | finaldelta = grad; 78 | for ind = 1:nx 79 | [grad(ind),err(ind),finaldelta(ind)] = derivest( ... 80 | @(xi) fun(swapelement(x0,ind,xi)), ... 81 | x0(ind),'deriv',1,'vectorized','no', ... 82 | 'methodorder',2); 83 | end 84 | 85 | end % mainline function end 86 | 87 | % ======================================= 88 | % sub-functions 89 | % ======================================= 90 | function vec = swapelement(vec,ind,val) 91 | % swaps val as element ind, into the vector vec 92 | vec(ind) = val; 93 | 94 | end % sub-function end 95 | 96 | 97 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/hessdiag.m: -------------------------------------------------------------------------------- 1 | function [HD,err,finaldelta] = hessdiag(fun,x0) 2 | % HESSDIAG: diagonal elements of the Hessian matrix (vector of second partials) 3 | % usage: [HD,err,finaldelta] = hessdiag(fun,x0) 4 | % 5 | % When all that you want are the diagonal elements of the hessian 6 | % matrix, it will be more efficient to call HESSDIAG than HESSIAN. 7 | % HESSDIAG uses DERIVEST to provide both second derivative estimates 8 | % and error estimates. fun needs not be vectorized. 9 | % 10 | % arguments: (input) 11 | % fun - SCALAR analytical function to differentiate. 12 | % fun must be a function of the vector or array x0. 13 | % 14 | % x0 - vector location at which to differentiate fun 15 | % If x0 is an nxm array, then fun is assumed to be 16 | % a function of n*m variables. 17 | % 18 | % arguments: (output) 19 | % HD - vector of second partial derivatives of fun. 20 | % These are the diagonal elements of the Hessian 21 | % matrix, evaluated at x0. 22 | % HD will be a row vector of length numel(x0). 23 | % 24 | % err - vector of error estimates corresponding to 25 | % each second partial derivative in HD. 26 | % 27 | % finaldelta - vector of final step sizes chosen for 28 | % each second partial derivative. 29 | % 30 | % 31 | % Example usage: 32 | % [HD,err] = hessdiag(@(x) x(1) + x(2)^2 + x(3)^3,[1 2 3]) 33 | % HD = 34 | % 0 2 18 35 | % 36 | % err = 37 | % 0 0 0 38 | % 39 | % 40 | % See also: derivest, gradient, gradest 41 | % 42 | % 43 | % Author: John D'Errico 44 | % e-mail: woodchips@rochester.rr.com 45 | % Release: 1.0 46 | % Release date: 2/9/2007 47 | 48 | % get the size of x0 so we can reshape 49 | % later. 50 | sx = size(x0); 51 | 52 | % total number of derivatives we will need to take 53 | nx = numel(x0); 54 | 55 | HD = zeros(1,nx); 56 | err = HD; 57 | finaldelta = HD; 58 | for ind = 1:nx 59 | [HD(ind),err(ind),finaldelta(ind)] = derivest( ... 60 | @(xi) fun(swapelement(x0,ind,xi)), ... 61 | x0(ind),'deriv',2,'vectorized','no'); 62 | end 63 | 64 | end % mainline function end 65 | 66 | % ======================================= 67 | % sub-functions 68 | % ======================================= 69 | function vec = swapelement(vec,ind,val) 70 | % swaps val as element ind, into the vector vec 71 | vec(ind) = val; 72 | 73 | end % sub-function end 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/hessian.m: -------------------------------------------------------------------------------- 1 | function [hess,err] = hessian(fun,x0) 2 | % hessian: estimate elements of the Hessian matrix (array of 2nd partials) 3 | % usage: [hess,err] = hessian(fun,x0) 4 | % 5 | % Hessian is NOT a tool for frequent use on an expensive 6 | % to evaluate objective function, especially in a large 7 | % number of dimensions. Its computation will use roughly 8 | % O(6*n^2) function evaluations for n parameters. 9 | % 10 | % arguments: (input) 11 | % fun - SCALAR analytical function to differentiate. 12 | % fun must be a function of the vector or array x0. 13 | % fun does not need to be vectorized. 14 | % 15 | % x0 - vector location at which to compute the Hessian. 16 | % 17 | % arguments: (output) 18 | % hess - nxn symmetric array of second partial derivatives 19 | % of fun, evaluated at x0. 20 | % 21 | % err - nxn array of error estimates corresponding to 22 | % each second partial derivative in hess. 23 | % 24 | % 25 | % Example usage: 26 | % Rosenbrock function, minimized at [1,1] 27 | % rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; 28 | % 29 | % [h,err] = hessian(rosen,[1 1]) 30 | % h = 31 | % 842 -420 32 | % -420 210 33 | % err = 34 | % 1.0662e-12 4.0061e-10 35 | % 4.0061e-10 2.6654e-13 36 | % 37 | % 38 | % Example usage: 39 | % cos(x-y), at (0,0) 40 | % Note: this hessian matrix will be positive semi-definite 41 | % 42 | % hessian(@(xy) cos(xy(1)-xy(2)),[0 0]) 43 | % ans = 44 | % -1 1 45 | % 1 -1 46 | % 47 | % 48 | % See also: derivest, gradient, gradest, hessdiag 49 | % 50 | % 51 | % Author: John D'Errico 52 | % e-mail: woodchips@rochester.rr.com 53 | % Release: 1.0 54 | % Release date: 2/10/2007 55 | 56 | % parameters that we might allow to change 57 | params.StepRatio = 2.0000001; 58 | params.RombergTerms = 3; 59 | 60 | % get the size of x0 so we can reshape 61 | % later. 62 | sx = size(x0); 63 | 64 | % was a string supplied? 65 | if ischar(fun) 66 | fun = str2func(fun); 67 | end 68 | 69 | % total number of derivatives we will need to take 70 | nx = length(x0); 71 | 72 | % get the diagonal elements of the hessian (2nd partial 73 | % derivatives wrt each variable.) 74 | [hess,err] = hessdiag(fun,x0); 75 | 76 | % form the eventual hessian matrix, stuffing only 77 | % the diagonals for now. 78 | hess = diag(hess); 79 | err = diag(err); 80 | if nx<2 81 | % the hessian matrix is 1x1. all done 82 | return 83 | end 84 | 85 | % get the gradient vector. This is done only to decide 86 | % on intelligent step sizes for the mixed partials 87 | [grad,graderr,stepsize] = gradest(fun,x0); 88 | 89 | % Get params.RombergTerms+1 estimates of the upper 90 | % triangle of the hessian matrix 91 | dfac = params.StepRatio.^(-(0:params.RombergTerms)'); 92 | for i = 2:nx 93 | for j = 1:(i-1) 94 | dij = zeros(params.RombergTerms+1,1); 95 | for k = 1:(params.RombergTerms+1) 96 | dij(k) = fun(x0 + swap2(zeros(sx),i, ... 97 | dfac(k)*stepsize(i),j,dfac(k)*stepsize(j))) + ... 98 | fun(x0 + swap2(zeros(sx),i, ... 99 | -dfac(k)*stepsize(i),j,-dfac(k)*stepsize(j))) - ... 100 | fun(x0 + swap2(zeros(sx),i, ... 101 | dfac(k)*stepsize(i),j,-dfac(k)*stepsize(j))) - ... 102 | fun(x0 + swap2(zeros(sx),i, ... 103 | -dfac(k)*stepsize(i),j,dfac(k)*stepsize(j))); 104 | 105 | end 106 | dij = dij/4/prod(stepsize([i,j])); 107 | dij = dij./(dfac.^2); 108 | 109 | % Romberg extrapolation step 110 | [hess(i,j),err(i,j)] = rombextrap(params.StepRatio,dij,[2 4]); 111 | hess(j,i) = hess(i,j); 112 | err(j,i) = err(i,j); 113 | end 114 | end 115 | 116 | 117 | end % mainline function end 118 | 119 | % ======================================= 120 | % sub-functions 121 | % ======================================= 122 | function vec = swap2(vec,ind1,val1,ind2,val2) 123 | % swaps val as element ind, into the vector vec 124 | vec(ind1) = val1; 125 | vec(ind2) = val2; 126 | 127 | end % sub-function end 128 | 129 | 130 | % ============================================ 131 | % subfunction - romberg extrapolation 132 | % ============================================ 133 | function [der_romb,errest] = rombextrap(StepRatio,der_init,rombexpon) 134 | % do romberg extrapolation for each estimate 135 | % 136 | % StepRatio - Ratio decrease in step 137 | % der_init - initial derivative estimates 138 | % rombexpon - higher order terms to cancel using the romberg step 139 | % 140 | % der_romb - derivative estimates returned 141 | % errest - error estimates 142 | % amp - noise amplification factor due to the romberg step 143 | 144 | srinv = 1/StepRatio; 145 | 146 | % do nothing if no romberg terms 147 | nexpon = length(rombexpon); 148 | rmat = ones(nexpon+2,nexpon+1); 149 | switch nexpon 150 | case 0 151 | % rmat is simple: ones(2,1) 152 | case 1 153 | % only one romberg term 154 | rmat(2,2) = srinv^rombexpon; 155 | rmat(3,2) = srinv^(2*rombexpon); 156 | case 2 157 | % two romberg terms 158 | rmat(2,2:3) = srinv.^rombexpon; 159 | rmat(3,2:3) = srinv.^(2*rombexpon); 160 | rmat(4,2:3) = srinv.^(3*rombexpon); 161 | case 3 162 | % three romberg terms 163 | rmat(2,2:4) = srinv.^rombexpon; 164 | rmat(3,2:4) = srinv.^(2*rombexpon); 165 | rmat(4,2:4) = srinv.^(3*rombexpon); 166 | rmat(5,2:4) = srinv.^(4*rombexpon); 167 | end 168 | 169 | % qr factorization used for the extrapolation as well 170 | % as the uncertainty estimates 171 | [qromb,rromb] = qr(rmat,0); 172 | 173 | % the noise amplification is further amplified by the Romberg step. 174 | % amp = cond(rromb); 175 | 176 | % this does the extrapolation to a zero step size. 177 | ne = length(der_init); 178 | rombcoefs = rromb\(qromb'*der_init); 179 | der_romb = rombcoefs(1,:)'; 180 | 181 | % uncertainty estimate of derivative prediction 182 | s = sqrt(sum((der_init - rmat*rombcoefs).^2,1)); 183 | rinv = rromb\eye(nexpon+1); 184 | cov1 = sum(rinv.^2,2); % 1 spare dof 185 | errest = s'*12.7062047361747*sqrt(cov1(1)); 186 | 187 | end % rombextrap 188 | 189 | 190 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/DeriveEst/jacobianest.m: -------------------------------------------------------------------------------- 1 | function [jac,err] = jacobianest(fun,x0) 2 | % gradest: estimate of the Jacobian matrix of a vector valued function of n variables 3 | % usage: [jac,err] = jacobianest(fun,x0) 4 | % 5 | % 6 | % arguments: (input) 7 | % fun - (vector valued) analytical function to differentiate. 8 | % fun must be a function of the vector or array x0. 9 | % 10 | % x0 - vector location at which to differentiate fun 11 | % If x0 is an nxm array, then fun is assumed to be 12 | % a function of n*m variables. 13 | % 14 | % 15 | % arguments: (output) 16 | % jac - array of first partial derivatives of fun. 17 | % Assuming that x0 is a vector of length p 18 | % and fun returns a vector of length n, then 19 | % jac will be an array of size (n,p) 20 | % 21 | % err - vector of error estimates corresponding to 22 | % each partial derivative in jac. 23 | % 24 | % 25 | % Example: (nonlinear least squares) 26 | % xdata = (0:.1:1)'; 27 | % ydata = 1+2*exp(0.75*xdata); 28 | % fun = @(c) ((c(1)+c(2)*exp(c(3)*xdata)) - ydata).^2; 29 | % 30 | % [jac,err] = jacobianest(fun,[1 1 1]) 31 | % 32 | % jac = 33 | % -2 -2 0 34 | % -2.1012 -2.3222 -0.23222 35 | % -2.2045 -2.6926 -0.53852 36 | % -2.3096 -3.1176 -0.93528 37 | % -2.4158 -3.6039 -1.4416 38 | % -2.5225 -4.1589 -2.0795 39 | % -2.629 -4.7904 -2.8742 40 | % -2.7343 -5.5063 -3.8544 41 | % -2.8374 -6.3147 -5.0518 42 | % -2.9369 -7.2237 -6.5013 43 | % -3.0314 -8.2403 -8.2403 44 | % 45 | % err = 46 | % 5.0134e-15 5.0134e-15 0 47 | % 5.0134e-15 0 2.8211e-14 48 | % 5.0134e-15 8.6834e-15 1.5804e-14 49 | % 0 7.09e-15 3.8227e-13 50 | % 5.0134e-15 5.0134e-15 7.5201e-15 51 | % 5.0134e-15 1.0027e-14 2.9233e-14 52 | % 5.0134e-15 0 6.0585e-13 53 | % 5.0134e-15 1.0027e-14 7.2673e-13 54 | % 5.0134e-15 1.0027e-14 3.0495e-13 55 | % 5.0134e-15 1.0027e-14 3.1707e-14 56 | % 5.0134e-15 2.0053e-14 1.4013e-12 57 | % 58 | % (At [1 2 0.75], jac should be numerically zero) 59 | % 60 | % 61 | % See also: derivest, gradient, gradest 62 | % 63 | % 64 | % Author: John D'Errico 65 | % e-mail: woodchips@rochester.rr.com 66 | % Release: 1.0 67 | % Release date: 3/6/2007 68 | 69 | % get the length of x0 for the size of jac 70 | nx = numel(x0); 71 | 72 | MaxStep = 100; 73 | StepRatio = 2.0000001; 74 | 75 | % was a string supplied? 76 | if ischar(fun) 77 | fun = str2func(fun); 78 | end 79 | 80 | % get fun at the center point 81 | f0 = fun(x0); 82 | f0 = f0(:); 83 | n = length(f0); 84 | if n==0 85 | % empty begets empty 86 | jac = zeros(0,nx); 87 | err = jac; 88 | return 89 | end 90 | 91 | relativedelta = MaxStep*StepRatio .^(0:-1:-25); 92 | nsteps = length(relativedelta); 93 | 94 | % total number of derivatives we will need to take 95 | jac = zeros(n,nx); 96 | err = jac; 97 | for i = 1:nx 98 | x0_i = x0(i); 99 | if x0_i ~= 0 100 | delta = x0_i*relativedelta; 101 | else 102 | delta = relativedelta; 103 | end 104 | 105 | % evaluate at each step, centered around x0_i 106 | % difference to give a second order estimate 107 | fdel = zeros(n,nsteps); 108 | for j = 1:nsteps 109 | fdif = fun(swapelement(x0,i,x0_i + delta(j))) - ... 110 | fun(swapelement(x0,i,x0_i - delta(j))); 111 | 112 | fdel(:,j) = fdif(:); 113 | end 114 | 115 | % these are pure second order estimates of the 116 | % first derivative, for each trial delta. 117 | derest = fdel.*repmat(0.5 ./ delta,n,1); 118 | 119 | % The error term on these estimates has a second order 120 | % component, but also some 4th and 6th order terms in it. 121 | % Use Romberg exrapolation to improve the estimates to 122 | % 6th order, as well as to provide the error estimate. 123 | 124 | % loop here, as rombextrap coupled with the trimming 125 | % will get complicated otherwise. 126 | for j = 1:n 127 | [der_romb,errest] = rombextrap(StepRatio,derest(j,:),[2 4]); 128 | 129 | % trim off 3 estimates at each end of the scale 130 | nest = length(der_romb); 131 | trim = [1:3, nest+(-2:0)]; 132 | [der_romb,tags] = sort(der_romb); 133 | der_romb(trim) = []; 134 | tags(trim) = []; 135 | 136 | errest = errest(tags); 137 | 138 | % now pick the estimate with the lowest predicted error 139 | [err(j,i),ind] = min(errest); 140 | jac(j,i) = der_romb(ind); 141 | end 142 | end 143 | 144 | end % mainline function end 145 | 146 | % ======================================= 147 | % sub-functions 148 | % ======================================= 149 | function vec = swapelement(vec,ind,val) 150 | % swaps val as element ind, into the vector vec 151 | vec(ind) = val; 152 | 153 | end % sub-function end 154 | 155 | % ============================================ 156 | % subfunction - romberg extrapolation 157 | % ============================================ 158 | function [der_romb,errest] = rombextrap(StepRatio,der_init,rombexpon) 159 | % do romberg extrapolation for each estimate 160 | % 161 | % StepRatio - Ratio decrease in step 162 | % der_init - initial derivative estimates 163 | % rombexpon - higher order terms to cancel using the romberg step 164 | % 165 | % der_romb - derivative estimates returned 166 | % errest - error estimates 167 | % amp - noise amplification factor due to the romberg step 168 | 169 | srinv = 1/StepRatio; 170 | 171 | % do nothing if no romberg terms 172 | nexpon = length(rombexpon); 173 | rmat = ones(nexpon+2,nexpon+1); 174 | % two romberg terms 175 | rmat(2,2:3) = srinv.^rombexpon; 176 | rmat(3,2:3) = srinv.^(2*rombexpon); 177 | rmat(4,2:3) = srinv.^(3*rombexpon); 178 | 179 | % qr factorization used for the extrapolation as well 180 | % as the uncertainty estimates 181 | [qromb,rromb] = qr(rmat,0); 182 | 183 | % the noise amplification is further amplified by the Romberg step. 184 | % amp = cond(rromb); 185 | 186 | % this does the extrapolation to a zero step size. 187 | ne = length(der_init); 188 | rhs = vec2mat(der_init,nexpon+2,ne - (nexpon+2)); 189 | rombcoefs = rromb\(qromb'*rhs); 190 | der_romb = rombcoefs(1,:)'; 191 | 192 | % uncertainty estimate of derivative prediction 193 | s = sqrt(sum((rhs - rmat*rombcoefs).^2,1)); 194 | rinv = rromb\eye(nexpon+1); 195 | cov1 = sum(rinv.^2,2); % 1 spare dof 196 | errest = s'*12.7062047361747*sqrt(cov1(1)); 197 | 198 | end % rombextrap 199 | 200 | 201 | % ============================================ 202 | % subfunction - vec2mat 203 | % ============================================ 204 | function mat = vec2mat(vec,n,m) 205 | % forms the matrix M, such that M(i,j) = vec(i+j-1) 206 | [i,j] = ndgrid(1:n,0:m-1); 207 | ind = i+j; 208 | mat = vec(ind); 209 | if n==1 210 | mat = mat'; 211 | end 212 | 213 | end % vec2mat 214 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/PF_Resampling/PFRS_multinom.m: -------------------------------------------------------------------------------- 1 | function [x,w,ind] = PFRS_multinom(x,w,N,~) 2 | %PFRS_MULTINOM applies multinomial resampling. 3 | % 4 | % - Input variable(s) - 5 | % X: the states matrix where each column is a state. 6 | % 7 | % W: the column vector of weights. 8 | % 9 | % N: desired size of resampled distribution. 10 | % 11 | % - Output variable(s) - 12 | % X: resampled states. 13 | % 14 | % W: column vector of weights associated with resampled states X. 15 | % 16 | % - Construction - 17 | % [X,W] = PFRS_MULTINOM(X,W) resamples all the states X and their associated 18 | % weights W using simple random sampling . 19 | % 20 | % [X,W] = PFRS_MULTINOM(X,W,N) returns a resampled distribution of size N. 21 | % 22 | % References: 23 | % [1] Resampling in particle filters - Internship performed at Division of 24 | % Automatic Control Department of Electrical Engineering Linkopings 25 | % University, Sweden - Jeroen D. Hol 26 | 27 | % check inputs 28 | if nargin<3 29 | N = length(w); 30 | end 31 | 32 | % generate ordered uniform random numbers 33 | u = cumprod(rand(1,N).^(1./(N:-1:1))); 34 | u = u(N:-1:1); 35 | 36 | % generatate cumulative weights (ordered) 37 | wc = cumsum(w); 38 | 39 | % indices of the selected states 40 | ind = zeros(1,N); 41 | k = 1; 42 | for i = 1:N 43 | while (wc(:,k)0 71 | [x1,~,ind1]=feval(rs,x,wb,n); 72 | x = [x(:,ind),x1]; 73 | ind=[ind, ind1]; 74 | else 75 | x=x(:,ind); 76 | end 77 | else 78 | if n>0 79 | [~,~,ind1]=feval(rs,x,wb,n); 80 | ind=[ind, ind1]; 81 | end 82 | end 83 | w=ones(N,1)./N; 84 | 85 | end 86 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/PF_Resampling/PFRS_stratisfied.m: -------------------------------------------------------------------------------- 1 | function [x,w,ind] = PFRS_stratisfied(x,w,N,~) 2 | %PFRS_STRATISFIED applies stratisfied resampling. 3 | % 4 | % - Input variable(s) - 5 | % X: the states matrix where each column is a state. 6 | % 7 | % W: the column vector of weights. 8 | % 9 | % N: desired size of resampled distribution. 10 | % 11 | % - Output variable(s) - 12 | % X: resampled states. 13 | % 14 | % W: column vector of weights associated with resampled states X. 15 | % 16 | % - Construction - 17 | % [X,W] = PFRS_STRATISFIED(X,W) resamples all the states X and their 18 | % associated weights W using stratisfied resampling . 19 | % 20 | % [X,W] = PFRS_STRATISFIED(X,W,N) returns a resampled distribution of 21 | % size N. 22 | % 23 | % References: 24 | % [1] Resampling in particle filters - Internship performed at Division of 25 | % Automatic Control Department of Electrical Engineering Linkopings 26 | % University, Sweden - Jeroen D. Hol 27 | 28 | % check inputs 29 | if nargin<3 30 | N = length(w); 31 | end 32 | 33 | % generate ordered uniform numbers 34 | u=((0:N-1)+(rand(1,N)))/N; 35 | 36 | % generatate cumulative weights (ordered) 37 | wc = cumsum(w); 38 | 39 | % indices of the selected states 40 | ind = zeros(1,N); 41 | k = 1; 42 | for i = 1:N 43 | while (wc(k) 0 % Not positive definite: check semi definite 104 | 105 | if reqSemiDef 106 | [V,D] = eig(Sigma); %Sigma= V D V' 107 | 108 | D = diag(D); 109 | tol = eps(max(D)) * length(D); 110 | ind = (abs(D) > tol); 111 | D(~ind) = 0; % set small eigenvalues=0 112 | negs = sum(D<0); % number of negative eigenvalues 113 | 114 | if (negs==0) %no negative eigenvalues 115 | if lower 116 | U = V*diag(sqrt(D)); 117 | else 118 | U = diag(sqrt(D))*V'; 119 | end 120 | code = 1; % Positive semi-definite 121 | else 122 | code = 3; % Indefinite 123 | U = []; 124 | end 125 | else 126 | U = []; 127 | code = 2; % Not positive definite 128 | end 129 | else 130 | code = 0; % Positive definite 131 | end 132 | end 133 | end 134 | 135 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/detSigma.m: -------------------------------------------------------------------------------- 1 | function value = detSigma(Sigma) 2 | %DETSIGMA Calculate determinant of covariance matrix Sigma. 3 | % 4 | % - Input variable(s) - 5 | % SIGMA: a matrix or a vector that contains the diagonal elements of a 6 | % diagonal matrix. 7 | % 8 | % - Output variable(s) - 9 | % VALUE: the value of the determinant of SIGMA. 10 | % 11 | % - Construction - 12 | % DETSIGMA(SIGMA) returns the determinant of SIGMA in a more efficient 13 | % manner than the function det(sigma). 14 | 15 | if min(size(Sigma))==1 %vector provided: assume diagonal matrix 16 | value=prod(Sigma); 17 | elseif isequal(Sigma,diag(diag(Sigma))) %diagonal matrix 18 | value=prod(diag(Sigma)); 19 | elseif isequal(triu(Sigma),Sigma) %upper triangular matrix 20 | value=prod(diag(Sigma)); 21 | elseif isequal(tril(Sigma),Sigma) %lower triangular matrix 22 | value=prod(diag(Sigma)); 23 | else 24 | value=det(Sigma); 25 | end 26 | 27 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/diag3D.m: -------------------------------------------------------------------------------- 1 | function diags = diag3D(x) 2 | %DIAG3D Returns the diagonal elements of 3D array of matrices. 3 | % 4 | % - Input variable(s) - 5 | % X: a 3D array of matrices 6 | % 7 | % - Output variable(s) - 8 | % DIAGS: the diagonal elements. Each column represents the diagonals of 9 | % one matrix from the 3D array. 10 | % 11 | % - Construction - 12 | % DIAGS = DIAG3D(X) returns the diagonal elements of 3D array of matrices. 13 | 14 | z = size(x); 15 | m = z(1); 16 | n = z(2); 17 | mn = m * n; 18 | p = prod(z)/mn; 19 | k = (1:m+1:m*n)'; 20 | k = repmat(k,1,p); 21 | k = bsxfun(@plus,k,(0:(p-1))*mn); 22 | diags = x(k); 23 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/f2str.m: -------------------------------------------------------------------------------- 1 | function str=f2str(func) 2 | %F2STR Function handle to string conversion 3 | % 4 | % - Input variable(s) - 5 | % FUNC: a function handle 6 | % 7 | % - Output variable(s) - 8 | % STR: sting containing the function handle 9 | % 10 | % - Construction - 11 | % STR = F2STR(FUNC) returns a sting containing the function handle FUNC. 12 | 13 | if isempty(func) 14 | str='- empty function -'; 15 | elseif isequal(func,0) 16 | str='- zero function -'; 17 | else 18 | str = func2str(func); 19 | if str(1)~='@' 20 | str=['@',str]; 21 | end 22 | end 23 | 24 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/findArrayVal.m: -------------------------------------------------------------------------------- 1 | function array=findArrayVal(array3D,tIndex,tMethod,t) 2 | %FINDARRAYVAL Find the correct 2D array from a 3D array. 3 | % 4 | % - Input variable(s) - 5 | % ARRAY3D: a 3D array where the third dimension represents different times. 6 | % 7 | % T: requested time value. 8 | % 9 | % TINDEX: time index vector used to indicate which time corresponds to 10 | % each vector or matrix in the 3D arrays. 11 | % 12 | % TMETHOD: string that indicates the rounding/interpolation 13 | % method to retrieve the covariance matrix or mean vector from the 14 | % 3D array at time T while taking into account tIndex. The 15 | % following strings are possible: 16 | % 1) 'low': find 2D array corresponding to the lower bound time in tIndex 17 | % 2) 'high': find 2D array corresponding to the higher bound time in tIndex 18 | % 3) 'near': find 2D array corresponding to the nearest time in tIndex 19 | % 4) 'interp': find interpolated 2D array corresponding to tIndex. When 20 | % T is lower/higher than lowest/highest value in tIndex, the first/last 21 | % element is used. 22 | % 5) 'extrap': find interpolated 2D array corresponding to tIndex. When 23 | % T is lower/higher than lowest/highest value in tIndex, extrapolation 24 | % is applied. 25 | % 26 | % - Output variable(s) - 27 | % ARRAY: the 2D array selected from the 3D array that corresponds to time T. 28 | % 29 | % - Construction - 30 | % ARRAY=FINDARRAYVAL(ARRAY3D,TINDEX,TMETHOD,T) returns a 2D array from the 31 | % 3D array corresponding to time T. To locate the correct 2d array, its index 32 | % is set identical to the index of T in TINDEX. When T is not exactly found 33 | % in TINDEX, the method defined in TMETHOD is used to find the correct 34 | % index. 35 | % 36 | % For example: 37 | % If ARRAY3D is the 2 x 2 x 3 array of [1 2;2 1], [2 3;3 2] and 38 | % [3 4;4 3] and TINDEX=[2 3 5] and T=2.4 than TMETHOD 39 | % 'low' yields [1 2;2 1] 40 | % 'high' yields [2 3;3 2] 41 | % 'near' yields [1 2;2 1] 42 | % 'interp' yields [1.4 2.4;2.4 1.4] 43 | % 'extrap' yields [1.4 2.4;2.4 1.4] 44 | % when T=1, TMETHOD 45 | % 'interp' yields [1 2;2 1] 46 | % 'extrap' yields [0.6 1.6;1.6 0.6] 47 | 48 | narginchk(4, 4); 49 | 50 | switch tMethod 51 | case 'low' 52 | [~,index]=findLowest(tIndex, t); 53 | array=array3D(:,:,index); 54 | case 'high' 55 | [~,index]=findHighest(tIndex, t); 56 | array=array3D(:,:,index); 57 | case 'near' 58 | [~,index]=findNearest(tIndex, t); 59 | array=array3D(:,:,index); 60 | case 'interp' 61 | array=interpolate3D(array3D,tIndex,t); 62 | case 'extrap' 63 | array=interpolate3D(array3D,tIndex,t,1); 64 | end 65 | 66 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/findHighest.m: -------------------------------------------------------------------------------- 1 | function [highVal, index] = findHighest(x, desiredVal) 2 | %FINDHIGHEST find the higher bound value to DESIREDVAL that exists in X 3 | % 4 | % - Input variable(s) - 5 | % X: a vector or matrix 6 | % DESIREDVAL: the desired value in x 7 | % 8 | % - Output variable(s) - 9 | % LOWVAL: the higher bound value 10 | % INDEX: the index where the higher bound value is found 11 | % 12 | % - Construction - 13 | % [LOWVAL, INDEX] = FINDHIGHEST(X, DESIREDVAL) finds the higher bound value 14 | % to DESIREDVAL that exsists in X and returns both value and index. 15 | 16 | x = x(:)'; %% this resizes the matrix 17 | if nargin == 2 18 | index = find(x == desiredVal,1); 19 | if ~isempty(index) 20 | highVal = x(x==desiredVal); 21 | else 22 | index=find(x>=desiredVal,1); 23 | if isempty(index) 24 | index = length(x); 25 | elseif index<1 26 | index=1; 27 | end 28 | highVal=x(index); 29 | end 30 | else 31 | error('DA:dautils:findHighest:argMismatch','You have not entered the correct amount of parameters'); 32 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/findLowest.m: -------------------------------------------------------------------------------- 1 | function [lowVal, index] = findLowest(x, desiredVal) 2 | %FINDLOWEST find the lower bound value to DESIREDVAL that exists in X 3 | % 4 | % - Input variable(s) - 5 | % X: a vector or matrix 6 | % DESIREDVAL: the desired value in x 7 | % 8 | % - Output variable(s) - 9 | % LOWVAL: the lower bound value 10 | % INDEX: the index where the lower bound value is found 11 | % 12 | % - Construction - 13 | % [LOWVAL, INDEX] = FINDLOWEST(X, DESIREDVAL) finds the lower bound value 14 | % to DESIREDVAL that exsists in X and returns both value and index. 15 | 16 | x = x(:)'; %% this resizes the matrix 17 | if nargin == 2 18 | index = find(x == desiredVal,1); 19 | if ~isempty(index) 20 | lowVal = x(x==desiredVal); 21 | else 22 | index=find(x>=desiredVal,1)-1; 23 | if isempty(index) 24 | index = length(x); 25 | elseif index<1 26 | index=1; 27 | end 28 | lowVal=x(index); 29 | end 30 | else 31 | error('DA:dautils:findLowest:argMismatch','You have not entered the correct amount of parameters'); 32 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/findNearest.m: -------------------------------------------------------------------------------- 1 | function [nearVal, index] = findNearest(x, desiredVal) 2 | %FINDNEAREST find the closest value to DESIREDVAL that exists in X 3 | % 4 | % - Input variable(s) - 5 | % X: a vector or matrix 6 | % DESIREDVAL: the desired value in x 7 | % 8 | % - Output variable(s) - 9 | % LOWVAL: the nearest value 10 | % INDEX: the index where the nearest value is found 11 | % 12 | % - Construction - 13 | % [LOWVAL, INDEX] = FINDHIGHEST(X, DESIREDVAL) finds the nearest value 14 | % to DESIREDVAL that exsists in X and returns both value and index. 15 | 16 | x = x(:)'; %% this resizes the matrix 17 | if nargin == 2 18 | if ismember(x,desiredVal) == 1 %% This is the O(1) case 19 | index = find(x == desiredVal); 20 | nearVal = x(x==desiredVal); 21 | else 22 | [~, index] = min(abs(desiredVal-x)); 23 | nearVal = x(index); 24 | end 25 | else 26 | error('DA:dautils:findNearest:argMismatch','You have not entered the correct amount of parameters'); 27 | end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/interpolate3D.m: -------------------------------------------------------------------------------- 1 | function [matrix] = interpolate3D(array3D,index,reqVal,varargin) 2 | %INTERPOLATE3D interpolates to find MATRIX, the values of ARRAY3D 3 | % at the point REQVAL in the INDEX vector. 4 | % 5 | % - Input variable(s) - 6 | % ARRAY3D: a 3D array 7 | % INDEX: the interpolation index for REQVAL 8 | % REQVAL: the desired value in INDEX 9 | % EXTRAPOL: 0=interpolate, 1=extrapolate 10 | % 11 | % - Output variable(s) - 12 | % MATRIX: the interpolated matrix 13 | % 14 | % - Construction - 15 | % [MATRIX] = INTERPOLATE3D(ARRAY3D,INDEX,REQVAL,EXTRAPOL) interpolates or 16 | % extrapolates to find MATRIX, the values of ARRAY3D at the point REQVAL 17 | % in the INDEX vector. 18 | % 19 | % [MATRIX] = INTERPOLATE3D(ARRAY3D,INDEX,REQVAL) interpolates to 20 | % find MATRIX, the values of ARRAY3D at the point REQVAL in the INDEX vector. 21 | 22 | % Note:Matlab uses copy-on-write to avoid making a copy of the input argument inside 23 | % the function workspace until or unless you modify the input argument! So 24 | % no problem to use complete 3D array. 25 | 26 | narginchk(3, 4); 27 | ni=nargin; 28 | 29 | if isempty(array3D)|| isempty(index)|| isempty(reqVal) 30 | error('DA:dautils:findHighest:interpolate3D:parMismatch','Parameter missing.'); 31 | end 32 | 33 | if ni==3;extrapolate=0;end; 34 | if ni==4 35 | if varargin{1}==1 36 | extrapolate=1; 37 | elseif varargin{1}==1 38 | extrapolate=0; 39 | else 40 | error('DA:dautils:findHighest:interpolate3D:parMismatch','Wrong parameter value for interpolate argument.'); 41 | end 42 | end 43 | 44 | [valLow,indLow] = findLowest(index,reqVal); 45 | [valHigh,indHigh] = findHighest(index,reqVal); 46 | 47 | if reqValindex(end) %desiredVal too high 58 | if extrapolate==1 59 | matrixLow=array3D(:,:,end-1); 60 | matrixHigh=array3D(:,:,end); 61 | valLow=index(end-1); 62 | valHigh=index(end); 63 | matrix = matrixHigh+(matrixHigh-matrixLow).*((reqVal-valHigh)./(valHigh-valLow)); 64 | else 65 | matrix=array3D(:,:,end); 66 | end 67 | elseif indLow==indHigh %right on an index value 68 | matrix=array3D(:,:,indHigh); 69 | else %in between: interpolate 70 | matrixLow=array3D(:,:,indLow); 71 | matrixHigh=array3D(:,:,indHigh); 72 | matrix = matrixLow+(matrixHigh-matrixLow).*((reqVal-valLow)./(valHigh-valLow)); 73 | end 74 | 75 | 76 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/isdiag.m: -------------------------------------------------------------------------------- 1 | function value = isdiag (X) 2 | %ISDIAG Check diagonality 3 | % 4 | % - Input variable(s) - 5 | % X: a vector or a matrix 6 | % 7 | % - Output variable(s) - 8 | % VALUE: contains a code for the diagonality of X. 9 | % * VALUE=2 if X is a vector. (it is considered as the diagonal vector of 10 | % the underlying matrix) 11 | % * VALUE=1 if X is a diagonal matrix. 12 | % * VALUE=0 if X is none of the above. 13 | % 14 | % - Construction - 15 | % VALUE = ISDIAG(X) returns a code in VALUE for the diagonality of X. 16 | 17 | N=size(X); 18 | if (N(1)==1 || N(2)==1) && length(N)<3 19 | value=2; 20 | elseif length(N)<3 && isequal(X,diag(diag(X))) 21 | value=1; 22 | else 23 | value=0; 24 | end 25 | 26 | % faster code when huge > 10000 diagonal matrix 27 | % if nnz(A)>size(A,1) %fast precheck 28 | % result=false; 29 | % return 30 | % end 31 | % 32 | % [I,J] = find(A); 33 | % 34 | % if ~isempty(I) 35 | % result = all(I == J); 36 | % else 37 | % result = true; %zero matrix is diagonal 38 | % end -------------------------------------------------------------------------------- /DA_Toolbox/dautils/ismonotonic.m: -------------------------------------------------------------------------------- 1 | function monotonic = ismonotonic(x, strict, direction, dim) 2 | % ISMONOTONIC(X) returns a boolean value indicating whether or not a vector is monotonic. 3 | % By default, ISMONOTONIC returns true for non-strictly monotonic vectors, 4 | % and both monotonic increasing and monotonic decreasing vectors. For 5 | % matrices and N-D arrays, ISMONOTONIC returns a value for each column in 6 | % X. 7 | % 8 | % ISMONOTONIC(X, 1) works as above, but only returns true when X is 9 | % strictly monotonically increasing, or strictly monotonically decreasing. 10 | % 11 | % ISMONOTONIC(X, 0) works as ISMONOTONIC(X). 12 | % 13 | % ISMONOTONIC(X, [], 'INCREASING') works as above, but returns true only 14 | % when X is monotonically increasing. 15 | % 16 | % ISMONOTONIC(X, [], 'DECREASING') works as above, but returns true only 17 | % when X is monotonically decreasing. 18 | % 19 | % ISMONOTONIC(X, [], 'EITHER') works as ISMONOTONIC(X, []). 20 | % 21 | % ISMONOTONIC(X, [], [], DIM) works as above, but along dimension DIM. 22 | % 23 | % NOTE: Third input variable is case insensitive, and partial matching is 24 | % used, so 'd' would be recognised as 'DECREASING' etc.. 25 | % 26 | % EXAMPLE: 27 | % x = [1:4; 6:-2:2 3] 28 | % ismonotonic(x) 29 | % ismonotonic(x, [], 'i') 30 | % ismonotonic(x, [], [], 2) 31 | % 32 | % x = 33 | % 1 2 3 4 34 | % 6 4 2 3 35 | % ans = 36 | % 1 1 1 1 37 | % ans = 38 | % 1 1 0 0 39 | % ans = 40 | % 1 41 | % 0 42 | % 43 | % SEE ALSO: is* 44 | % 45 | % $ Author: Richie Cotton $ $ Date: 2010/01/20 $ $ Version: 1.2 $ 46 | 47 | 48 | %% Basic error checking & default setup 49 | if ~isreal(x) || ~isnumeric(x) 50 | warning('DA:dautils:ismonotonic:badXValue', ... 51 | 'The array to be tested is not real and numeric. Unexpected behaviour may occur.'); 52 | end 53 | 54 | if nargin < 2 || isempty(strict) 55 | strict = false; 56 | end 57 | 58 | if nargin < 3 || isempty(direction) 59 | direction = 'either'; 60 | end 61 | 62 | % Accept partial matching for direction 63 | lendir = length(direction); 64 | if strncmpi(direction, 'increasing', lendir) 65 | testIncreasing = true; 66 | testDecreasing = false; 67 | elseif strncmpi(direction, 'decreasing', lendir) 68 | testIncreasing = false; 69 | testDecreasing = true; 70 | elseif strncmpi(direction, 'either', lendir) 71 | testIncreasing = true; 72 | testDecreasing = true; 73 | else 74 | warning('DA:dautils:ismonotonic:badDirection', ... 75 | 'The string entered for direction has not been recognised, reverting to ''either''.'); 76 | testIncreasing = true; 77 | testDecreasing = true; 78 | end 79 | 80 | if nargin < 4 || isempty(dim) 81 | dim = find(size(x) ~= 1, 1); 82 | if isempty(dim) 83 | dim = 1; 84 | end 85 | end 86 | 87 | %% Test for monotonic increasing 88 | if testIncreasing 89 | if strict 90 | comparison = @gt; 91 | else 92 | comparison = @ge; 93 | end 94 | monotonicAscending = all(comparison(diff(x, [], dim), 0), dim); 95 | else 96 | monotonicAscending = false; 97 | end 98 | 99 | %% Test for monotonic decreasing 100 | if testDecreasing 101 | if strict 102 | fhComparison = @lt; 103 | else 104 | fhComparison = @le; 105 | end 106 | monotonicDescending = all(fhComparison(diff(x, [], dim), 0), dim); 107 | else 108 | monotonicDescending = false; 109 | end 110 | 111 | monotonic = monotonicAscending | monotonicDescending; -------------------------------------------------------------------------------- /DA_Toolbox/dautils/issym.m: -------------------------------------------------------------------------------- 1 | function value = issym(matrix,tol) 2 | %ISSYM Check symmetry 3 | % 4 | % - Input variable(s) - 5 | % MATRIX: Square matrix (mxm) or an array of l square matrices (m x m x l) 6 | % 7 | % TOL: Tolerance to decide on symmetry. If difference between two matrix 8 | % elements is within this tolerance, the elements are considered 9 | % identical. 10 | % 11 | % - Output variable(s) - 12 | % VALUE: Indicates symmetry, 1=symmetric, 0=not symmetric. 13 | % 14 | % - Construction - 15 | % VALUE = ISSYM(MATRIX,TOL) indicates if MATRIX is a symmetric matrix 16 | % within the tolerance TOL. 17 | % 18 | % VALUE = ISSYM(MATRIX) indicates if MATRIX is a symmetric matrix. 19 | % Since the tolerance is not provided, itis determined automatically. 20 | % 21 | 22 | 23 | ni=nargin; 24 | 25 | N=size(matrix); 26 | 27 | if length(N)==2 && N(1)==N(2) %Square matrix 28 | 29 | if ni<2;tol = 10*eps(max(abs(diag(matrix))));end; % no tolerance provided: use 10*eps of max diag element 30 | % also see comment on bottom 31 | value=all(all( abs(matrix - matrix')<=tol )); % value=1: symmetric within tolerance 32 | 33 | elseif length(N)==3 && N(1)==N(2) %3D Square matrix array 34 | 35 | if ni==2 && tol==0 36 | value=isequal(matrix,permute(matrix,[2 1 3])); % tol=0: must be exactly symmetric 37 | else 38 | tols=zeros(1,N(3)); 39 | for i=1:N(3) 40 | tmpmatrix = matrix(:,:,i); 41 | if ni<2 42 | tol = 10*eps(max(abs(diag(tmpmatrix)))); 43 | end 44 | tols(i)=all(all( abs(tmpmatrix - tmpmatrix')<=tol )); 45 | end 46 | value=all(tols); 47 | end 48 | 49 | else 50 | value=0; 51 | end 52 | 53 | end 54 | 55 | %better would be using eps of max of each off-diagonal entry in a 56 | %for loop but would be to extensive. Diag is made as a choise. 57 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/iszeromean.m: -------------------------------------------------------------------------------- 1 | function value = iszeromean(x) 2 | %ISZEROMEAN Check if vector is zero mean (all zeros) 3 | % 4 | % - Input variable(s) - 5 | % X: vector 6 | % 7 | % - Output variable(s) - 8 | % VALUE: Indicates if X is zero mean (1=zero mean, 0=not zero mean) 9 | % 10 | % - Construction - 11 | % VALUE = ISZEROMEAN(X) indicates if X is zero mean. 12 | % 13 | 14 | value = ~any(x); -------------------------------------------------------------------------------- /DA_Toolbox/dautils/numSubplots.m: -------------------------------------------------------------------------------- 1 | function [rows,cols]=numSubplots(n) 2 | %NUMSUBPLTS determines a proper size for subplots in a nonfancy way 3 | % 4 | % - Input variable(s) - 5 | % N: required amount of subplots 6 | % 7 | % - Output variable(s) - 8 | % ROWS:amount of rows 9 | % COLS:amount of columns 10 | % 11 | % - Construction - 12 | % [ROWS, COLS] = NUMSUBPLTS(N) determines a proper size for subplots in a 13 | % nonfancy way. Current maximum =20. (Should be more than enough) 14 | 15 | switch n 16 | case 1; rows=1;cols=1; 17 | case 2; rows=1;cols=2; 18 | case 3; rows=3;cols=1; 19 | case 4; rows=2;cols=2; 20 | case {5,6}; rows=2;cols=3; 21 | case 7,8; rows=2;cols=4; 22 | case 9; rows=3;cols=3; 23 | case {10,11,12}; rows=3;cols=4; 24 | case {13,14,15}; rows=3;cols=5; 25 | case {16,17,18,19,20}; rows=4;cols=5; 26 | otherwise; rows=0;cols=0; 27 | end 28 | 29 | end 30 | 31 | 32 | -------------------------------------------------------------------------------- /DA_Toolbox/dautils/sqrtmSchur.m: -------------------------------------------------------------------------------- 1 | function sqrtm = sqrtmSchur(matrix) 2 | 3 | if isequal(matrix,diag(diag(matrix))) %diagonal matrix 4 | 5 | sqrtm = diag(sqrt(diag(matrix))); 6 | 7 | else 8 | n = length(matrix); 9 | [U, T] = schur(matrix,'complex'); %matrix=UTU' 10 | 11 | if any(diag(T) <= eps) 12 | warning('DA:dautils:sqrtmSchur:notPosDef','Matrix is numerically singular.'); 13 | end 14 | 15 | %R= square root of T 16 | if isequal(T,diag(diag(T))) % Check if T is diagonal. 17 | 18 | R = diag(sqrt(diag(T))); % Square root always exists. 19 | 20 | else 21 | % Compute upper triangular square root R of T, a column at a time. 22 | R = zeros(n); 23 | for j=1:n 24 | R(j,j) = sqrt(T(j,j)); 25 | for i=j-1:-1:1 26 | k = i+1:j-1; 27 | s = R(i,k)*R(k,j); 28 | R(i,j) = (T(i,j) - s)/(R(i,i) + R(j,j)); 29 | end 30 | end 31 | end 32 | 33 | sqrtm = U * R * U'; 34 | end 35 | 36 | end -------------------------------------------------------------------------------- /DA_Toolbox/init.m: -------------------------------------------------------------------------------- 1 | function init(save) 2 | %INIT adds the data assimilation toolbox paths to the Matlab search path 3 | % 4 | % - Input variable(s) - 5 | % SAVE: integer value to indicate whether the paths need to be saved for 6 | % future sessions. (0=do not save) 7 | % 8 | % - Construction - 9 | % INIT(SAVE) adds the data assimilation toolbox paths to the Matlab search 10 | % path and does not save it when SAVE=0. 11 | % 12 | % INIT() adds and saves the data assimilation toolbox paths to the Matlab 13 | % search path. 14 | 15 | if nargin == 0 16 | save = 1; 17 | end 18 | 19 | % add the different folders to the search path 20 | currDir=pwd; 21 | addpath(currDir); 22 | addpath(genpath(strcat(currDir,'\da'))); 23 | addpath(genpath(strcat(currDir,'\dademos'))); 24 | addpath(genpath(strcat(currDir,'\daguis'))); 25 | addpath(genpath(strcat(currDir,'\dahelp'))); 26 | addpath(genpath(strcat(currDir,'\daresource'))); 27 | addpath(genpath(strcat(currDir,'\dautils'))); 28 | addpath(genpath(strcat(currDir,'\datemplates'))); 29 | 30 | if save ~= 0 31 | savepath 32 | end 33 | 34 | clc; 35 | 36 | disp('The Data Assimilation Toolbox has been properly installed. '); 37 | disp(' '); 38 | disp('When first using the toolbox it is recommended to run through'); 39 | disp('the included demos. To see the complete list of the available'); 40 | disp('demos enter ''dademos'' in the command line.'); 41 | disp(' '); 42 | disp('Also the manual of the data assimilation toolbox which can be'); 43 | disp('found under the folder ''dahelp'' is very instructive.'); 44 | disp('Additional information can be found in the help files of the'); 45 | disp('classes and functions. A list of the most important help files'); 46 | disp('can be retrieved by entering ''dahelp'' in the command line.'); 47 | disp(' '); 48 | disp('Finally, several templates of functions are available under the'); 49 | disp('folder datemplates.'); 50 | disp(' '); 51 | disp('We are confident that you will enjoy using the DA Toolbox.'); 52 | 53 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Assimilation Toolbox for Matlab 2 | 3 | Master thesis title: Data assimilation toolbox for Matlab
4 | Master Program: Master in Mathematical Engineering
5 | University: KU Leuven
6 | Academic year : 2012-2013 7 | 8 | Author: Wannes Van den Bossche
9 | Supervisor: Dr. Ir. Oscar Mauricio Agudelo
10 | Promotor: Prof. Dr. Ir. Bart De Moor
11 | 12 | Reference:
13 | W. Van den Bossche, "Data assimilation toolbox for Matlab", Master Thesis (Supervisors: Oscar Mauricio Agudelo, Bart De Moor), Faculty of Engineering, KU Leuven, 2013, 103 p.
14 | 15 | "Note: The code is not being maintained" 16 | 17 | ## Summary 18 | Data assimilation is the common name given to the techniques that combine numerical models and measurements in order to obtain an improved estimation of the state 19 | of a system. In data assimilation it is assumed that both models and measurements 20 | are subject to uncertainties that can be defined as a statistical distribution. The 21 | goal of data assimilation is to combine the knowledge of models, measurements and 22 | uncertainties to obtain a better estimation than either the measurements or the 23 | models alone can provide. The application of this technique arises in many fields 24 | such as weather forecasting, oceanography, space weather forecasting and air quality.

25 | Although data assimilation is a highly active research domain, Matlab does not 26 | have an official data assimilation toolbox. This is why the goal of this thesis has 27 | been to develop a generic data assimilation toolbox for Matlab with at least five 28 | data assimilation schemes to improve the estimations of any given model as defined 29 | by the user. Not only is this initial goal achieved, it has been extended by providing 30 | a wider range of data assimilation schemes together with several possibilities to 31 | configure noise models and state space models. In addition, several tools to analyze 32 | the acquired data assimilation results are incorporated. By applying the Matlab 33 | object-oriented programming approach, the toolbox not only provides the required 34 | generic behaviour, but also maintains a structured framework that can be easily 35 | extended with new algorithms and classes. Furthermore, the interface of the toolbox 36 | is intuitively straightforward since it resembles the ones of official Matlab toolboxes.

37 | The toolbox currently contains data assimilation techniques that range from the 38 | regular Kalman filter up to particles filters. More specifically the data assimilation 39 | toolbox is equipped with the following techniques: the Kalman Filter (KF), the 40 | Extended Kalman Filter (EKF), the Unscented Kalman Filter (UKF), the Ensemble 41 | Kalman Filter (EnKF), the Deterministic Ensemble Kalman Filter (DEnKF), the 42 | Ensemble Transform Kalman Filter (ETKF), the Ensemble Square Root Filter 43 | (EnSRF), the Optimal Interpolation (OI) technique, the Generic Particle filter 44 | (GEN), the Sampling Importance Resampling (SIR) Particle filter and the Auxiliary 45 | Sampling Importance Resampling (ASIR) Particle filter. 46 | 47 | ## Installation 48 | Check Appendix A of the document/file "manuscript_user_guide.pdf". 49 | ## User's guide 50 | Check Chapters 3 and 4 of the document/file "manuscript_user_guide.pdf". 51 | -------------------------------------------------------------------------------- /manuscript_user_guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magudelo/DataAssimilationToolboxMatlab/e57ad7d986eae013056461133e5c3837b4a6807f/manuscript_user_guide.pdf --------------------------------------------------------------------------------