How to named/called the files: Yi.dat for i=1:nt ?

3 views (last 30 days)
Hello to all! I tried to use the proposed code for dynamically changing of the file name, but something didn't work. So, the code is (sorry that it is so long) :
function [Abl3d,F]=timecycle1
clear all
%Geometry
EJ=849309; m=0.249; M=0.785;U=2.5;
Cs=540; Ks=341.4;
% Mesh
dt=0.02;
dx=0.5;
n=5;
NUI=n+2;
nt=3; % Time level assumed for this exmp=3, but it should be nt=1:17
Asub=zeros(3,3); Asub(2,2)=-((2*M*U)/dx); Asub(2,3)=EJ/(dx^2); Asub(3,2)=-dt/(dx^2);
Amd=zeros(3,3); Amd(1,1)=1;Amd(1,2)=-dt; Amd(2,1)=Ks;Amd(2,2)=((2*M*U/dx)+((M+m)/dt)+Cs); Amd(2,3)=(M*(U^2))-(2*EJ/(dx^2));Amd(3,2)=2*dt/(dx^2); Amd(3,3)=1;
Asup=zeros(3,3); Asup(2,3)=EJ/(dx^2); Asup(3,2)=-dt/(dx^2);
Amdg=zeros(3,3); Amdg(1,1)=1;Amdg(1,2)=-dt; Amdg(2,1)=Ks+((2*EJ)/(dx^4)); Amdg(2,2)=((2*M*U/dx)+((M+m)/dt)+Cs);Amdg(2,3)=(M*(U^2))-(2*EJ/(dx^2));Amdg(3,3)=1;
D=zeros(3,3); D(1,1)=1; D(2,2)=(M+m)/dt; D(3,3)=1;
K=zeros(3,3); K(2,1)=Ks; K(2,2)=Cs;
Gl=zeros(3,3); Gl(2,1)=2*EJ/(dx^4); Gl(2,2)=2*M*U/dx;Gl(3,2)=dt/(dx^2);
Gr=zeros(3,3); Gr(2,1)=2*EJ/(dx^4); Gr(3,2)=dt/(dx^2);
%%CALLING THE SUBFUNCTION.
% (Additional functions within the file are called subfunctions, and these are only visible to the primary function or to other subfunctions in the same file.)
F = ReadyF;
Abl3d = Bl3D;
%%SOLUTION OF THE ALGEBRAIC SYSTEM AND OBTAINING OF THE UNKNOWING VARIABLES FOR THE CURRENT TIME LEVEL
% time level- nt
format shortE
Y=Abl3d\F;
disp(Y);
csvwrite('Y.dat',Y); % !!!!! the file names change dynamically with 'nt',i.e. Y1.dat;Y2.dat;...
%%I.1.Subfunction Bl3D, a subfunction of the main function "combine" that gives the left side of tha algebraic system
function A = Bl3D % Calling "blktridiag" subfunction
Asub;% the sub-diagonal blocks
Amd; % the main diagonal blocks
Asup; % the super-diagonal blocks
Amdg; % The boundary block,that stands at the beginning and end af the main diagonal
n; %give the size of the blok 3-diag.matrix and is equal to the number of the inner mesh X-node
A = blktridiag(Amd,Asub,Asup,n);
%spy(A)
%full(A)
%%%I.1.a.Subfunction "blktridiag"
% It is a subfunction of the "Bl3D" and sub-subfunction of the main function "combine"
function A = blktridiag(Amd,Asub,Asup,n)
% BLKTRIDIAG: computes a sparse (block) tridiagonal matrix with n blocks
% usage: A = BLKTRIDIAG(Amd,Asub,Asup,n) % identical blocks
% usage: A = BLKTRIDIAG(Amd,Asub,Asup) % a list of distinct blocks
Amdg;
% Which mode of operation are we in?
if nargin==4
% replicated block mode
% verify the inputs in this mode are 2-d arrays.
if (length(size(Amd))~=2) || ...
(length(size(Asub))~=2) || ...
(length(size(Asup))~=2)
error 'Inputs must be 2d arrays if a replication factor is provided'
end
% get block sizes, check for consistency
[p,q] = size(Amd);
if isempty(Amd)
error 'Blocks must be non-empty arrays or scalars'
end
if any(size(Amd)~=size(Asub)) || any(size(Amd)~=size(Asup))
error 'Amd, Asub, Asup are not identical in size'
end
if isempty(n) || (length(n)>1) || (n<1) || (n~=floor(n))
error 'n must be a positive scalar integer'
end
% scalar inputs?
% since p and q are integers...
if (p*q)==1
if n==1
A = Amd;
else
% faster as Jos points out
A = spdiags(repmat([Asub Amd Asup],n,1),-1:1,n,n);
end
% no need to go any farther
return
end
% use sparse. the main diagonal elements of each array are...
s=repmat(Amdg(:),1,1); % Replicate and tile array
v=[s;repmat(Amd(:),n-2,1);s]; % Поставяме Amdg в началото и края на гл.диагонал
% then the sub and super diagonal blocks.
if n>1
% sub-diagonal
v=[v;repmat(Asub(:),n-1,1)];
% super-diagonal
v=[v;repmat(Asup(:),n-1,1)];
end
elseif nargin==3
% non-replicated blocks, supplied as planes of a 3-d array
% get block sizes, check for consistency
[p,q,n] = size(Amd);
if isempty(Amd)
error 'Blocks must be (non-empty) arrays or scalars'
end
if (p~=size(Asub,1)) || (q~=size(Asub,2)) || (p~=size(Asup,1)) || (q~=size(Asup,2))
error 'Amd, Asub, Asup do not have the same size blocks'
end
if (n>1) && (((n-1) ~= size(Asub,3)) || ((n-1) ~= size(Asup,3)))
error 'Asub and Asup must each have one less block than Amd'
end
% scalar inputs?
if (p*q)==1
if n==1
A = Amd(1);
else
% best to just use spdiags
A = spdiags([[Asub(:);0], Amd(:), [0;Asup(:)]],-1:1,n,n);
end
% no need to go any farther
return
end
% The main diagonal elements
v = Amd(:);
% then the sub and super diagonal blocks.
if n>1
% sub-diagonal
v=[v;Asub(:)];
% super-diagonal
v=[v;Asup(:)];
end
else
% must have 3 or 4 arguments
error 'Must have either 3 or 4 arguments to BLKTRIDIAG'
end
% now generate the index arrays. first the main diagonal
[ind1,ind2,ind3]=ndgrid(0:p-1,0:q-1,0:n-1);
rind = 1+ind1(:)+p*ind3(:);
cind = 1+ind2(:)+q*ind3(:);
% then the sub and super diagonal blocks.
if n>1
% sub-diagonal
[ind1,ind2,ind3]=ndgrid(0:p-1,0:q-1,0:n-2);
rind = [rind;1+p+ind1(:)+p*ind3(:)];
cind = [cind;1+ind2(:)+q*ind3(:)];
% super-diagonal
rind = [rind;1+ind1(:)+p*ind3(:)];
cind = [cind;1+q+ind2(:)+q*ind3(:)];
end
% build the final array all in one call to sparse
A = sparse(rind,cind,v,n*p,n*q);
end
end
%%I.2.Subfunction ReadyF, Bl3D is a subfunction of the main function "combine" and gives the right side of the algebraic system
function F3 = ReadyF
global Ugd Vgd Ypred;
load 'Ypred.dat'
load 'Ugd.dat'
load 'Vgd.dat'
n; NUI; D;
Y=Ypred;
y=reshape(Y,3,[]);
Dy=D*y;
format long
Gl; Gr; K;
i=1;
ugvg1=[ Ugd(nt,i);Vgd(nt,i);0];
Glugvg1=Gl*ugvg1;
Gld=Dy(:,1)+Glugvg1;
i=NUI;
ugvgNUI=[Ugd(nt,i);Vgd(nt,i);0];
GrugvgNUI=Gr*ugvgNUI;
Grd=Dy(:,n)+GrugvgNUI;
ugvg=zeros(3,1);
for i=2:NUI-1
ii=i-1;
iii=3*ii;
ugvg(:,ii)=[Ugd(nt,i);Vgd(nt,i);0];
end;
Kugvg=K*ugvg;
FKugvg=reshape(Kugvg,[iii 1]);
F2=FKugvg;
Dy1=Dy;
Dy1(:,1)=[];
Dy2=Dy1;
Dy2(:,n-1)=[];
a=3*n-6;
Dy3=reshape(Dy2,[a 1]);
F1=[Gld;Dy3;Grd];
F3=F1+F2;
end
end
P.S. The code will need 3 external data files, should I post or send them? The line 31-33 contain the information and the question? I hope that you will be able to understand.
In fact, there is something more. If you run the code you will see the it produces one unwanted variable "ans=" , which I want to suppress?
Thank you again and excuse me for the heavy code, but I'm not sure that with a sample short one the question would be clear ask!!! Regards

Accepted Answer

Matt Kindig
Matt Kindig on 18 Apr 2012
I didn't go through the entire code, but I think you want the line to be:
csvwrite(['Y', num2str(nt), '.dat'],Y);
By the way, the 'ans' that appears is because you have a command that is not terminated by a semicolon. I can't seem to find it right now, however.
  3 Comments
D.Chehov
D.Chehov on 19 Apr 2012
Thank you very much about the useful code and the whole information.
About the appearance of "ans", I've checked very carefully every line in the code for termination with semicolon. In addition, if somewhere the semicolon is omitted Matlab dusplays a warning message. So the problem is somewhere else and will keep looking on it, any suggestions are highly appreciated!
Regards
Jan
Jan on 19 Apr 2012
Accepting an answer means, that the problem is solved.

Sign in to comment.

More Answers (1)

Jan
Jan on 19 Apr 2012
You can find the line, which causes the "ans =" output by using the debugger. Simply step through the code line by line until the "ans " appears.
There are several strange lines, which do not have any effect:
n; NUI; D;
...
Asub;
Amd;
Asup;
Amdg;
n;
...
Gl; Gr; K;
What do you want to achieve with such lines? I think beside confusing the reader they are useless.
Starting a function by clear all is a waste of time without any advantage. This command clear all variables in the current workspace (but there are no variables in a function without inputs) and it removes all loaded functions from the memory. Reloading and parsing a functions need a lot of time, therefore a clear all can reduce the speed dramatically.
It is not clear which are the lines 31 to 33.
The variable iii is not used inside the for i=2:NUI-1 loop, so it would be faster and cleaner to move the calculation out of the loop.
  3 Comments
Matt Kindig
Matt Kindig on 19 Apr 2012
How do you call the function? If you call it at the command line, i.e.
>> timecycle1 %note no semicolon here
then the first output of the function will be assigned to the variable 'ans' and output to the command window.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!