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

1 view (last 30 days)
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

Accepted Answer

Stephan
Stephan on 28 Oct 2020
Edited: Stephan on 28 Oct 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

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!