initial condition vector is 4 but returns a vector of 1

4 visualizaciones (últimos 30 días)
Ted Kim
Ted Kim el 1 de Nov. de 2020
Comentada: Walter Roberson el 1 de Nov. de 2020
Error using odearguments (line 95)
CT_ACETONE_EXAM2 returns a vector of length 1, but the length of initial conditions vector is 4. The vector returned by CT_ACETONE_EXAM2 and the initial conditions vector must have the same number of elements.
Error in ode15s (line 150)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in CT_Acetone_exam2_DAE (line 21)
[t,y] = ode15s(@CT_Acetone_exam2, tspan, y0, Options);
function UV = CT_Acetone_exam2 (t,y)
% Beer Lambert Data
phi = 155; %Quantum Yield (mol/Ein)
Io = -1.225806452E7; %Intensity = k3/-phi **zero order kinetics**
f = 1; %Acetone is the only Chromophore
ep = 8.0; %Molar absorption coefficient (M-1cm-1)
L = 2.54; %Path length (cm)
PROP = 2; %Concentration of Propanol - 2 mol/L
% Kinetic constant
k2 = 2.5E10; % M-1 sec-1
k3 = 1.9E9; % M-1 sec-1
UV =[-phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)))+k3.*y(3).*y(4)+(phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)))-k2.*y(2).*PROP)+(2*k2.*y(2).*PROP-k3.*y(3).*y(4))-(k3.*y(4).*y(3))];
tic; %Clock starts
clc
clf
clear all
AC = 69E-3; %Initial conc in moles/L
CT = 3E-3; %Target Initial concentration
tspan = [0 60];
y0 = [AC 0 0 CT];
% Option: Tolerances (AC, AC*, RAD, CT, Abs)
M = diag([1 1 1 1 1]);
Options = odeset('Mass', M,'Reltol', 1E-8, 'AbsTol', [1E-6 1E-20 1E-16 1E-9]);
% ODE's Stiff solver
[t,y] = ode15s(@CT_Acetone_exam2, tspan, y0, Options);
% Plotting results
hold all
figure(1)
plot(t,1E3*y(:,3),'-','LineWidth',3,'Color',[0 1 0]);
xlabel('time (sec)');ylabel('AC* (mM)');
title('RAD vs Time');
figure(2)
plot(t,1E3*y(:,4),'-','LineWidth',3,'Color',[1 0 1]);
xlabel('time (sec)');ylabel('CT (mM)');
title('CT vs Time');
toc; %Clock Stops

Respuestas (1)

Walter Roberson
Walter Roberson el 1 de Nov. de 2020
Editada: Walter Roberson el 1 de Nov. de 2020
The message is correct.
Your odefun uses four states, y(1), y(2), y(3), y(4).
Your odefun must return a column vector
[dy(1)/dt; dy(2)/dt; dy(3)/dt; dy(4)/dt]
  6 comentarios
Walter Roberson
Walter Roberson el 1 de Nov. de 2020
y(1) = -phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)));
y(2) = phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)))-k2.*y(2).*(PROP);
y(3) = 2*k2.*y(2).*(PROP)-k3.*y(3).*y(4);
y(4) = -k3.*y(4).*y(3);
At this point y exists and is the boundary conditions input. You then alter every component of the vector, in some cases using the old (input) values but in other cases using the values you just computed. This is confusing for the readers, who would have good reason to doubt that the code has been implemented properly. If you need a vector of coefficients calculated from the inputs, then use a different variable name to store the values, so as to clearly distinguish between old and new values.
Walter Roberson
Walter Roberson el 1 de Nov. de 2020
Your output is not y. Your output is UV
UV = [-phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)))+k3.*y(3).*y(4)+phi.*Io.*f.*(1-exp(-2.303.*ep.*L.*y(1)))-k2.*y(2).*(PROP)+2*k2.*y(2).*(PROP)-k3.*y(3).*y(4)-k3.*y(4).*y(3)];
and although you wrote that with [] as if you are producing a vector output, you are only calculating a scalar there.
Your CT_ACETONE_exam2 must return a column vector [dy(1)/dt; dy(2)/dt; dy(3)/dt; dy(4)/dt]
No matter how complicated the calculation is, this is required.
You look to be doing some kind complicated chemical reactions. If the four boundary conditions are concentrations of four different species, then the four outputs would be the derivatives of how those species each change.

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by