MATLAB Example 2
← ChE 555: Applied Mathematics & Modeling for Chemical Engineers
- ChE 585 MATLAB Examples EXAMPLE - 2 In this example we solve the following set of ODEs: ODEs:
I.C.s:
Analytical solution:
The constant:
Program Name: EX2.M clear; % Initial value of indepemdent variable (x0) % Final value of independent variable (xf) % Initial value of dependent variable (y0) % Integration error tolerance (tol) % ------------------------------------------ x0 = 0; xf = 3; y0 = [0 0]; tol = 1.e-3; trace = 0; % % Integrate by ODE45 the derivative functions % defined in file EX2-F.M and assign the % solution vector to maxrix [x,y] % -------------------------------------------- [x,y] = ode45 ('ex2-f',x0,xf,y0,tol,trace); % % Evaluate the analytical solution and assign % the solution vector to vector [yanalytic] % --------------------------------------------- c1 = 1; c2 = 1; yanalytic(:,1) = c1∗exp(x(:))+c2∗exp(-x(:))-2; yanalytic(:,2) = c1∗exp(x(:))-c2∗exp(-x(:))-x(:); % % Plot the numerical solution vector [x,y] % ----------------------------------------- hold; plot(x,y(:,1),'r-'); plot(x,y(:,2),'y-'); title('ChE585 EXAMPLE-2'); grid; xlabel('x-axis'); ylabel('y-axis'); text(0.2,15,'red: y(1), numerical'); text(0.2,14,'yellow: y(2), numerical'); % pause % Strike any key to see the analytical solution % % Plot the analytical solution vector [x,yanalytic] % -------------------------------------------------- plot(x,yanalytic(:,1),'ro'); plot(x,yanalytic(:,2),'yo'); text(0.2,13,'o: analytical'); Function Name: EX2-F.M function dydx = ode (x,y) % Define the derivative vector dydx=[dy1dx dy2dx] dydx = [y(2)+x y(1)+1];