Using ODE45 with a matrix as input to my function
57 ビュー (過去 30 日間)
古いコメントを表示
I want to repeatedly solve a set of ODEs for different values of some parameters, which will be encoded in a matrix. The equation I want to solve is
where Γcontains the parameters I wish to vary.
I made a function file:
function dxdt = UseGamma(t,x,Gamma)
Gamma
y = Gamma*x;
dxdt = zeros(size(x));
dxdt(1) = y(1);
dxdt(2) = y(2);
dxdt(3) = y(3);
dxdt(4) = y(4);
dxdt(5) = y(5) ;
If I create the variables tspan, xinit, Gamma of appropriate size and call the function, I get:
UseGamma(tspan,xinit,Gamma)
Gamma =
-0.7500 0 -0.2500 1.0000 0.2500
0 -0.2500 -0.2500 1.0000 0.7500
-0.1250 -0.1250 -1.0000 0 0
0 0 0 -2.0000 0
0.7500 0.2500 0.5000 0 -1.0000
ans =
-0.7400
0.0075
-0.1237
0
0.7325
However when I try to use this function as the RHS of my differential equation by calling it with ode45, I get:
[t,x] = ode45('UseGamma', tspan, xinit, Gamma);
Gamma =
0×0 empty char array
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
Error in UseGamma (line 3)
y = Gamma*x;
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Is it possible to use the matrix as the input to ode45? If so, what am I doing wrong? If not, what is the simplest way to input these 25 values in Gamma to ode45? Thanks to everybody who is always so helpful!
0 件のコメント
採用された回答
Jan
2023 年 3 月 2 日
編集済み: Jan
2023 年 3 月 2 日
You can abbreviate:
function dxdt = UseGamma(t,x,Gamma)
Gamma
y = Gamma*x;
dxdt = zeros(size(x));
dxdt(1) = y(1);
dxdt(2) = y(2);
dxdt(3) = y(3);
dxdt(4) = y(4);
dxdt(5) = y(5) ;
to
function dxdt = UseGamma(t,x,Gamma)
dxdt = Gamma * x;
This style of calling an ODE integrator is outdated for over 20 years now (since Matlab R6.5):
[t,x] = ode45('UseGamma', tspan, xinit, Gamma);
In Matlab 2022b this is treated as error and I get a message about an unknown function "UseGamma" when I run this in the forum. Prefer:
[t,x] = ode45(@(t,x) UseGamma(t, x, Gamma), tspan, xinit);
But the main problem is, that somewhere Gamma is defined as empty CHAR ''. Please post a copy of the code.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Gamma Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!