Why does one equation become a 1x2 matrix with one NaN, when all variables within the matrix equation are 1x1?
Mostra commenti meno recenti
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 Commento
Walter Roberson
il 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.
Risposta accettata
Più risposte (1)
Walter Roberson
il 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 Commenti
Matt J
il 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
il 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
il 28 Apr 2013
Categorie
Scopri di più su Common Operations in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!