ODE45 - seperate function from commandfile
古いコメントを表示
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 件のコメント
Uh, the lovely "urgent" tag. See http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency.
Do you get an error message, when you run this? Or what else is the problem?
Matt Kindig
2013 年 5 月 14 日
Well for one, ode45 takes a function as its first argument, rather than a variable. As your function is currently defined, the function is named 'func', without an output of 'concentration'. You probably want the function itself to be called concentration, such as:
function c = concentration(t,y)
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)
];
Jan
2013 年 5 月 14 日
@fxo: Not funny. People have died.
@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
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!