Trouble with ODE45 for an array of values
Show older comments
Hi,
I have an equation udC/dx=R, where C and R are arrays. I've written a code but its giving a linear plot. Need help
% Solve ODE for Multi-Species Isothermally
% Constants
u = 1.5;
A = 2.2e10;
Ea = 130;
Ru = 8.3145;
T = 750;
L = 0.12;
N = 39;
% Initial Conditions
Cco(1) = 0.52;
Co2(1) = 0.99;
Ch2o(1) = 0;
delx = L/(N-1);
x = 0:delx:0.12;
C0 = [Cco(1) Co2(1) Ch2o(1)];
C = zeros(3,1);
Rco = -A * (exp(-Ea / (Ru*T))) * Cco * sqrt(Co2);
Ro2 = 0.5 * Rco;
Rh2o = -Rco;
dC = [Rco Ro2 Rh2o] / u;
R = [Rco Ro2 Rh2o];
R = zeros(3,1);
% ODE45 solver
[x,C] = ode45(@(x,C) R/u, x, C0);
plot(x,C,'-+')
title('Molar Concentration vs. Distance');
legend('CO','O2','CO2');
xlabel('Axial(x) Direction [m]');
ylabel('Concentrations [mol/m3]');
Thank you.
Accepted Answer
More Answers (1)
Walter Roberson
on 28 Feb 2017
You have
[x,C] = ode45(@(x,C) R/u, x, C0);
and R and u are constants, 3 x 1 and 1 x 1. The integral of a constant is the straight line a*x + b so all of your lines are linear, not curved.
Note that you have
R = [Rco Ro2 Rh2o];
R = zeros(3,1);
which constructs R and then overwrites it with 0. You might have wanted
R = [Rco Ro2 Rh2o] .';
which would give you lines with different slopes, not curves.
You cannot get a curve unless your function depends upon the time or the input derivatives.
11 Comments
Walter Roberson
on 28 Feb 2017
Your line
[x,C] = ode45(@(x,C) R/u, x, C0);
makes the derivative values independent of x or C. Your function needs to depend upon at least one of the two of those in order to get a curve.
When I glance over your code, my eye is drawn to
Rco = -A * (exp(-Ea / (Ru*T))) * Cco * sqrt(Co2);
which gives me the impression that Rco should depend upon time, since T often means time. But possibly in your case T means Temperature; if so then I would wonder whether maybe temperature should depend upon time?
It would help if you posted the equations so we could look to see whether you correctly went from equation to code.
DIP
on 28 Feb 2017
DIP
on 28 Feb 2017
Walter Roberson
on 28 Feb 2017
Notice in the line after the one that defines u, the numeric values for the C_CO and so on are given with "i=1". Those are initial conditions.
Notice in the definition of R_CO that the C_CO and C_O2 do not have the "i=1". They are not constants: they are functions that could also be written as C_CO(x) and C_O2(x) and then those relate back to the initial u*dC/dx = R where C is a vector with [C_CO, C_O2, C_CO2] and C so is really the derivative of [C_CO, C_O2, C_CO2] with respect to x.
If you go back and write in the (x) everywhere appropriate then it becomes much more clear where the differential equations are.
DIP
on 28 Feb 2017
Torsten
on 28 Feb 2017
[x,C] = ode45(@(x,C) [-A * (exp(-Ea / (Ru*T))) * C(1) * sqrt(C(2)) ; 0.5*(-A * (exp(-Ea / (Ru*T))) * C(1) * sqrt(C(2))) ; -(-A * (exp(-Ea / (Ru*T))) * C(1) * sqrt(C(2)))]/u, x, C0);
Best wishes
Torsten.
Walter Roberson
on 28 Feb 2017
Thanks, Torsten.
DIP
on 28 Feb 2017
DIP
on 28 Feb 2017
Torsten
on 1 Mar 2017
Use a stiff solver, e.g. ODE15S, instead of ODE45.
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!
