How to use ode45 for an equation with space dependent coefficients?

1 回表示 (過去 30 日間)
Mirlan Karimov
Mirlan Karimov 2019 年 3 月 16 日
コメント済み: darova 2019 年 3 月 16 日
Consider an equation of form: where so that after each time iteration I have to update to be used in the next time iteration. I have hard coded Runge-Kutta scheme but I believe that there are some tolerance problems. Therefore, I want to try ODE45 and check the difference.

採用された回答

darova
darova 2019 年 3 月 16 日
p = @(x) sin(x).*sqrt(tan(x));
% x(1) == x
% x(2) == x'
f = @(t,x) [x(2); -p(x(1)).*x(1)];
tspan = [0 2];
x0 = [0.5 0.1];
[t, x] = ode45(f,tspan,x0);
plot(t,x(:,1),'r',t,x(:,2),'b')
  2 件のコメント
Mirlan Karimov
Mirlan Karimov 2019 年 3 月 16 日
Thank you!
I think I should clarify the question a bit more. What if p is a bit more complicated so that cant be directly written as a function? For example, in my case, where C is a complicated function of x and I is another complicated function obtained by summation of vector cross products?
darova
darova 2019 年 3 月 16 日
function main
clc, clear
tspan = [0 1];
x0 = [ 0.1 0.5]
[t,y] = ode45(f,tspan,x0);
X = y(:,1);
Y = y(:,2);
plot(X,Y,'r')
end
function ydot = f(t,x)
x1 - x(1);
dx = x(2);
C = % complicated function
I = % another
ydot(1) = dx; % x'
ydot(2) = -C/I*x1; % x''
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by