MATLAB Example 4

← ChE 555: Applied Mathematics & Modeling for Chemical Engineers

  • ChE 585 MATLAB Examples EXAMPLE - 4 In this example we solve the following set of 2nd order ODEs: ODEs: fortex19 I.C.s: fortex20 B.C.s: fortex21 Analytical solution: fortex22 The constants can be determined using the I.C.s and B.C.s (solution of 4 nonlinear algebraic equations). Since the ODE solvers we have are IVP (Initial Value Problem) solvers for first order ODEs, in order to solve the set of second order ODEs (BVP) we have to convert them into four first order ODEs. fortex23 Then, the original set of 2nd order ODEs can be written as a set of 1st order ODEs: fortex24 Now, the initial conditions on x & y are known as fortex25 . On the other hand, the initial conditions on z2 and z4 are not known (i.e., the original problem does not state fortex26 explicitly). However, the boundary conditions on x & y (i.e., fortex27 ) can be used to determine the proper initial conditions on z2 and z4 (e.g., by trial and error). So, by trial and error, the proper initial conditions are fortex28 . (The exact initial conditions on z2 and z4 could be determined from the analytical solution as well.) Program Name: EX4.M clear; % Initial value of indepemdent variable (x0) % Final value of independent variable (xf) % Initial value of dependent variable (y0) % Integration error tolerance (tol) % ------------------------------------------ t0 = 0; tf = 1; z0 = [0 0.5871 0 0.0228]; tol = 1.e-3; trace = 0; % % Integrate by ODE45 the derivative functions % defined in file EX4-F.M and assign the % solution vector to maxrix [t,z] % -------------------------------------------- [t,z] = ode45 ('ex4-f',t0,tf,z0,tol,trace); % % Plot the numerical solution vector [t,z] % ----------------------------------------- hold; plot(t,z(:,1),'r-'); plot(t,z(:,3),'y-'); title('ChE585 EXAMPLE-4'); grid; xlabel('x-axis'); ylabel('y-axis'); text(0.1,0.85,'red: z(1)=x, numerical'); text(0.1,0.75,'yellow: z(3)=y, numerical'); Function Name: EX4-F.M function dzdt = ode (t,z) % Define the derivative vector % dzdt=[dz1dt dz2dt dz3dt dz4dt] dzdt = [z(2) z(1)+2∗z(3)+t z(4) 3∗z(1)+2∗z(3)+1];