% example of integrating two coupled ODE's with matlab % A -> B -> C irreversible rxn in isothermal, isobaric PFR % c holds the values of the dependent variables (concentrations) % the first column of c holds the values of the concentration of A % the second column of c holds the values of the concentration of B % tau is the independent variable % SPECIAL NOTE: In many cases, your dependent variables will have different % names. For example, concentration of A and temperature. In such a % case, pick a general name such as "y" for the matrix that holds the dependent % variables. Then, the first column in y would hold the concentration % values, and the second column of y would hold the temperature values. % Write some comments defining the relationships and use these definitions in % the main m-file and the m-file in which the derivatives are defined. % declare "global" variables whose values are shared with file "deriv02.m" global k1 k2 k1 = 1; % rate constant of A -> B used in "deriv02.m" k2 = 1; % rate constant of B -> C used in "deriv02.m" tauSpan = [0 10]; % set span of tau c0 = [1; 0]; % set initial cond., cA = 1, cB = 0 at tau = 0 % call ode45 integrator % 'deriv02' is defined in the file "deriv02.m" % this is a user-written "m" file containing the % derivative equations [tau,c] = ode45('deriv02', tauSpan, c0); % all the integration is finished now, so plot and save results % pull individual cocentration columns out cA = c(:,1); % concentration of A cB = c(:,2); % concentration of B plot(tau,cA,'b',tau,cB,'r') title('example of integrating two ODEs') xlabel('tau (space time)') ylabel('concentration') % put curve legends in color % see "text" and "ColorSpec" in MATLAB manual h = text(5,.6,'A'); set(h,'color','b') h = text(5,.4,'B'); set(h,'color','r') info = 'press any key to continue' pause % put tau and concentrations into one array % so disk file will be in correct format for loading % into graphing program u = [tau, c]; info = 'Saving data tab-delimited text file "data02.temp"' % col 1 = tau, col 2 = conc of A, col 3 = conc of B. % IMPORT as Text file with Delimeter = Tab save data02.temp u -ascii -tabs info = '*** ex02 finished ***'