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

조회 수: 1 (최근 30일)
Cate may
Cate may 2020년 10월 28일
댓글: Cate may 2020년 10월 28일
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

채택된 답변

Stephan
Stephan 2020년 10월 28일
편집: Stephan 2020년 10월 28일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by