Why does one equation become a 1x2 matrix with one NaN, when all variables within the matrix equation are 1x1?
10 views (last 30 days)
Show older comments
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(1,1)=0; %1.3814; angle in radians
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.
Thank you for your time.
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.
Accepted Answer
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.
2 Comments
Matt J
on 28 Apr 2013
Edited: 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.
More Answers (1)
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.
4 Comments
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!