Can you help me find the mistake in my Matlab code?

I use Matlab to find the optimal number of wells, however when I run the file of Optimization.m, it seems not work. I don't know where it is wrong. I have checked my functions, they are correctly, but when I set direct number to NPV.m, it run too slow. So, can you help me find my mistake in Optimization.m as the attached file and how to improve speed of process

6 Comments

Tien - rather than attaching a zip file, which some contributors may be reluctant to open (like me!), please attach the MATLAB m files to your question. Also, please clarify what you mean by it seems not to work. Describe what you observe (the errors?) and then describe what you expect to happen.
As for performance, try using the MATLAB profile function to analyze the execution time for your functions.
I send a zip file because Matlab limit 10 file each day. The Optimization.m is file I need to run, but I wait two hours, it is nothing display on screen. So, I ask to experts to check my code what it is wrong? If files are correctly, how can I run quickly or repair to gain result faster ?
Tien - I ran your code and it finished within about twenty seconds. The wellCountNPV was all zeros, so I suspect that this isn't the answer that you are looking for. As for getting stuck (for two hours?), this could be because of the while loop in the Optimization function. A loop like this is the usual suspect as a condition like
while diff > 0
may always evaluate to true, and so you become stuck in an infinite loop. To prevent this from occurring, you will need to and an additional condition to ensure that you only evaluate the while block if diff is greater than zero AND you have only evaluated (iterated) less than some upper bound. For example, you could do
iterCount = 0;
while diff > 0 && iterCount < 1000
iterCount = iterCount + 1;
% etc. (your code)
end
You should also (to avoid errors) rename this diff variable since this is also the name of a MATLAB built-in function (see diff for details).
Thank for your answer.
It is not the result I expect. When I replace directly single numbers into NPV.m, it run well. So, why use a loop taking from VariableSet.xlsx it return to 0 ? I am still don't understand. As you said, (while diff >0) may always evaluate to true, but in my case is don't occur because of when the number of well (N) increase, the cost is also increase, so NPV reach the maximum about at 12 wells. I send you an attached file, if you understand my problem, please provide some advice to resolve problem
Tien - are you sure that the data you are reading from each column corresponds to the variable that you are reading it into to? And that the units are the same? For example, your RFpl (recovery factor at end of plateau) has values between 30 and 70 (from the VariableSet.xlsx file, but your example from Tfigure43.m assigns 0.5 to this same variable. Does this mean that the wrong column from the Excel file is being assigned to this variable or has it not been converted correctly? Please verify that the data in the file is correct and that you are reading each column correctly.
As for performance, look at the following lines from Optimization.m
if NPV(j, OGIP, PGi, C, n, Pwh, ID, depth, cost, price, rate, RFpl) <= 0
wellCountOpt = 0;
npvOpt = 0;
wellCountNPV(i,:) = [wellCountOpt,npvOpt];
else
delta = 10;
npvOld = NPV(j, OGIP, PGi, C, n, Pwh, ID, depth, cost, price, rate, RFpl);
npvNew = NPV(j+delta, OGIP, PGi, C, n, Pwh, ID, depth, cost, price, rate, RFpl);
diff = npvNew - npvOld;
% etc.
Note that in the if condition you call
NPV(j, OGIP, PGi, C, n, Pwh, ID, depth, cost, price, rate, RFpl)
and if this isn't zero, then we calculate this again for npvOld in the else block. Rather than doing this twice, just do it the once as
npvResult = NPV(j, OGIP, PGi, C, n, Pwh, ID, depth, cost, price, rate, RFpl);
if nvpResult <= 0
% do something
else
delta = 10;
nvpOld = nvpResult;
% etc.
end
The same can be applied to your Tfigure43.m file which duplicates calls to this function.
Thank you very much. You have found my mistake, RFpl in VariableSet.xlsx lies in interval (30% -> 70%), so data in Excel file is wrong and the result is incorrect. Best regards.

Sign in to comment.

 Accepted Answer

Requested that Tien verify that the data being read from each column was correct with respect to units and variable descriptions. Pointer out that the RFpl (recovery factor at end of plateau) has values between 30 and 70 (from the VariableSet.xlsx file), but that the example from Tfigure43.m assigned 0.5 to this same variable.
Tien verified that the data in the column was incorrect (percentages rather than the decimal equivalent).

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!