Problems with ode45, Input arguments to function include colon operator. To input the colon character, use ':' instead.
Show older comments
I try to solve a differential equation in matrix form using ode45. This is my code:
dCdt= @(t,C) -(transpose(C)*M*C+((1-gamma)/gamma)*(inv(omega)+inv(omega)*G*C+...
C*G*inv(omega)+transpose(C)*M*C));
%I define the differentia equation where C is an n*n matrix, M is a n*n matrix, gamma a scalar, omega a n*n matrix and G is a n*n matrix too.
dCdt=dCdt(:); I use this trick because ode45 must return a column vector
tspan=[0 240];
C0=zeros(n^2,1);
[t,C] = ode45(dCdt, tspan, C0);
But Matlab returns an error: Input arguments to function include colon operator. To input the colon character, use ':' instead. And I don't understand why and what it means. How I can solve this problem?
Answers (1)
A function handle is not an array, so it is unclear what you imagine indexing would do to a function handle. You are basically doing something equivalent to this: sin(:), which is clearly rather meaningless. You need to apply the indexing to an array, not to the function handle.
"I use this trick because ode45 must return a column vector"
Actually it is your function that must return a column vector, not ode45.
One easy way would be to wrap your function in reshape to convert its output to a column:
dCdt= @(t,C) ...;
[t,C] = ode45(@(t,C)reshape(dCdt(t,C),[],1), tspan, C0);
6 Comments
Serena De Lorenzi
on 5 Sep 2017
Serena De Lorenzi
on 5 Sep 2017
Stephen23
on 5 Sep 2017
@Serena De Lorenzi: please show the complete error message, which means all of the red text, and the code that you are using.
Serena De Lorenzi
on 5 Sep 2017
Edited: Serena De Lorenzi
on 5 Sep 2017
Actually there is only one error, because MATLAB stops as soon as one error occurs. The other lines tell us where the error occurs (i.e. the stack). The error you get is:
Error using * Inner matrix dimensions must agree.
Which means that you need to fix your function dCdt to work correctly with the dimensions of the input variables. Remember that matrix multiplication requires the inner dimensions to match. If you really want element-wise multiplication then you need to change the operator that you are using. Read about the differences here:
It is not clear why you make C symbolic, but it probably does not help.
Categories
Find more on Ordinary Differential Equations 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!