Using ode45() to find c when t is from 0 to 10 seconds.

1 visualizzazione (ultimi 30 giorni)
Hi,
I would like to use ode45() for the function dc/dt = -k*c^n to find c when t is between 0 and 10 seconds for the different intial conditions of c0 = 1,2,4,6,8 and 10.
I then aim to plot c against t for each initial condition.
This is the code that I have written so far, but I keep getting errors when using ode45 and I'm not sure whether to make dc/dt a function handle.
Any help is appreciated!
dcdt = -k*c(t).^2; %function
k = 0.9;
n = 2;
c0 = [1 2 3 6 8 10]; %initial conditions
t = [0 10]; % t is from 0 to 10 seconds
% y = 1:length(c0);
y = ode45(dcdt,t,c0)
plot(t,c) %plot t against c

Risposta accettata

Stephan
Stephan il 28 Ott 2020
Modificato: Stephan il 28 Ott 2020
k = 0.9;
dcdt = @(t,c) -k*c.^2; %function
c0 = [1 2 3 6 8 10]; %initial conditions
t = [0 10]; % t is from 0 to 10 seconds
for k = 1:numel(c0)
[t,c(:,k)] = ode45(dcdt,t,c0(k));
end
plot(t,c) %plot t against c

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by