# How to create a loop to run my ODE for two sets of data and then store each data set in two cell arrays.

5 Ansichten (letzte 30 Tage)
Jacob am 14 Mär. 2023
Kommentiert: Jan am 15 Mär. 2023
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]
the initial script with the ODEs called rates.
Y0 = [3 1 0 0 0]
Y1 = [4 2 0 0 0]
tspan = [0:0.1:5];
[tout,yout]=ode45('rates',tspan,y0);
I want to Run this part for both Y0 and then Y1 and store each set of values but don't know how.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 14 Mär. 2023
I do not see the problem:
Y0 = [3 1 0 0 0]
Y1 = [4 2 0 0 0]
tspan = 0:0.1:5;
[tout{1}, yout{1}] = ode45(@rates, tspan, Y0);
[tout{2}, yout{2}] = ode45(@rates, tspan, Y1);
Notes:
• 0:0.1:5 is a vector already. [ ] is Matlab's operator for a concatenation. In [0:0.1:5] you concate the vector 0:0.1:5 with nothing, so this is a (tiny) waste of time.
• Providing the function to be integrated as CHAR vector is outdated for over 20 years now. Use "modern" function handles instead.
##### 2 KommentareKeine anzeigenKeine ausblenden
Jacob am 14 Mär. 2023
I'm confused as what you mean by providing a function as a CHAR vector as I'm very new to Matlab.
Jan am 15 Mär. 2023
ode45('rates',tspan,y0)
% ^^^^^^^ function is provided as CHAR, outdated sind R6.5 (2002)
ode45(@rates, tspan, Y0)
% ^^^^^^ function is provided as function handle
Where did you got the example with using a CHAR vector from? Prefer to learn Matlab from modern sources.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by