ODE45 error must return column vector

조회 수: 3 (최근 30일)
Brianna Biondo
Brianna Biondo 2023년 3월 11일
댓글: Walter Roberson 2023년 3월 11일
No matter what I do I keep receiving the error of a column vector. I have tried making it all zeros first befort completing the function to create a column vector but nothing is working. Any insight would be helpful.
CO(2,1) = 10;
tRange(2,1) = 10;
[tSol,CSol] = ode45(@ConvFunction,tRange,CO);
Error using odearguments
CONVFUNCTION must return a column vector.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
plot(tSol,CSol(:,1))
function F = ConvFunction(t,~)
CaO=10;
k1=.45;
k2=.05;
Ca=CaO*exp(-t*(k1+k2));
dCadt=-(k1*(Ca)-(k2*(Ca)));
dCbdt=k1*(Ca);
dCcdt=k2*(Ca);
dFdt = zeros(3,1);
dFdt = [dCadt dCbdt dCcdt];
F= dFdt;
end

채택된 답변

Paul
Paul 2023년 3월 11일
Try doing what the error message says and make sure that F is a column vector. Here's one option
dFdt = [dCadt ; dCbdt ; dCcdt];
Also, the code is only specifying two initial conditions. It needs to specify three.
  댓글 수: 2
Brianna Biondo
Brianna Biondo 2023년 3월 11일
편집: Brianna Biondo 2023년 3월 11일
Thank you for the help, im getting this as an answer now. Sorry I am newer to matlab and am getting really confused.
CO(2,1) = 10;
tRange(2,1) = 10;
[tSol,CSol,~] = ode45(@ConvFunction,tRange,CO);
Error using odearguments
CONVFUNCTION returns a vector of length 3, but the length of initial conditions vector is 2. The vector returned by CONVFUNCTION and the initial conditions vector must have the same number of
elements.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
plot(tSol,CSol(:,1))
function F = ConvFunction(t,~,~)
CaO=10;
k1=.45;
k2=.05;
Ca=CaO*exp(-t*(k1+k2));
dCadt=-(k1*(Ca)-(k2*(Ca)));
dCbdt=k1*(Ca);
dCcdt=k2*(Ca);
dFdt = zeros(3,1);
dFdt = [dCadt;dCbdt;dCcdt];
F= dFdt;
end
Walter Roberson
Walter Roberson 2023년 3월 11일
Unless you have assigned something to CO before this,
CO(2,1) = 10;
creates CO as a column vector with exactly two values. You are passing that vector of length 2 as the initial state. You ignore the state inside ConvFunction and return a vector of length 3 . The length of the vector you return must be the same as the number of input values.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by