MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Why does one equation become a 1x2 matrix with one NaN, when all variables within the matrix equation are 1x1?

Asked by Edvardas on 28 Apr 2013

Hello,

Basically as the title mentions, I am having a problem that an equation I'm using becomes a 1x2 double matrix, with a second member inside being NaN. Whilst debugging I checked the members within the equation, all of them seem to be 1x1, hence I am wondering what could be the reason for this, as at the moment I can't use the member found from the calculations as it causes further errors because it's a larger matrix than it should be.

Here is the code in which the equation goes wrong (the equation itself is x(k-1)=(sqrt(l3^2-d(k-1).^2)) ):

Angle.m

```function [t, z] = Angle(tInc)
% Set init;ial conditions
global x;
t(1)=0;
z(2,1) = 0; %angular velocity
mast=6;
l=1.15; %half the widht of the catamaran
l3=sqrt(mast^2+l^2); %resultant of half the width and mast
k=1;
```
```while z(1,k)<=1.381426277 %0.18937005; %d=-mast-r:0;
k=k+1;
d=-mast*cos(z(1,k-1))+l*sin(z(1,k-1));
x(k-1)=(sqrt(l3^2-d(k-1).^2));    %shortest distance between the force of the air bag and hull
t(k)=t(k-1)+tInc;
[z(:,k)] = RKF(t(k-1), z(:,k-1), tInc);
end
end
```

This function also uses two more functions, I am unsure whether those are necessary, as the NaN occurs before calling the functions out, but just in case here they are:

RKF.m

```function [z] = RKF(t, z, tInc)
global x;
%tolh=0.000002; %chosen tolerance for time increment. Used for RK5
%RK4
A=tInc*Dz(t,z);
B=tInc*Dz(tInc/2+t,z+A/2);
C=tInc*Dz(tInc/2+t,z+B/2);
D=tInc*Dz(tInc+t,z+C);
z=z + (A+2*B+2*C+D)/6;
end
```

Dz.m

```function [dz] = Dz(t, z)
```
```global x;
```
```g=9.81; %/sqrt((6400+z(2)/1000)/6400);
m = 44.6; % Mass (kg)
a=1030; %water density
%a = (p*M)/(R*T); % Air density (kg/m^3)
l=1.15; %half the width of main body of catamaran
r=0.3; %diameter of missile (m)
V=(4*pi*r^3)/3; %volume
I=391.5; %moment of inertia for the catamaran
mast=6; %length of mast
```
```l3=sqrt(mast^2+l^2);  %distance between air bag and hull
Fb=a*g*V*x; %buoyancy force of the air bag
Fg=m*g*2*l*cos(z(1));
```
```dz(1)=z(2); %dtheta
```
```dz(2)=(Fb-Fg)/I %ddtheta
```
```%Transpose the dz vector
dz=dz';
```

I hope someone may be able to help with this.

Regards,

Edvardas

## 1 Comment

Walter Roberson on 28 Apr 2013

At the command line, please command

```dbstop if error
```

and then run your program. When it stops, please show which line it is on, and show size(mast), size(d), size(l3), size(k), size(z), and also show the value of k.

## Products

Answer by Matt J on 28 Apr 2013

At the command line do

` >>dbstop if naninf`

Then rerun your code. When it stops at the K>> prompt, show us the output of

` K>> whos`

Also paste the error messages for us.

Edvardas on 28 Apr 2013

Hello,

dbstop didn't seem to stop the code, so I stopped it at the point where the NaN starts occurring manually:

From the whom command it's then:

K>> whos Name Size Bytes Class Attributes

```Fb        1x2                16  double
Fg        1x1                 8  double
I         1x1                 8  double
V         1x1                 8  double
a         1x1                 8  double
dz        1x1                 8  double
g         1x1                 8  double
l         1x1                 8  double
l3        1x1                 8  double
m         1x1                 8  double
mast      1x1                 8  double
r         1x1                 8  double
t         1x1                 8  double
x         1x2                16  double    global
z         2x1                16  double
```

If the function is not stopped the error which appears is this:

```In an assignment  A(I) = B, the number of elements in B and I must be the same.
```
```Error in Dz (line 23)
dz(2)=(Fb-Fg)/I %ddtheta
```
```Error in RKF (line 5)
A=tInc*Dz(t,z);
```
```Error in Angle (line 30)
[z(:,k)] = RKF(t(k-1), z(:,k-1), tInc);
```

Fb being an equation with the multiplier x, which has the unexpected 1x2 with NaN becomes 1x2 as well, hence I get the A(I)=B error (I think).

Regards,

Edvardas

Matt J on 28 Apr 2013

Do you have a copy of x in your base workspace (where it is also global)? If so, you should clear it before you run. Better yet, don't use global variables at all, since they are generally regarded as dangerous. Pass x explicitly to RFK and Dz or else make them Nested Functions within Angle.m, so that they can share its workspace.

Otherwise, I cannot reproduce the error you are seeing. When I run Angle(1) with tInc=1, I encounter another error

```Attempted to access d(2); index out of bounds because numel(d)=1.
```
```Error in test (line 15)
x(k-1)=(sqrt(l3^2-d(k-1).^2));
```

This makes sense because "d" is 1x1, yet you are accessing d(k-1) as if it were a vector. You should review that.

Answer by Walter Roberson on 28 Apr 2013

You have

```dz(2)=(Fb-Fg)/I
```

and before that

```Fg=m*g*2*l*cos(z(1))
```

with m, g, l, and z(1) all being scalars, so Fg is a scalar, so look at Fb

```Fb=a*g*V*x
```

a, g are constants, V you can trace back to see must be a scalar. But x? According to whos, x is 1 x 2... which would make Fb 1x2, which would make (Fb-Fb)/I into 1x2, which is not going to fit into dz(2).

You do not have any assignments to x, so the reason it is 1x2 lies in whatever created it outside of this. You will probably find that x(2) is NaN that that that is why the second element of the expression becomes NaN.

Matt J on 28 Apr 2013

However, for some reason it creates a slot for 2nd cycle, during the first cycle, in the matrix and fills it with NaN.

If x is global in your base workspace as well as in the functions you've shown, and if it was previously 1x2, and if you didn't clear x in your base workspace, then it will continue to start off 1x2 when you rerun Angle.m. See also my last Comment.

Walter Roberson on 28 Apr 2013

Suppose the problem is not occurring on the first call to RKF, and suppose that the while condition is still true after that call leading there to be another round, k becomes 3, x(2) becomes assigned to ?

When it stops because of the error, use

```dbup;dbup
```

to move the stack up to the angle function, and check out the k value at that level.

Edvardas on 28 Apr 2013

Hmm, I am uncertain of whether I understood the "k becomes 3, x(2) becomes assigned to ?". After looking through Matt's analysis it did come to me that using x as a vector might have been the mistake. So I changed x into a regular variable, so that it would keep overwriting itself with new values all the time and it seems to have solved the problem, and the ODE seems like it's being solved now.

In terms of clearing, it wasn't the case. It simply created the 1x2 matrix from scratch. I am still uncertain as to why though. And in terms of the global being dangerous, I have been warned before as well, will try using nested functions.

Thank you both for your time and help, I really appreciate it :)