Problems with ode45, Input arguments to function include colon operator. To input the colon character, use ':' instead.

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?

回答(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 个评论

The solution of my differential equation is a matrix because all the variables involved are n*n matrix. But when I tried to use ode45 without reshaping my matrix into a column vector, Matlab returned an error. I tried to apply our suggestion but the code didn't work.
Check that the number of elements in the output is the same as those in the input arguments.
@Serena De Lorenzi: please show the complete error message, which means all of the red text, and the code that you are using.
%input
n=4;
C=sym('c',n);
gamma=2;
omega=[0.0021 0.0003 -0.0003 -0.0010;
0.0003 0.0010 -0.0000 -0.0004;
-0.0003 -0.0000 0.0010 0.0001;
-0.0010 -0.0004 0.0001 0.0010];
M=[0.3340 0.1284 -0.0215 -0.3281;
0.1284 0.0494 -0.0083 -0.1262;
-0.0215 -0.0083 0.0014 0.0211;
-0.3281 -0.1262 0.0211 0.3224];
G=[0.5853 0.2251 -0.0376 -0.5751;
0.2251 0.0866 -0.0145 -0.2211;
-0.0376 -0.0145 0.0024 0.0370;
-0.5751 -0.2211 0.0370 0.5649];
%differential equation
dCdt= @(t,C) -(transpose(C)*M*C+((1-gamma)/gamma)*(inv(omega)+inv(omega)*G*C+...
C*G*inv(omega)+transpose(C)*M*C));
%tspan
taustart=1;
taufinal=240;
tspan=[taustart taufinal];
% initial condition --> the initial condition is that the matrix C is 0,
% but I create a column vector since then I reshape matrix C into a column vector
C0=zeros(n^2,1);
%solve differential equation using ode45
[t,c] = ode45(@(t,C)reshape(dCdt(t,C),[],1), tspan, C0);
This is my code.
But it returns many errors: Error using * Inner matrix dimensions must agree.
Error in derivata>@(t,C)-(transpose(C)*M*C+((1-gamma)/gamma)*(inv(omega)+inv(omega)*G*C+C*G*inv(omega)+transpose(C)*M*C))
Error in derivata>@(t,C)reshape(dCdt(t,C),[],1)
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);
Error in derivata (line 32) [t,c] = ode45(@(t,C)reshape(dCdt(t,C),[],1), tspan, C0);
"derivata" is the name of the m.file
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.

请先登录,再进行评论。

类别

标签

Community Treasure Hunt

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

Start Hunting!

Translated by