69 views (last 30 days)
Show older comments
Emily on 2 Mar 2023
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/1921940-using-ode45-with-a-matrix-as-input-to-my-function
Commented: Jan on 2 Mar 2023
Accepted Answer: Jan
Open in MATLAB Online
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.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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Jan on 2 Mar 2023
Edited: Jan on 2 Mar 2023
Open in MATLAB Online
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.
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Jan on 2 Mar 2023
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1921940-using-ode45-with-a-matrix-as-input-to-my-function#comment_2642105
Open in MATLAB Online
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];
tspan = [0, 1];
xinit = rand(5, 1);
% [t,x] = ode45('UseGamma', tspan, xinit, Gamma); % Failing with message:
Unrecognized function or variable 'UseGamma'.
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
% Working correctly:
[t,x] = ode45(@(t,x) UseGamma(t, x, Gamma), tspan, xinit);
function dxdt = UseGamma(t,x,Gamma)
dxdt = Gamma * x;
end
Emily on 2 Mar 2023
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1921940-using-ode45-with-a-matrix-as-input-to-my-function#comment_2642150
Open in MATLAB Online
Thanks Jan,
I had originally used your shorter version of the function, but had been trying the longer more explicit version to troubleshoot.
Yes, the problem seems to be the way I was calling the integrator. I was following an example on the internet. Here is a code that works:
% A MWE calling UseGamma in ODE45
ParameterA = 1
ParameterB = 10
sum = ParameterA + ParameterB
Gamma=zeros(5,5);
Gamma(1,1) = -(1/2)*(1+ParameterA^2/sum^2);
Gamma(1,3) = -(ParameterB*ParameterA/(2*sum^2));
Gamma(1,4) = 1;
Gamma(1,5) = ParameterB^2/(2*sum^2);
Gamma(2,2) = -1*ParameterB^2/(2*sum^2);
Gamma(2,3) = -ParameterA*ParameterB/(2*sum^2);
Gamma(2,4) = 1;
Gamma(2,5) = (1/2)*(1+ParameterA^2/sum^2);
Gamma(3,1) = -ParameterA*ParameterB/(4*sum^2);
Gamma(3,2) = -ParameterA*ParameterB/(4*sum^2);
Gamma(3,3) = -1;
Gamma(4,4) = -2;
Gamma(5,5) = -1;
Gamma(5,1) = (1/2)*(1+ParameterA^2/sum^2);
Gamma(5,2) = 1*ParameterB^2/(2*sum^2);
Gamma(5,3) = ParameterB*ParameterA/sum^2;
xinit = [0.99; 0; 0;0;0.01];
tspan = [0 50];
UseGamma(tspan,xinit,Gamma)
[t,x] = ode45(@(t,x) UseGamma(t, x, Gamma), tspan, xinit);
function dxdt = UseGamma(t,x,Gamma)
dxdt = Gamma * x;
end
However when I call it using the old format, it doesn't work:
[t,x] = ode45('UseGamma', tspan, xinit, Gamma);
Gamma =
0×0 empty char array
Jan on 2 Mar 2023
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/1921940-using-ode45-with-a-matrix-as-input-to-my-function#comment_2642370
Open in MATLAB Online
@Emily: Fine.
Just a note: Using the names of built-in functions as variables is a frequent cause of bugs. See:
sum(1:10) % Working
ans = 55
sum = 17;
sum(1:10) % Error:
Index exceeds the number of array elements. Index must not exceed 1.
'sum' appears to be both a function and a variable. If this is unintentional, use 'clear sum' to remove the variable 'sum' from the workspace.
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABMathematicsElementary MathSpecial FunctionsGamma Functions
Find more on Gamma Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office