ODE45 - seperate function from commandfile

조회 수: 1 (최근 30일)
fxo
fxo 2013년 5월 14일
I'm calculating some chemical reactions using the code below but I would like to bring out the "incode" function concentration to a seperate functionfile. I've made a try in the code at the bottom.
s = 1;
q = 1;
w = 0.1610;
% define y = [α(t) β(t) γ(t)] = [y(1) y(2) y(3)], then
concentration = @(t,y) [...
s * (y(2) - y(1)*(y(2)+1) - q*y(1)^2) % α'(t)
(-y(2) - y(1)*y(2) + y(3))/s % β'(t)
w * (y(1)-y(3)) % γ'(t)
];
% initial value
y0 = [30 1 30];
% time span
t_span = [0 10];
% solve numerically
[t, y] = ode45(concentration, t_span, y0);
plot(t,y);
Function draft
function concentration = func(t,y)
concentration=[
s * (y(2) - y(1)*(y(2)+1) - q*y(1)^2) % α'(t)
(-y(2) - y(1)*y(2) + y(3))/s % β'(t)
w * (y(1)-y(3)) % γ'(t)
];
I'm a bit lost and would appreciate some help to do this.
  댓글 수: 4
Jan
Jan 2013년 5월 14일
@fxo: Not funny. People have died.
Jan
Jan 2013년 5월 14일
편집: Jan 2013년 5월 14일
@fxo: Please note, that the "why is your question not urgent" thread is the one with the 2nd most votes in this forum. See also: http://www.mathworks.com/matlabcentral/answers/43073-a-guide-to-tags

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2013년 5월 14일
Save this as file:
function c = func(t, y, s, q, w)
c = [s * (y(2) - y(1)*(y(2)+1) - q*y(1)^2); ... % α'(t)
(-y(2) - y(1)*y(2) + y(3))/s; ... % β'(t)
w * (y(1)-y(3))]; % γ'(t)
Then start the integrator like this:
s = 1;
q = 1;
w = 0.1610;
y0 = [30 1 30];
t_span = [0 10];
[t, y] = ode45(@(t,x) func(t, x, s, q, w), t_span, y0);
  댓글 수: 1
fxo
fxo 2013년 5월 14일
Thank you for the help, worked very well!:)

댓글을 달려면 로그인하십시오.

추가 답변 (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