Save each loop values, without overwriting
Show older comments
Hi. I think that my program is not saving each loop value for the Matrix Qabal which comes from Q, and for QQ neither, but I just want to solve the Q or Qabal, which it should be a matrix (22x24)
MND is a matrix (14x25) and MTB(which is inside the function has 22 rows).
for j=2:25
QQ=MND(:,j); %m3/s Vector containing fixed nodal discharges with each PF
[Y,Df,Q,VK1,Vn1]=funF2(H,QQ,MTB,VK,Vn,Vii,g,vi);
er=sum(abs(Y)); %Initial guess solution error
nit=0; %Iteration counter initiation
%Iterative solution search
while er>=eps && nit<=nma
VK=VK1;
Vn=Vn1;
H(Vii,j)=H(Vii)-Df\Y; %New extended H vector (solution vector)
[Y,Df,Q,VK1,Vn1,Qij]=funF2(H,QQ,MTB,VK,Vn,Vii,g,vi);
Qabal(Hores,j)=Q(Hores);
er=sum(abs(Y)); %New approximate solution error
nit=nit+1; %Iteration counter update
%Hores=Hores+1;
HH=H(:,2:end); %Only found H columns
end
end
I don't know If i have explained it well, maybe is needed the other part of the program to find it out, but I think that with that should be enough.
Thank you.
Answers (1)
for j=2:25
QQ=MND(:,j-1); %m3/s Vector containing fixed nodal discharges with each PF
[Y,Df,Q,VK1,Vn1]=funF2(H,QQ,MTB,VK,Vn,Vii,g,vi);
The above will overwrite Q each pass with the results for that output argument from funF2 given the input using the j column of MND and the other inputs. We don't know what the dimensions are, presuming it is also a vector and you wish to save the various columns into an array Q instead, you would need
[Y,Df,Q(:,j),VK1,Vn1]=funF2(H,QQ,MTB,VK,Vn,Vii,g,vi);
instead and if that is so, you should "preallocate" Q as
Q=zeros(size(MTB,1),size(MND,1)-1); % preallocate
Similar holds for the other output array excepting you've written
Qabal(Hores,j)=Q(Hores);
which will load individual elements but you commented out the defining line that incremented the row index Hores. But, you can only have a single element to assign to a specific array location and it appears your function returns a vector so you'll need to (it would seem) also use
Qabal(:,j)=Q(Hores);
ERRATUM
Dang, missed an edit when pasted the line...intended to write
Qabal(:,j)=Q;
Of course, this raises the question of just how the iteration is supposed to be working--looks like maybe it's trying to update individual locations somehow, not the full vector but we don't know the intent just from code (particularly if the code doesn't work as intended :) ).
2 Comments
Jordi Escola
on 27 Apr 2016
dpb
on 27 Apr 2016
Not sure what "this" refers to in the above but see the ERRATUM and comment following it to my previous Answer. Maybe the question raised there will lead to some enlightenment to resolve the issues.
As stated there, don't know any of us here can help without a more definitive statement of the problem trying to be solved, not just the code and a syntax issue with it.
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!