フィルターのクリア

Need help on using ODE45

1 回表示 (過去 30 日間)
John Smith
John Smith 2015 年 2 月 19 日
編集済み: John Smith 2015 年 2 月 19 日
I'm trying to solve the following ODE
dx(1)/dt = k1 (1 + k(2)x(3)) − x(1)x(2)^2 − k(3)x(1),
dx(2)/dt = x(1)x(2)^2 − x(2) + k(3)x(1),
dx(3)/dt = x(2) − k(4)x(3),
subject to the initial conditions x(0) = x0 and plot x1, x2 and x3 as a function of t using 10^4 points for 0 ≤ t ≤ T. It should take T and vectors x0 and k0 as inputs.
So far I have the following code:
function crosscatalator(x0,k,T)
sol = ode45(@(t,x) f(t,x,k),[0 T],x0);
t = linspace(0,T,10000); X = deval(sol,t); plot(t,X(1,:),t,X(2,:),t,X(3,:))
legend('x_1','x_2','x_3')
xlabel('t')
function f = f(~,x,k)
f = zeros(3,1);
f(1) = k(1)*(1+k(2)*x(3))-x(1)*x(2)^2-k(3)*x(1);
f(2) = x(1)*x(2)^2-x(2)+k(3)*x(1);
f(3) = x(2)-k(4)*x(3);
When I try with initial conditions x0=[1 1 1], k=[1 2 3 4], T=200. I get an error saying index is out of bounds. I've tried everything I can possibly do to fix this but I keep getting an error. What am I doing wrong?
  2 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 2 月 19 日
I get the plots, no error occurs. Do you maybe have a variable which is also called "crosscatalator"? If that's not the case, please tell us where exactly the error occurs.
John Smith
John Smith 2015 年 2 月 19 日
編集済み: John Smith 2015 年 2 月 19 日
I don't have another variable called "crosscatalator". I have noted the error below. Thanks.

サインインしてコメントする。

採用された回答

Mischa Kim
Mischa Kim 2015 年 2 月 19 日
John, your code is good. Is the code above all contained within one file with name crosscatalator.m?
If so, try at the MATLAB command prompt
clear;
x0 = [1 1 1]; k = [1 2 3 4]; T = 200;
crosscatalator(x0,k,T)
and tell us if it is still not working.
  8 件のコメント
Mischa Kim
Mischa Kim 2015 年 2 月 19 日
One last (for now) change: re-name the ODE file. Of course, also in the function call, line 2.
function f = myf(~,x,k)
...
John Smith
John Smith 2015 年 2 月 19 日
編集済み: John Smith 2015 年 2 月 19 日
It's still the same with this change, doesn't work. I'm starting to think it's probably something wrong with matlab on the university computers...is that even possible?
I haven't tried testing the code using matlab on my laptop, so it might work as it worked for you, even so it seems strange that it's not working on university computers.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by