Index exceeds the number of array elements. Index must not exceed 1.

1 view (last 30 days)
function ans = final_project()
CI_CO2 = [0,10];
Y0 = [0.0001,0.0001,0.0001,0.0001,0.0001,0.0001,4.75,0.7];
options = odeset('RelTol', 1e-10,'AbsTol',[1e-10,1e-10,1e-10]);
[CI_CO2,VA] = ode45(@respiratory_model,CI_CO2,Y0,options);
plot(CI_CO2,VA)
function res = respiratory_model(CI_CO2,VA)
MT_CO2 = 0.221; MT_O2 = 0.221; MB_CO2 = 0.042; MB_O2 = 0.042; PB = 760; k1 = 0.107;
k2 = 0.415; k3 = 0.2; k4 = 0.05; k5 = 0.92; h = 820067507; u = -148822662; v = 9.8949e+06;
p = -2.7670e+05; q = 3.3179e+03; r = 43; f = 0.003; s = 2.3; g = 98; W = 0.014; S = 0.024;
a = 99; b = 19.6; d = 0.0008; n = 3; m = 98;
Q = 6; QB = 0.7; QBN = 1; CB_CO2 = 55.9; CB_O2 = 0.110; CA_CO2 = 5.6; CA_O2 = 14.1; CT_CO2 = 54;
CT_O2 = 0.134; CI_O2 = 0.1967;
eq58 = MB_CO2 + QB*(k1*(PB*CA_CO2)^k2) - CB_CO2;
eq59 = MT_CO2 + (Q-QB)*(k1*(PB*CA_CO2)^k2-CT_CO2);
eq60 = QB*(CB_CO2-CT_CO2) + Q*(CT_CO2-k1*(PB*CA_CO2)^k2) + VA*(CI_CO2-CA_CO2);
eq61 = -MB_O2 + k3*QB*((1-exp(-k4*k5*PB*CA_O2))^2 - (1-exp(-k4*PB*CB_O2/S))^2);
eq62 = -MT_O2 + k3*(Q-QB)*((1-exp(-k4*k5*PB*CA_O2))^2 - (1-exp(-k4*PB*CT_O2/S))^2);
eq63 = -k3*QB*((1-exp(-k4*PB*CB_O2/S))^2 - (1-exp(-k4*PB*CT_O2/S))^2) + k3*Q*((1-exp(-k4*PB*CT_O2/S))^2 - (1-exp(-k4*k5*PB*CA_O2))^2) + VA*(CI_O2-CA_O2);
eq64 = -VA + a*(CB_CO2)^(1/k2) - b + d*(m-k5*PB*CA_O2)^n;
eq65 = -QB + W*(h*(CA_CO2)^5 + u*(CA_CO2)^4 +v*(CA_CO2)^3 + p*(CA_CO2)^2 + q*CA_CO2 + r + f*(g-1/k5(PB*CA_O2))^8) + QBN;
res = [eq58; eq59; eq60; eq61; eq62; eq63; eq64; eq65];
end
end
Trying to obtain a plot to see how different CI_CO2 (x-variable) values effect VA (y-variable), which is solved from this system of equations. Any help would be greatly appreciated, thank you!
  2 Comments
DGM
DGM on 27 Nov 2021
This is an indexing operation into a scalar:
(g-1/k5(PB*CA_O2))^8
is this supposed to be a multiplication operation?
(g-1/k5*(PB*CA_O2))^8
or
(g-1/(k5*PB*CA_O2))^8
?
Jack Verderber
Jack Verderber on 27 Nov 2021
(g-1/k5*(PB*CA_O2))^8
This is the correct code, thanks for catching this error. However, now I am getting the following error message:
FINAL_PROJECT/RESPIRATORY_MODEL returns a vector of length 29, but the length of initial conditions vector is 8. The
vector returned by FINAL_PROJECT/RESPIRATORY_MODEL and the initial conditions vector must have the same number of
elements.
Any help with solving this issue is also appreciated. Thank you!

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 28 Dec 2021
Edited: Cris LaPierre on 28 Dec 2021
The issue is that eq60, eq63 and eq64 are returning 8x1 vectors. That adds 24 items to your ouput vector. Plus the scalar values from the other equations gives you 29 items.
This is because you are using your vector of ICs VA in these equations. We don't have the original equations to know what the correct value here should be, but it is likely you are supposed to index just one of the VA values in each equation.

Community Treasure Hunt

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

Start Hunting!