Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Loop output not saving to a cell array. Only last solution saving to cell

3 vues (au cours des 30 derniers jours)
Ali Berkcan Yagiz
Ali Berkcan Yagiz le 6 Mai 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
clear all
close all
clc
%load data
load('H.mat')
load('facies.mat')
load('q.mat')
%domain size
Nx = 230;
Ny = 240;
Nz = 95;
dx = 100;
dy = 200;
dz = 5;
Lx = Nx*dx;
Ly = Ny*dy;
Lz = Nz*dz;
% particle starting location
x_start = 118*dx+50; % x starting position of the particle (0,0 is the bottom left corner of the domain)
y_start = 153*dy+100; % y starting position of the particle
z_start = 58*dz;
%Making Particle Tracking Automatic by keeping x&y constant but changing z
for ii=0:50:100
x_start=118*dx+ii;
%flow
qx=q.qx;
qy=q.qy;
qz=q.qz;
geometry = facies;
% set porosity value
por = 0.3;
% evaluate velocity from fluxes
vx=-qx./por; % sign - is for being consistent with sdr
vy=+qy./por; % sign + is for being consistent with sdr
vz=-qz./por;
% evaluate inlet and outlet velocity for each cell
Vxin = vx(:,1:(end-1),:);
Vxout = vx(:,2:end,:);
Vyin = vy(1:(end-1),:,:);
Vyout = vy(2:end,:,:);
Vzin = vz(:,:,1:(end-1),:);
Vzout = vz(:,:,2:end,:);
% seed = 16843592;
% RandStream.setGlobalStream(RandStream('mt19937ar','seed',seed));
% tic
N = 1;
dt=0.05; %in days
% % D = 6.093e-11; % diffusion coefficient
D=0; % diffusion/dispersion coefficient
tic
for i=1:N
[xnew,ynew,znew]=PTrack_function_3D(Nx,Ny,Nz,dx,dy,dz,Lx,Ly,Lz,Vxin,Vxout,Vyin,Vyout,Vzin,Vzout,x_start(i),y_start(i),z_start(i),D,dt,geometry);
end
toc
%%plot trajectory
figure
xv=(0+dx/2:dx:Lx-dx/2);
yv=flip(0+dy/2:dy:Ly-dy/2);
imagesc(xv,yv,geometry(:,:,56))
hold on
plot(xnew,ynew,'.r')
figure
scatter3(xnew,ynew,znew, 'filled', 'r')
end
I want to save all outputs from the functions. But it only saves the last step of iteration. How can I solve this?

Réponses (1)

Raj
Raj le 6 Mai 2019
1) Your comment says:
"%Making Particle Tracking Automatic by keeping x&y constant but changing z"
However you are changing x_start inside loop and keeping y and z constant.
2) What is the need for 'for' loop here? (since N is equal to 1 this loop will run only once)
for i=1:N
[xnew,ynew,znew]=PTrack_function_3D(Nx,Ny,Nz,dx,dy,dz,Lx,Ly,Lz,Vxin,Vxout,Vyin,Vyout,Vzin,Vzout,x_start(i),y_start(i),z_start(i),D,dt,geometry);
end
3) Now I understand you are asking about storing xnew,ynew and znew for each iteration of 'for ii=1:50:100'. In that case, you can predefine your outputs as empty arrays and extract the function output to dummy variables. Something like this before your 'ii' loop starts:
xnew=zeros(3,1);
ynew=xnew;
znew=ynew;
Then something like this inside the 'for' loop:
[A,B,C]=PTrack_function_3D(Nx,Ny,Nz,dx,dy,dz,Lx,Ly,Lz,Vxin,Vxout,Vyin,Vyout,Vzin,Vzout,x_start,y_start,z_start,D,dt,geometry);
xnew(ii/50+1,1)=A;
ynew(ii/50+1,1)=B;
znew(ii/50+1,1)=C;
Now at the end of "for ii=1:50:100" loop your xnew,ynew and znew will have 3 elements each corresponding to ii=0 case, ii=50 case and ii=100 case.
4) Shift the plot trajectory portion of code outside the 'for' loop.

Cette question est clôturée.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by