help with for loop
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
Hello everyone,
I got these two equation. They represent the depth.
The first one is the first depth measured which represents the first value of (Di-1) in the generalized equation below.
I need the code to generate Di until it reaches the condition.
I typed this code buti don't know if it is right as it doesn't fit the calculations i make myself.
Thanks.


% This script represents the Analytical gas lift design
Pks = input('Enter surface Kick off pressure');
dPkm = input('Enter kick off pressure margin');
Phf = input('Enter Wellhead Pressure');
Gs = input('Enter Static Gradient') ;
Pcs = input('Enter surface casing pressure');
dPcm = input('Enter casing pressure margin');
Phfd = input('Enter Wellhead desired Pressure');
Gfd = input('Enter desired flowing Gradient') ;
D = (Pks - dPkm - Phf)/(Gs-(Pks - dPkm)/40000);
D=Di;
for Di=1:10
Di = (Pcs - dPcm - Phfd+(Gs-Gfd)*Di)/(Gs-(Pcs - dPcm)/40000);
if Di > 5000;
break
end
end
3 Kommentare
Walter Roberson
am 23 Nov. 2018
no you use Di as the for loop variable but you also compute it in the loop . you should use different variables
Seif Kazamel
am 23 Nov. 2018
Walter Roberson
am 24 Nov. 2018
you have not defined Di at that point
Antworten (1)
Daniel Vela
am 24 Nov. 2018
Hello, please take a look at this code, it might help you fix what you need. Be aware that it might be wise to use a while loop if you want the program to calculate your value more than a determined number of times until it reaches your condition.
userinput1=input('enter value 1');
userinput2=input('enter value 2');
aMinusOne=userinput1+userinput2 %I define my starting value for a(i-1) through formula (use your formula here)
for i=1:10 % The loop will calculate a 10 times and then exit if condition is not.
a=aMinusOne+5; % the formula takes a(i-1) to calculate the current value of a
if a>150 %if condition is met, the loop ends and prints some nice message.
'the condition was reached'
break
end
aMinusOne=a;% we redifine A(i-1) with the most recently calculated value for A for next iteration.
end
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!