MATLAB Answers

0

Error in ODE 45: MYODE must return a column vector.

Asked by Nancy Rodriguez on 11 Oct 2019
Latest activity Edited by Stephan
on 11 Oct 2019
I am trying to discretize a system of PDEs with a finite difference scheme and solve the resulting ODE system with ODE45:
PDE Solver code:
global N;global dx;
global a11; global a12; global a22; global a21;
a11=1;a12=1; a22=1;a21=1;
dx = .05;
X = -2:dx:2;
N = length(X);
tspan = [0 2];
u = ones(1,N);
v = 2*ones(1,N);
%Initial Data
%%
y0 = [u,v];
%%
% Solve ODE
[t,y] = ode45(@myODE, tspan, y0);
myODE code:
function dy = myODE(~,y)
global N;
global dx;
global a11; global a12; global a22; global a21;
u=y(1:N);
v = y((N+1):2*N);
du = zeros(size(u));
dv = zeros(size(v));
du(1) = (2*a11*(u(2)-u(1)))/(dx^2)+a12*(u(1)*v(2)-u(2)*v(1)-u(1)*v(1)-u(2)*v(2))/(dx^2);
dv(1) = 2*a22*(v(2)-v(1))/(dx^2)+a21*(v(1)*u(2)-v(2)*u(1)-v(1)*u(1)-v(2)*u(2))/(dx^2);
du(N) = 2*a11*(u(N-1)-u(N))/(dx^2)+a12*(u(N)*v(N-1)-u(N-1)*v(N)-u(N)*v(N)-u(N-1)*v(N-1))/(dx^2);
dv(N) = 2*a22*(v(N-1)-v(N))/(dx^2)+a21*(v(N)*u(N-1)-v(N-1)*u(N)-v(N)*u(N)-v(N-1)*u(N-1))/(dx^2);
for i=2:N-1
du(i) = a11*(u(i+1)-u(i)+u(i-1))/(dx^2)+a12*(u(i)*v(i+1)-u(i-1)*v(i)-u(i)*v(i)-u(i-1)*v(i-1))/(dx^2);
dv(i) = a22*(v(i+1)-v(i)+v(i-1))/(dx^2)+a21*(v(i)*u(i+1)-v(i-1)*u(i)-v(i)*u(i)-v(i-1)*u(i-1))/(dx^2);
end
dy = [du,dv];

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Stephan
on 11 Oct 2019
Edited by Stephan
on 11 Oct 2019
 Accepted Answer

To solve this error make sure that dy is column vector.
dy = [du; dv]
could already solve the problem - but only if they are scalar valued ( I did not check) or are column vectors too.

  0 Comments

Sign in to comment.