MATLAB Example 1
← ChE 555: Applied Mathematics & Modeling for Chemical Engineers
- ChE 585 MATLAB Examples EXAMPLE - 1 In this example we solve the following single ODE: ODE:
I.C.:
Analytical solution:
The constant:
Program Name: EX1.M clear; % Initial value of indepemdent variable (t0) % Final value of independent variable (tf) % Initial value of dependent variable (y0) % Integration error tolerance (tol) % ------------------------------------------ t0 = 0; tf = 3; y0 = 0; tol = 1.e-3; trace = 0; % % Integrate by ODE23 the derivative functions % defined in file EX1-F.M and assign the % solution vector to maxrix [t,y] % -------------------------------------------- [t,y] = ode23 ('ex1-f',t0,tf,y0,tol,trace); % % Evaluate the analytical solution and assign % the solution vector to vector [yanalytic] % --------------------------------------------- constant = (y0-2)/exp(-t0^2); yanalytic = 2+constant∗exp(-diag(t)^2); % % Plot the numerical solution vector [t,y] % with red ('r') line ('-') % ----------------------------------------- plot(t,y,'r-'); title('ChE585 EXAMPLE-1'); grid; xlabel('x-axis'); ylabel('y-axis'); text(2.2,0.3,'red: numerical'); % % Hold the current graph to plot the analytical % solution on the same graph % ---------------------------------------------- hold; % pause % Strike any key to see the analytical solution % % Plot the analytical solution vector % [t,diagonal(yanalytic)] with yellow ('y') line ('-') % ----------------------------------------------------- plot(t,diag(yanalytic),'y-'); text(2.2,0.1,'yellow: analytical'); Function Name: EX1-F.M function dydt = ode (t,y) dydt = -2∗t∗y+4∗t;