├── dynamics_adaptive.m ├── main_file_adaptive.m ├── README.md └── LICENSE.md /dynamics_adaptive.m: -------------------------------------------------------------------------------- 1 | function dxdt = dynamics_adaptive(t, x, um, ym, time_um,am,bm,a,b,gamma) 2 | 3 | um_interp = interp1(time_um, um, t); % Interpolate the data set (time_um, um) at time t 4 | ym_interp = interp1(time_um, ym, t); % Interpolate the data set (time_um, ym) at time t 5 | 6 | dxdt(1,1)=x(2); 7 | dxdt(2,1)=-am*x(2)-gamma*am*x(5)*um_interp+gamma*am*ym_interp*um_interp; 8 | dxdt(3,1)=x(4); 9 | dxdt(4,1)=-am*x(4)+gamma*am*x(5)^2-gamma*am*x(5)*ym_interp; 10 | dxdt(5,1)=-a*x(5)+b*x(1)*um_interp-b*x(3)*x(5); 11 | 12 | -------------------------------------------------------------------------------- /main_file_adaptive.m: -------------------------------------------------------------------------------- 1 | % MATLAB simulation of a model reference adaptive controller 2 | % Author: Aleksandar Haber 3 | % Date: April 21, 2021 4 | 5 | clear, pack, clc 6 | 7 | % reference model parameters 8 | 9 | am=2 10 | bm=2 11 | 12 | % plant parameters (that are unknown during control design) 13 | a=1 14 | b=0.5 15 | 16 | % final values of the parameters for verification of convergence 17 | theta1final=bm/b 18 | theta2final=(am-a)/b 19 | 20 | % reference model 21 | Wm=tf([bm],[1 am]) 22 | 23 | tmax=100 % max simulation time 24 | time=0:0.001:tmax; %time vector 25 | 26 | % define a reference input signal 27 | pulsew = 10; %pulse width 28 | delayop= pulsew/2:pulsew*2:tmax; %delay vector 29 | % reference input 30 | ur=2*pulstran(time,delayop,'rectpuls',pulsew)-1; 31 | 32 | % input reference signal 33 | figure(1) 34 | plot(time,ur); 35 | set(gca,'Ylim',[-1.5 1.5]); 36 | 37 | % output reference signal 38 | yr=lsim(Wm,ur,time) 39 | 40 | figure(2) 41 | plot(time,yr,'r') 42 | hold on 43 | plot(time,ur,'k') 44 | 45 | 46 | % initial condition 47 | x0=zeros(5,1); 48 | % gain 49 | gamma2=0.5; 50 | 51 | [time1 state_trajectories] = ode45(@(t,x) dynamics_adaptive(t, x, ur, yr, time,am,bm,a,b,gamma2), time, x0); 52 | 53 | theta1=state_trajectories(:,1) 54 | theta2=state_trajectories(:,3) 55 | y=state_trajectories(:,5) 56 | 57 | 58 | figure(3) 59 | plot(time,yr,'k') 60 | hold on 61 | plot(time,y,'m') 62 | 63 | figure(4) 64 | plot(time,theta1,'k') 65 | hold on 66 | plot(time,theta2,'m') 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Model-Reference-Adaptive-Control---MIT-Rule 2 | 3 | **IMPORTANT NOTE: First, thoroughly read the license in the file called LICENSE.md!** 4 | 5 | In this repository, I posted MATLAB codes for simulating the behavior of a Model Reference Adaptive Control (MRAC) algorithm. 6 | A detailed post that explains the basics of the MRAC algorithm is given here: 7 | https://aleksandarhaber.com/simulation-of-model-reference-adaptive-controller-in-matlab-part-i-mit-rule-and-a-first-order-system/ 8 | 9 | A YouTube video explaining the MRAC algorithm is given here 10 | https://www.youtube.com/watch?v=5xLbtyN5XBU 11 | 12 | The files are: 13 | 1.) "main_file_adaptive.m" - the main file for simulating the dynamics. You can start from this file. 14 | 15 | 2.) "dynamics_adaptive.m" - the MATLAB function describing the closed-loop dynamics. 16 | 17 | 18 | LICENSE: 19 | THIS IS NOT FREE SOFTWARE AND CODE. IF YOU WANT TO USE THIS CODE IN THE COMMERCIAL SETTING OR ACADEMIC SETTING, THAT IS, IF YOU WORK FOR A COMPANY OR IF YOU ARE AN INDEPENDENT CONSULTANT AND IF YOU WANT TO USE THIS CODE OR IF YOU ARE ACADEMIC RESEARCHER OR STUDENT, THEN WITHOUT MY PERMISSION AND WITHOUT PAYING THE PROPER FEE, YOU ARE NOT ALLOWED TO USE THIS CODE. YOU CAN CONTACT ME AT 20 | 21 | aleksandar.haber@gmail.com 22 | 23 | TO INFORM YOURSELF ABOUT THE LICENSE OPTIONS AND FEES FOR USING THIS CODE. 24 | ALSO, IT IS NOT ALLOWED TO 25 | 26 | (1) MODIFY THIS CODE IN ANY WAY WITHOUT MY PERMISSION. 27 | 28 | (2) INTEGRATE THIS CODE IN OTHER PROJECTS WITHOUT MY PERMISSION. 29 | 30 | DELIBERATE OR INDELIBERATE VIOLATIONS OF THIS LICENSE WILL INDUCE LEGAL ACTIONS AND LAWSUITS. 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | LICENSE AND COPYRIGHT NOTICE 2 | 3 | This code repository and all listed files are the ownership of Dr. Aleksandar Haber. Dr. Aleksandar Haber is referred to as the Author in the sequel. All the files on this repository, including all the supporting files on this repository, are referred to as the material in the sequel. 4 | The Author's contact is ml.mecheng@gmail.com 5 | 6 | (1) You have the right to download, share, fork, and use the material for personal educational use without modifying, upgrading, or integrating the material into other projects. You cannot remix, transform, or build upon the material, meaning you can only share the original work without any adaptations. That is, you have the right to use the material solely in its original and unaltered form. 7 | 8 | (2) The material should not be used for commercial purposes without explicit approval of the Author and/or paying an appropriate fee. 9 | 10 | (3) The material should not be used by engineers working in companies as a tool for their work without the explicit approval of the Author and/or paying an appropriate fee. 11 | 12 | (4) The material should not be used by academic staff (paid researchers, paid grad students, paid post-docs, paid professors, etc.) working in universities or research institutions as a tool in their research or for teaching other people without explicit approval of the Author and/or paying an appropriate fee. 13 | 14 | (5) The material should not be used for producing results in academic papers, engineering reports, books, and similar material without explicit approval of the author and possibly without paying an appropriate fee. 15 | 16 | (6) The material or parts of the material should not be copied and published in other commercial or open-source projects. The material should not be used to build other programs without the explicit approval of the Author and without paying an appropriate fee. 17 | 18 | (7) You must give appropriate credit to the Author and reference the Author and material. The citation and the reference must contain the Author’s name, the link, the name of the repository, and the date of accessing the code. 19 | 20 | 21 | --------------------------------------------------------------------------------