I have a problem with square bracket in solving ode45

Hello, as in the title I have small problem with square brackets. I try to solve 6 ordinary equations using function ode45 but it gives me an error "dimensions of matrices being concatenated are not consistent" and i check that 3 equations give only one value, but other 3 equations give two values (first number is the results of solving equation and second number is 0). Here is my question how to remove this zero value as result. I check these equations and without square bracket they give one value.
here is my code
function xprime = lorenz(t,x);
alfa = 0.017;
beta = 0.067;
tau_e = 0.017;
tau_l = 0.0001;
tau_j =0.00000001;
tau_k =0.00000001;
c = 0.000001;
p = 0.000001;
d = 0.000001;
xprime = [c*x(2) + x(3)*tau_k -(tau_l*x(1) - tau_j*x(1));
d*x(3) + x(1)*tau_j - (tau_e*x(2)-c*x(2));
tau_l*x(1) + tau_e*x(2) + beta*x(4) -(x(3)*alfa + x(3)*d + x(3)*tau_k);
x(3)*alfa + x(5)*tau_e + tau_l*x(6) -(x(4)*beta+x(4)*p+x(4)*tau_k);
x(4)*p + x(6)*tau_j - (x(5)*tau_e + x(5)*c);
x(5)*c + x(4)*tau_k-(x(6)*tau_l + x(6)*tau_j)];
I run the code this way [t,x]=ode45(@lorenz,[0 500],[0 0 0.8 0.2 0 0])

Answers (2)

function xprime = lorenz(t,x);
alfa = 0.017;
beta = 0.067;
tau_e = 0.017;
tau_l = 0.0001;
tau_j =0.00000001;
tau_k =0.00000001;
c = 0.000001;
p = 0.000001;
d = 0.000001;
xprime = zeros(6,1);
xprime(1) = c*x(2) + x(3)*tau_k -(tau_l*x(1) - tau_j*x(1));
xprime(2) = d*x(3) + x(1)*tau_j - (tau_e*x(2)-c*x(2));
xprime(3) = tau_l*x(1) + tau_e*x(2) + beta*x(4) -(x(3)*alfa + x(3)*d + x(3)*tau_k);
xprime(4) = x(3)*alfa + x(5)*tau_e + tau_l*x(6) -(x(4)*beta+x(4)*p+x(4)*tau_k);
xprime(5) = x(4)*p + x(6)*tau_j - (x(5)*tau_e + x(5)*c);
xprime(6) = x(5)*c + x(4)*tau_k-(x(6)*tau_l + x(6)*tau_j);
Best wishes
Torsten.
This is an evil behavior of Matlab, which tries to be smart:
a = 1;
b = 2;
c1 = [a - b] % -1
c2 = [a -b] % [1, -2]
c3 = [a-b] % -1
It is even hard to describe this in clear words: If a minus occurs without a separating space before an expression, but a space before, it is assumed to be a unary minus. Then including the expression in square brackets the corresponding term is assumed to be an extra element. To avoid this use commas to separate elements in a vector concatenated by square brackets and insert two space around each operator:
xprime = [c * x(2) + x(3) * tau_k - (tau_l * x(1) - tau_j * x(1)); ...
d * x(3) + x(1) * tau_j - (tau_e * x(2) - c * x(2)); ...
tau_l * x(1) + tau_e * x(2) + beta * x(4) - (x(3) * alfa + x(3) * d + x(3) * tau_k); ...
x(3) * alfa + x(5) * tau_e + tau_l * x(6) - (x(4) * beta + x(4) * p+x(4) * tau_k); ...
x(4) * p + x(6) * tau_j - (x(5) * tau_e + x(5) * c); ...
x(5) * c + x(4) * tau_k - (x(6) * tau_l + x(6) * tau_j)];
This differs from your version only by spaces and to be secure the line continuation is written explicitly by "..." .
See also the not obvious meaning of:
2./a
Is this the elementwise division of 2 by the vector 2 (valid) or is it the abbreviated 2.0 divided by the vector (error)?
Summary: Insert spaces around operators with 2 arguments. Saving some key-presses during typing is not useful, if it causes ambiguities.

Asked:

on 2 Feb 2018

Answered:

Jan
on 2 Feb 2018

Community Treasure Hunt

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

Start Hunting!