How do I store the outputs of my ODEs in a structured array?

1 次查看(过去 30 天)
I can plot it but i need to be able to store the value in an array and im not exaxally sure how you do this. Any help would be much appriciated.
function [dAsdt]=rates(tspan, y)
% Rate constants
K1 = 0.4526;
K2 = 0.3958
K3 = 0.3523
%ODE for each spicies in reactor
dA1dt = -K1*y(1)*y(2) -K2*y(1)*y(4) -K3*y(1)*y(5);
dA2dt = -K1*y(1)*y(2);
dA3dt = K1*y(1)*y(2) +K2*y(1)*y(4) +K3*y(1)*y(5);
dA4dt = K1*y(1)*y(2) -K2*y(1)*y(4);
dA5dt = K2*y(1)*y(4) -K3*y(1)*y(5);
dA6dt = K3*y(1)*y(5);
%combine all ODEs into one matrix
dAsdt = [dA1dt; dA2dt; dA3dt; dA4dt; dA5dt; dA6dt]
%%script file to solve ODEs
%setting the initil conditions
y0 = [3 1 0 0 0 0];
% timespan to solve over
tspan = [0 0.2];
%call function solver and pass our system of reaction equations
[tout,yout]=ode45('rates',tspan,y0);
plot(tout, yout(:,1),'r-',tout,yout(:,2),'b--',tout,yout(:,3),'g:',tout,yout(:,4),'c-',tout,yout(:,5),'m--',tout,yout(:,6),'y:')
legend('[MeOH]','[TG]','[BD]','[DG]','[MG]','[GL]')
xlabel('time (sec)')
ylabel('concentration (Kmol/m^3)')
title('Formation of biodiesel reaction')

采纳的回答

Star Strider
Star Strider 2023-3-12
It would be easiest to use a cell array —
[tout,yout]=ode45('rates',tspan,y0);
Results = {t,yout};
This also works in a loop, if for example you wanted to change a parameter between iterations:
for k = 1:N
[tout,yout]=ode45(@(t,y)rates(t,y,p(k),tspan,y0);
Results{k,:} = {t,yout];
end
If you want to save the results to a .mat file, use the save function.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by