for loop not working in fixed point iteration while loop
Show older comments
I donot know how to solve this problem
after 1st of iteration I got this error in to for loop.
the code attached with its variables

clc
clear all
load('results.mat')
N_pr = 99;
P_sat = Pb;
P_abandon = 1700;
delh_pr = (P_sat-P_abandon)/N_pr;
for pr=1:N_pr+1
Pavg_pr(pr)=P_abandon+(pr-1)*delh_pr;
end
Pavg_pr = sort(Pavg_pr,'descend');
Pavg_pr=Pavg_pr';
%--------------------------------------------------
Vpi = (W*Bwi + N*Boi)*(cf*(p0-P_sat));
%--------------------------------------------------
%Interpolate PVT data @ P_sat & Pavg_pr
%--------PVT @ P_sat-----------
Boi = pchip(P_o,Bo,5200);
Bo_Pb = pchip(P_o,Bo,P_sat);
Bg_Pb = pchip(P_g,Bg,P_sat);
muo_Pb=pchip(P_o,mu_o,P_sat);
mug_Pb=pchip(P_g,mu_g,P_sat);
Rs_Pb = pchip(P_o,Rs,P_sat);
Rv_Pb=pchip(P_g,Rv,P_sat);
%---------- PVT @ Pavg_pr-------
Bo_pr = pchip(P_o,Bo,Pavg_pr);
Bg_pr = pchip(P_g,Bg,Pavg_pr);
muo_pr=pchip(P_o,mu_o,Pavg_pr);
mug_pr=pchip(P_g,mu_g,Pavg_pr);
Rs_pr = pchip(P_o,Rs,Pavg_pr);
Rv_pr=pchip(P_g,Rv,Pavg_pr);
%--------------------------------------------------------------------
%calculate Np @ P_sat %Ignored Vpi
Np_sat = ((N*(Bo_Pb-Boi))+Vpi)/Bo_Pb;
%--------------------------------------------------------------------
%calculate Bto , Xn , Xg , Fn & Fg %Ignored Vpi in Fn & Fg
Bto_pr = (Bo_pr.*(1-Rv_pr.*Rs_Pb)+Bg_pr.*(Rs_Pb-Rs_pr))./(1-Rs_pr.*Rv_pr);
Xn = (Bo_pr-(Bg_pr.*Rs_pr))./(1-(Rv_pr.*Rs_pr));
Xg = (Bg_pr-(Bo_pr.*Rv_pr))./(1-(Rv_pr.*Rs_pr));
Fn = Xn./(Bto_pr-Bo_Pb);
Fg = Xg./(Bto_pr-Bo_Pb);
%-----------------------------------------------------------------------
dNp=zeros(length(Pavg_pr),1);
dGp =zeros(length(Pavg_pr),1);
Np_cum=zeros(length(Pavg_pr),1);
Gp_cum = zeros(length(Pavg_pr),1);
Rp_guess = 10;
dNp(1)=Np_sat;
dGp(1)=Np_sat*Rp_guess;
Np_cum(1)=dNp(1);
Gp_cum(1) =dGp(1);
Error = 100;
iterationss=0;
while Error > 0.5E-3
%--------------------------------------
%Calculate dNp , dGp , cum Np , cum Gp
for y =2:length(Pavg_pr)
dNp(y)=((N-Np_cum(y-1)*Fn(y))-(Gp_cum(y-1)*Fg(y)))/...
(Fn(y)+(Rp_guess*Fg(y)));
dGp(y)=dNp(y)*Rp_guess;
Np_cum(y) =dNp(y)+ Np_cum(y-1);
Gp_cum(y) = dGp(y)+Gp_cum(y-1);
end
%Np_cum =cumsum(dNp);
%Gp_cum = cumsum(dGp);
%--------------------------------------
%Calculate Saturations and Relative Permeabilities
%Sg_pr , Krg_pr , So_pr , Kro_pr
Wps=W;
Bw_pr=Bwi;
Sg_pr = Bg_pr.*((G-Gp_cum)-(N-Np_cum).*Rs_pr)./((W-Wps).*Bw_pr.*(1-Rs_pr.*Rv_pr)+(G-Gp_cum).*(Bg_pr-Rv_pr.*Bo_pr)+(N-Np_cum).*(Bo_pr-Rs_pr.*Bg_pr));
So_pr = Bo_pr.*((N-Np_cum)-(G-Gp_cum).*Rv_pr)./((W-Wps).*Bw_pr.*(1-Rs_pr.*Rv_pr)+(G-Gp_cum).*(Bg_pr-Rv_pr.*Bo_pr)+(N-Np_cum).*(Bo_pr-Rs_pr.*Bg_pr));
Krg_pr = pchip(SoT,KrgT,So_pr);
Kro_pr = pchip(SoT,KroT,So_pr);
%-------------------------------------
%Calculate Rp_new
Rp_new = ((Krg_pr./(mug_pr.*Bg_pr))+((Kro_pr.*Rs_pr)/(muo_pr.*Bo_pr)))./...
((Kro_pr./(muo_pr.*Bo_pr))+((Krg_pr.*Rv_pr)/(mug_pr.*Bg_pr)));
Error = abs(Rp_guess-Rp_new);
Rp_guess = Rp_new;
iterationss=iterationss+1;
clc
Error;
end
6 Comments
KSSV
on 1 Jul 2023
Variable Rp_guess is matrix of size 100x100. This is the reason for error.
Replace this line:
dNp(y)=((N-Np_cum(y-1)*Fn(y))-(Gp_cum(y-1)*Fg(y)))/(Fn(y)+(Rp_guess*Fg(y)));
with:
dNp(:,:,y)=((N-Np_cum(y-1)*Fn(y))-(Gp_cum(y-1)*Fg(y)))./(Fn(y)+(Rp_guess*Fg(y)));
Hazem Mohamed
on 1 Jul 2023
Hazem Mohamed
on 1 Jul 2023
KSSV
on 1 Jul 2023
dNP will be of size 100x100 becuase Rp_guess is 100x100. You need to initialize dNP before loop.
Hazem Mohamed
on 1 Jul 2023
KALYAN ACHARJYA
on 2 Jul 2023
Based on the information you provided, there is an error that is directly visible. The variable Rp_guess is initially defined as a scalar value, but later in the code, it is replaced by Rp_new, which is a 100x100 matrix.
To address this issue, you can modify the code as follows:
- Declare dNp as a cell array to accommodate the 100x100 data:
dNp = cell(100, 100);
Alternatively, ensure that only scalar values are returned by the following statements (after exiting the loop).
((N-Np_cum(y-1)*Fn(y))-(Gp_cum(y-1)*Fg(y)))/...
(Fn(y)+(Rp_guess*Fg(y)));
2. Replace the calculation statement with an appropriate loop to generate the 100x100 data and store it in the dNp cell array:
dNp(y)=((N-Np_cum(y-1)*Fn(y))-(Gp_cum(y-1)*Fg(y)))/...
(Fn(y)+(Rp_guess*Fg(y)));
By making these modifications, you can generate the 100x100 data using element-wise division (./) and store it in the dNp cell array.
Please note that I assumed you wanted to replace Rp_guess with Rp_new. If there are additional details or specific requirements, please provide them, and I'll be happy to assist you further.
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!