Got Questions? Get Answers.
Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
num2str Error?

Subject: num2str Error?

From: Hugh

Date: 15 Oct, 2010 16:37:03

Message: 1 of 3

Hi there,

When running the code shown at the bottom I get the following error?

??? Error using ==> num2str at 32
Numeric array is unspecified

Error in ==> Heat2DADI at 65
        title (['t=', num2str (Tsnap (1)) ],'Fontsize',12);

Seems like its not recognising Tsnap(1) as a numerical value but it is?

[code]
function U = Heat2DADI (dt, dx, dy, Tmax, Tsnap, value, bounds)
% make sure steps are consistent
Nx = round (1/dx);
dx = 1/Nx;
Ny = round (1/dy);
dy = 1/Ny;
Nt = round (Tmax/dt);
dt = Tmax/Nt;
rhox = dt/dx^2;
rhoy = dt/dy^2;

Layers = zeros (2, 1+Nx, 1+Ny);
Auxlayer = zeros (1+Nx, 1+Ny);
tpast = 1;
tnow = 2;
iTsnap = Tsnap/dt;
[X, Y] = meshgrid (0:dx:1, 0:dy:1);
% set up initial conditions
Layers (tpast, (1+round (bounds (1)/dx)):(1+round (bounds (2)/dx)), ...
    (1+round (bounds (3)/dy)):(1+round (bounds (4)/dy))) = value;
U = shiftdim (Layers (tpast,:,:));
figure;
surf (X,Y,U);
title ('t=0','Fontsize',12);
% Prepare matrices and LU decomposition
Matrix1 = diag ((1+rhox)*ones (Nx-1,1)) + ...
    diag (-rhox/2*ones (Nx-2,1),1) + ...
    diag (-rhox/2*ones (Nx-2,1),-1);
[L1, U1] = lu (Matrix1);
Matrix2 = diag ((1+rhoy)*ones (Ny-1,1)) + ...
    diag (-rhoy/2*ones (Ny-2,1),1) + ...
    diag (-rhoy/2*ones (Ny-2,1),-1);
[L2, U2] = lu (Matrix2);
Rhs1 = zeros (Nx-1,1);
Rhs2 = zeros (Ny-1,1);
% Carry out iterations
for t=1:Nt
    % first half step
    for j=1:Ny-1
        % set up right hand side
        for i=1:Nx-1
            Rhs1 (i) = rhoy/2*Layers (tpast,i+1,j) + ...
                (1-rhoy)*Layers (tpast,i+1,j+1) + ...
                rhoy/2*Layers (tpast,i+1,j+2);
        end
        % solve
        Auxlayer (2:Nx,j+1) = U1 \ (L1 \ Rhs1);
    end
    % second half step
    for i=1:Nx-1
        % set up right hand side
        for j=1:Ny-1
            Rhs2 (j) = rhox/2*Auxlayer (i,j+1) + ...
                (1-rhox)*Auxlayer (i+1,j+1) + ...
                rhox/2*Auxlayer (i+2,j+1);
        end
        % solve
        Layers (tnow, i+1,2:Ny) = (U2 \ (L2 \ Rhs2))';
    end
    % plot if necessary
    if find (iTsnap == t)
        U = shiftdim (Layers (tnow,:,:));
        figure;
        surf (X,Y,U);
        title (['t=', num2str (Tsnap (1)) ],'Fontsize',12);
        Tsnap (1) = [];
    end
    % swap layers
    tnow = 1+mod (t+1,2);
    tpast = 1+mod (t,2);
end
[/code]

Subject: num2str Error?

From: someone

Date: 15 Oct, 2010 16:59:05

Message: 2 of 3

"Hugh " <h_a_patience@hotmail.com> wrote in message <i99vvf$1bb$1@fred.mathworks.com>...
> Hi there,
>
> When running the code shown at the bottom I get the following error?
>
> ??? Error using ==> num2str at 32
> Numeric array is unspecified
>
> Error in ==> Heat2DADI at 65
> title (['t=', num2str (Tsnap (1)) ],'Fontsize',12);
>
> Seems like its not recognising Tsnap(1) as a numerical value but it is?
>
> [code]
> function U = Heat2DADI (dt, dx, dy, Tmax, Tsnap, value, bounds)
> % make sure steps are consistent

... snip...

for t=1:Nt
    % first half step
    for j=1:Ny-1
        % set up right hand side
        for i=1:Nx-1
            Rhs1 (i) = rhoy/2*Layers (tpast,i+1,j) + ...
                (1-rhoy)*Layers (tpast,i+1,j+1) + ...
                rhoy/2*Layers (tpast,i+1,j+2);
        end
        % solve
        Auxlayer (2:Nx,j+1) = U1 \ (L1 \ Rhs1);
    end
    % second half step
    for i=1:Nx-1
        % set up right hand side
        for j=1:Ny-1
            Rhs2 (j) = rhox/2*Auxlayer (i,j+1) + ...
                (1-rhox)*Auxlayer (i+1,j+1) + ...
                rhox/2*Auxlayer (i+2,j+1);
        end
        % solve
        Layers (tnow, i+1,2:Ny) = (U2 \ (L2 \ Rhs2))';
    end
    % plot if necessary
    if find (iTsnap == t)
        U = shiftdim (Layers (tnow,:,:));
        figure;
        surf (X,Y,U);
        title (['t=', num2str (Tsnap (1)) ],'Fontsize',12);
        Tsnap (1) = [];
    end

...snip...

end
> [/code]

First, with what value of Tsnap do you call Heat2DADI?

If thats OK, maybe your problemn is:
During one iteration of the loop (if iTsnap == t)
you set Tsnap(1) = [].
Then on the next iteration of the loop (if iTsnap == t)
Tsnap(1) will be empty when you use it in num2str.
 

Subject: num2str Error?

From: Sean

Date: 15 Oct, 2010 17:05:04

Message: 3 of 3

"Hugh " <h_a_patience@hotmail.com> wrote in message <i99vvf$1bb$1@fred.mathworks.com>...
> Hi there,
>
> When running the code shown at the bottom I get the following error?
>
> ??? Error using ==> num2str at 32
> Numeric array is unspecified
>
> Error in ==> Heat2DADI at 65
> title (['t=', num2str (Tsnap (1)) ],'Fontsize',12);
>
> Seems like its not recognising Tsnap(1) as a numerical value but it is?

Put a break point on that line and inspect the various variables, i.e. Tsnap.

You could also do this with
dbstop if error
and it will stop when that error occurs so you can see what's going on.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us