MATLAB: ex2
← ChE 555: Applied Mathematics & Modeling for Chemical Engineers
echo off;
clc;
clear all;
close all;
% Initial value of indepemdent variable (t0)
% Final value of independent variable (tf)
% Initial value of dependent variable (y0)
% Integration error tolerance (tol)
x0 = 0;
xf = 3;
y0 = [0 0];
% Define the integration options
% see ode23 help
options = odeset('RelTol',1e-4,'AbsTol',[1e-5],'Stats','on');
% Integrate by ODE45 the derivative functions
% defined in file EX2F.M and assign the
% solution vector to maxrix [x,y]
[x,y] = ode45 ('ex2f',[x0 xf],y0,options);
% 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]
figure
plot(x,y(:,1),'r-');
hold on
plot(x,y(:,2),'b-');
title('ChE585 EXAMPLE-2'); grid;
xlabel('x-axis'); ylabel('y-axis');
text(0.2,15,'red: y(1), numerical');
text(0.2,14,'blue: y(2), numerical');
hold on
% Plot the analytical solution vector [x,yanalytic]
plot(x,yanalytic(:,1),'ro');
hold on
plot(x,yanalytic(:,2),'bo');
text(0.2,13,'o: analytical');
12 successful steps
0 failed attempts
73 function evaluations

Published with MATLAB® 7.0