Hi everyone!
It's been a long time since I used MATLAB for the last time. Unfortunatelly, I got rusty and I'm trying to make some examples to brush up my skills. One of this example drove into a bug that I have no idea what can be and how to solve it. I'm trying to use the ODE45 function (Rugge Kutta Method) but I got this error alert.
@(T,Y)TANKMODEL(T,Y) returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by @(T,Y)TANKMODEL(T,Y) and the initial conditions vector must have the same number of elements.
Can someone give me a north to solve this?
My code is:
clc;
close all;
format long
%Rugge Kutta Method
tspan = [0 5];
IC = [0 0];
[t,y] = ode45(@(t,y)tankmodel(t,y),tspan ,IC);
%Grafica
figure
plot(t,y)
title('Carbon Concentration');
xlabel('Time (s)');
ylabel('Concentration mg/m^3');
function dydt = tankmodel(t,y,z)
format long
%%%Dataset Parameters
Wgrill = 500; Qink = 100; Cink = 2;QE = 25;CE = 2;Ve= 840;Vk= 280;Ee= 25;Ek= 25;
Qoutk = Qink;
Qoute = Qink + QE;
% Equations
dydt = ((Wgrill + Qink*Cink - Qoutk*y + Ee*z - Ek*y)/Vk);
y(1)= 0;
dzdt = ((QE*CE + Qoutk*y - Qoute*z + Ek*y - Ee*z)/Ve);
z(1) = 0;
end

 채택된 답변

Star Strider
Star Strider 2018년 12월 10일

1 개 추천

Change your ‘tankmodel’ function to this and it works:
function dydt = tankmodel(t,yv)
format long
%%%Dataset Parameters
Wgrill = 500; Qink = 100; Cink = 2;QE = 25;CE = 2;Ve= 840;Vk= 280;Ee= 25;Ek= 25;
Qoutk = Qink;
Qoute = Qink + QE;
y = yv(1);
z = yv(2);
% Equations
dydt(1,:) = ((Wgrill + Qink*Cink - Qoutk*y + Ee*z - Ek*y)/Vk);
dydt(2,:) = ((QE*CE + Qoutk*y - Qoute*z + Ek*y - Ee*z)/Ve);
end

댓글 수: 3

Thiago Brito
Thiago Brito 2018년 12월 10일
Thank you a lot! Just save my day!
Just one last question. Does it mean that the lenght of the vector is controlled by this part "dydt(1,:)"?
Star Strider
Star Strider 2018년 12월 10일
As always, my pleasure!
I am not certain what you are asking.
The time vector length is controlled by the ‘tspan’ argument.
The ‘y’ vector and the derivative your function returns (here ‘dydt’) are dictated by the function you are integrating. Here, both are (2x1) vectors.
madhan ravi
madhan ravi 2018년 12월 11일
+1 @ Star Strider I learn something in your answers each time :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2018년 12월 10일

댓글:

2018년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by