Errors in ode45 system of equations
Show older comments
Hey everyone, I am trying to solve a system of 5 differential equations using ode45. To begin with I had recursion errors, so I tried to fix it by creating a subfunction, however that created more errors. Since I have many equations that define each other, I am not sure how to order my code. The way it is ordered now I get an "undefined function" error and a bunch of syntax errors that I did not get before. Thank You in advance
P.S. My code is in the attached txt file
Answers (1)
Torsten
on 26 Feb 2016
Try this:
function main
[t,y]=ode45(@Second,[0 20],[0 502 305 0 0],options);
function dy=Second(t,y)
%Initial Condition
F=7.0; %Feed (kmol/s)
%Feed Pressure
yo(2)=305; %bar
%Feed Temperature
yo(1)=502; %K
%Physical Properties
Ar=1; %m^2
Pc=1400;
R=8.314;
d=7.04*10^(-3);
%Some Constants
A=0.55*exp(36696/(y(1)*R));
B=3453.38;
C=0.499*exp(17197/(y(1)*R));
D=(6.62*(10^(-11))*exp((124119)/(R*y(1))));
E=(0.61*(10^10))*exp((-94765)/(R*y(1)));
%Compositions
Fco=(F*0.25-y(4));
Fhh=(F*0.71-3*y(3)+y(4));
Fchoh=(F*0.01+y(3));
Fcoo=(F*0.01-y(3)+y(4));
Fhho=(F*0.02+y(3)-y(4));
Yco=Fco/(Fco+Fhh+Fchoh+Fcoo+Fhho);
Yhh=Fhh/(Fco+Fhh+Fchoh+Fcoo+Fhho);
Ymeth=Fchoh/(Fco+Fhh+Fchoh+Fcoo+Fhho);
Ycoo=Fcoo/(Fco+Fhh+Fchoh+Fcoo+Fhho);
Yhho=Fhho/(Fco+Fhh+Fchoh+Fcoo+Fhho);
K1=10^((3066/y(1))-10.592);
K2=10^((2073/y(1))-2.029);
%Rates
r1=(A*Ycoo*y(2)*Yhh*y(2)*(1-((Ymeth*y(2)*Yhho*y(2))/(K1*Ycoo*y(2)*(Yhh*y(2))^3))))/((((1+((B*Yhho*y(2))/(Yhh*y(2)))+(C*(Yhh)*y(2))^(0.5))+(D*Yhho*y(2)))^3));
r2=(E*Ycoo*y(2)*(1-(K2*Yco*y(2)*Yhho*y(2))/(((Ycoo*y(2)*(Yhh)*y(2)))))/((1+((B*Yhho*y(2))/(Yhh*y(2)))+(C*Yhh*y(2))^(0.5))+(D*Yhho*y(2))));
%Mass Flux
G=F*(Yco*28+Yhh*2+Ymeth*32+Ycoo*44+Yhho*18);
%Average Molecular Weight
u=(Yco*28+Yhh*2+Ymeth*32+Ycoo*44+Yhho*18);
%Heat Capacities
aco=28.142;
bco=0.167*10^(-2);
cco=-1.217*10^(-5);
dco=-8.033*10^(-9);
ahh=29.087;
bhh=-0.191*10^(-2);
chh=0.4*10^(-5);
dhh=-0.87*10^(-9);
achoh=19.037;
bchoh=9.146*10^(-2);
cchoh=-1.217*10^(-5);
dchoh=-8.033*10^(-9);
acoo=19.8;
bcoo=7.344*10^(-3);
ccoo=-5.602*10^(-5);
dcoo=17.15*10^(-9);
ahho=32.217;
bhho=0.192*10^(-2);
chho=1.055*10^(-5);
dhho=-3.593*10^(-9);
Cpco=aco+bco*y(1)+cco*(y(1)^2)+dco*y(1)^3;
Cphh=ahh+bhh*y(1)+chh*(y(1)^2)+dhh*y(1)^3;
Cpchoh=achoh+bchoh*y(1)+cchoh*(y(1)^2)+dchoh*y(1)^3;
Cpcoo=acoo+bcoo*y(1)+ccoo*(y(1)^2)+dcoo*y(1)^3;
Cphho=ahho+bhho*y(1)+chho*(y(1)^2)+dhho*y(1)^3;
%Overall Cp
Cp=Yco*Cpco+Yhh*Cphh+Ymeth*Cpchoh+Ycoo*Cpcoo+Yhho*Cphho;
function dy=Second(t,y)
%Column vector
dy=zeros(5,1);
%Differential Functions
%dT/dz
dy(1)=-(r1*-48980+r2*-41000)/(Cp);
%dP/dz
dy(2)= -((1.75*R*y(1)*(G^2)*(1-0.6))/(d*u*y(2)*0.6^3));
%dE1/dz
dy(3)=r1*Pc*0.6;
%dE2/dz
dy(4)=r2*Pc*0.6;
%dMc/dz
dy(5)=Pc*0.6;
Best wishes
Torsten.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!