From: MCE on
Hi,
I am trying to solve a ODE with the command ode15s. When I run my code (beneath) I get the error message that the inner matrix dimensions do not agree, but I really not see what I am doing wrong. Can somebody help me with this?
Thanks a lot in advance.
------------------------
function exdiff
D = 0.05;
N = 19;
space = [1:N]/(N+1);
dx = 1/(N+1);

% Define matrix A (N x N) and g(N x 1)
A = -2*eye(N)+diag(ones((N-1),1),1)+diag(ones((N-1),1),-1)
g = [ones(1,1);zeros(N-1,1)]

tspan = [0 2];
y0 = 0;
y0 = y0(:);
tic
[t,y] = ode15s(@diffs,tspan,y0);
toc

%% Nested function
function yprime = diffs(t,y)

yprime = (D/(dx^2))*(y.*A + g);
end
end
------------------------------
From: Jan Simon on
Dear MCE,

> I am trying to solve a ODE with the command ode15s. When I run my code (beneath) I get the error message that the inner matrix dimensions do not agree, but I really not see what I am doing wrong. Can somebody help me with this?

Before I start to analyse your code, it would be much easier, if you post, in which line the error appears. Then you could use the debugger ("dbstop if error") and inspect the dimensions of your variables by your own.
The debugger has access to full access to all variables, so it is much more powerful than a newsgroup.
If you've found the line and the corresponding variables, we could gain some help more efficiently.

Kind regards, Jan
From: MCE on
Dear Jan,

Sorry for my unclear question. I'll try to explain some more. I've used the debugger as you said and the error appears first in the line:

yprime = (D/(dx^2)).*y.*A + (D/(dx^2)).*g;

Here it has to do with the inner matrix dimensions. And I've found out that it is 'y' or 'A' which gives the error. For the dimensions: D and dx are constants, A is a NxN matrix and g is a Nx1 matrix.

I hope this is usefull? Thanks for your time.

Best regards,
Marlinde
From: Steven_Lord on


"MCE" <m.c.dejonge(a)student.tudelft.nl> wrote in message
news:2144499587.50756.1280867285626.JavaMail.root(a)gallium.mathforum.org...
> Dear Jan,
>
> Sorry for my unclear question. I'll try to explain some more. I've used
> the debugger as you said and the error appears first in the line:
>
> yprime = (D/(dx^2)).*y.*A + (D/(dx^2)).*g;
>
> Here it has to do with the inner matrix dimensions. And I've found out
> that it is 'y' or 'A' which gives the error. For the dimensions: D and dx
> are constants, A is a NxN matrix and g is a Nx1 matrix.

There are five variables in that expression. You've told us the dimensions
of four of them -- D and dx are 1-by-1 (I'm assuming that you're using the
word "constants" to refer to "scalar constants"), A is N-by-N, and g is
N-by-1. You haven't told us what the size of y is.

Regardless of the size of y, there's a problem here. The first term that
makes up yprime, (D/(dx^2)).*y.*A, will either error or be size N-by-N (if y
is a scalar or is N-by-N.) The second term, (D/(dx^2)).*g, will be N-by-1.
You can't add an N-by-N matrix and an N-by-1 vector, and so this code SHOULD
error. You need to determine what size the correct answer for yprime should
be and determine how to modify the variables to result in a vector of that
size. [My guess is that yprime should be an N-by-1 vector and so there's a
missing matrix multiplication in the first term:

((D/(dx^2)).*y.*A)*nBy1Vector + (D/(dx^2)).*g

but only you know your problem and so only you can determine that.]

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com