0
votes

I am trying to solve a second order differential using ODE45 in Matlab with matrix as inputs. I am struck with couple of errors that includes :

"In an assignment  A(I) = B, the number of elements in B and
 I must be the same."

Double order differential equations given below:

dy(1)= diag(ones(1,100) - 0.5*y(2))*Co;
dy(2)= -1 * Laplacian(y(1)) * y(2);

Main function call is:

[T,Y] = ode45(@rigid,[0.000 100.000],[Co Xo]);

Here, Co is Matrix of size 100x100 and Xo is a column matrix of size 100x1. Laplacian is a pre-defined function to compute matrix laplacian.

I will appreciate any help in this. Should I reshape input matrices and vectors to fall in same dimensions or something?

1

1 Answers

1
votes

Your guess is correct. The MATLAB ode suite can solve only vector valued ode, i.e. an ode of the form y'=f(t,y). In your case you should convert y, and dy, back and forth between a matrix and an array by using reshape.

To be more precise, the initial condition will be transformed into the array

y0 = reshape([Co Xo], 100*101, 1);

while y will be obtained with

y_matrix = reshape(y, 100, 101);
y1 = y_matrix(:,1:100);
y2 = y_matrix(:,101);

After having computed the matrices dy1 and dy2 you will have to covert them in an array with

dy = reshape([dy1 dy2], 100*101, 1);

Aside from the limitations of ode45 your code gives that error because, in MATLAB, matrices are not indexed in that way. In fact, if you define A = magic(5), A(11) gives the eleventh element of A i.e. 1.