MATLAB: How to solve this vector lenght problem

errorMATLAB and Simulink Student Suiteode45vector length

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

Best Answer

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
Related Question