COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Debug problem in a finite difference code

• Email
• Follow

```Dear All
I wrote this small code to calculate unsteady PDE but I got error message. I could not solve it because I am still beginer in using matlab
The error message is:
??? Subscripted assignment dimension mismatch.

Error in ==> FDDD at 55
S(j,i)=S(j,i-1)+dt*(K1*S_D2+K2*D1_2+K33.*D1-S_1);

the Code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Script to solve the counter-current imbibition in reservoir

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
phi=0.3;
mun=0.01;
k=0.02*10^-12;
rohn=660;
rohi=1000;
droh=rohi-rohn;
gamans=0.03;
gamais=.075;
sm=100*10^5;
beta=(gamans-gamais)*sm;
c=0.1;
g=9.8;
K1=beta*k/(phi*mun);
K2=-K1;
K3=k*g*c*droh/(phi*mun);
K4=2*K3;
% K3=0;
% K4=0;
%%% Initialize variables
dz = .01; %each depth step
Nz = 100; % Choose the number of depth steps
Nt = 5000; % Choose the number of time steps
dt =(24*60*60)/Nt; %Length of each time step in seconds
S0=0.;
S = S0*ones(Nz+1,Nt+1);  %Create saturation matrix with Nz+1 rows, and Nt+1 columns

time = [0:12/Nt:12];
S(1,:) = 1;  %Set surface saturation

% X axis
z0=0;
z(1) = z0;
for i = 1:Nz
z(i+1) = z0 + i*dz;
end
% z';
% z(100)';
maxiter = 500;
for iter = 1:maxiter
Slast = S; %Save the last guess
S(:,1) = Slast(:,end); %Initialize S at t=0 to the last temp
for i=2:Nt+1,
for j=2:Nz
D2 = (S(j+1,i-1)-2*S(j,i-1)+S(j-1,i-1))/dz^2;
S_D2=(1-S(j,i-1)).*D2;
D1 = (S(j,i-1)-S(j-1,i-1))/dz;
D1_2 =D1.*D1;
K33=K3./z.^2;
K44=K4./z.^3;
S_1=K44.*(S(j,i-1)-1);
S(j,i)=S(j,i-1)+dt*(K1*S_D2+K2*D1_2+K33.*D1-S_1);

S(Nz+1,i) = 0; % Enforce bottom BC
%         S(Nz+1,i) = 0; % Enforce bottom BC
end
end

err(iter) = max(abs(S(:)-Slast(:)));  %Find difference between last two solutions

if err(iter)<1E-4
break; % Stop if solutions very similar, we have convergence
end
end
if iter==maxiter;
warning('Convergence not reached')
end

figure(1)
plot(log(err)), title('Convergence plot')
% figure(2)
% imagesc([0 12],[0 1],S); title('Saturation plot (imagesc)')
% colorbar
figure(3)
depth = [0:dz:Nz*dz];
contourf(time,-depth,S); title('Saturation plot (contourf)')
colorbar

figure(4)
plot(time,S(1,:),time,S(21,:),time,S(41,:),time,S(61,:))
xlabel('Time (months)'); ylabel('Saturation');
legend('0m','5m','10m','15m', '20m')
```
 0

See related articles to this posting

```On Dec 26, 3:49=A0am, "Mohamed El-Amin" <mfam2...@yahoo.com> wrote:
> Dear All
> I wrote this small code to calculate unsteady PDE but I got error message=
.. I could not solve it because I am still beginer in using matlab
> The error message is:
> ??? Subscripted assignment dimension mismatch.
>
> Error in =3D=3D> FDDD at 55
> =A0 =A0 =A0 =A0 S(j,i)=3DS(j,i-1)+dt*(K1*S_D2+K2*D1_2+K33.*D1-S_1);

whos K1 S_D2 K2 D1_2 K33 D1 S_1

Hope this helps.

Greg
```
 0

```Thanks Greg
but I can not find your message.

Greg Heath <heath@alumni.brown.edu> wrote in message <ef808a40-7101-40db-bc3d-a5083200b143@a32g2000yqm.googlegroups.com>...
> On Dec 26, 3:49 am, "Mohamed El-Amin" <mfam2...@yahoo.com> wrote:
> > Dear All
> > I wrote this small code to calculate unsteady PDE but I got error message. I could not solve it because I am still beginer in using matlab
> > The error message is:
> > ??? Subscripted assignment dimension mismatch.
> >
> > Error in ==> FDDD at 55
> >         S(j,i)=S(j,i-1)+dt*(K1*S_D2+K2*D1_2+K33.*D1-S_1);
>
>
> whos K1 S_D2 K2 D1_2 K33 D1 S_1
>
> Hope this helps.
>
> Greg
```
 0

```On 26 Des, 09:49, "Mohamed El-Amin" <mfam2...@yahoo.com> wrote:
> Dear All
> I wrote this small code to calculate unsteady PDE but I got error message. I could not solve it because I am still beginer in using matlab

The way to handle such problems is to break up the long
and complicated function or script into a number of small,
easily tested functions. Once you have all the required
functions, and have verified that they work correctly,
you can put together the larger function from the
building blocks.

Rune
```
 0

```CORRECTED FOR THE HEINOUS SIN OF TOP-POSTING!

On Dec 26, 8:28=A0am, "Mohamed El-Amin" <mfam2...@yahoo.com> wrote:
> Greg Heath <he...@alumni.brown.edu> wrote in message <ef808a40-7101-40db-=
> > On Dec 26, 3:49=A0am, "Mohamed El-Amin" <mfam2...@yahoo.com> wrote:
> > > Dear All
> > > I wrote this small code to calculate unsteady PDE but I got error mes=
sage. I could not solve it because I am still beginer in using matlab
> > > The error message is:
> > > ??? Subscripted assignment dimension mismatch.
>
> > > Error in =3D=3D> FDDD at 55
> > > =A0 =A0 =A0 =A0 S(j,i)=3DS(j,i-1)+dt*(K1*S_D2+K2*D1_2+K33.*D1-S_1);
>
> > whos K1 S_D2 K2 D1_2 K33 D1 S_1
>
> Thanks Greg
> but I can not find your message.

The just type in the above whos command

Hope this helps.

Greg

```
 0