Why am I getting an index looping error when looping over variable?

2 views (last 30 days)
Problem: In short, I am trying to use a loop to evaluate a function over a range of one variable input with everything else held equal and continue getting the error "In an assignment A(I) = B, the number of elements in B and I must be the same."
I've pre-allocated matrices for the output and can't figure out what I'm doing wrong. I've tried to keep the code vectorized by using a 3D variable and element-wise multiplication, but eventually learned that was not the correct method to use.
My code is below, and a more thorough overview of the calculations is discussed below the code.
My desired output from the loop are the pvq_pr and payout_pr values at every value in the variable price
Thanks in advance.
Code:
% Financial Calculations - Price range
price = 3.00:0.50:5.00; % PRICE RANGE TO ITERATE OVER
pvq_pr = zeros(size(price,2),size(qm_30,2)); %PRE-ALLOCATE OUTPUT MATRIX
payout_pr = zeros(size(price,2),size(qm_30,2)); %PRE-ALLOCATE OUTPUT MATRIX
for ii = 1:length(price)
netcfq_pr = sum(qm_30.*(nri.*(price(ii)+pricediff) - price(ii).*prodtax - opex_var) - opex_fix); % NET CASH FLOW FUNCTION
netcfq_pr(isnan(netcfq_pr))=0; % Remove NaN and treat as zeros
netcfq_ec_pr = netcfq_pr.*(netcfq_pr>0); % Test if economic and create variable of only montly cash flows that are positive
cfq_pr = [-wellcost_row; netcfq_ec_pr]; %Concatenate cash outflow with cash infows for PV and IRR calculations
netcfcumq_pr = cumsum(cfq_pr);
pvq_pr(ii) = pvvar(cfq_pr,disc_rate/12);
payout_pr(ii) = sum(netcfcumq_pr<0);
end
Further detail:
qm_30 is the output of a function I wrote which generates production values for a hybrid curve which is both hyperbolic and exponential (representing natural gas production from a well). So with., qm_30 solved for, I am then trying to assess the economic viability of the separate well cases (each column in qm_30) by calculating netcfq_pr (monthly net cash flow) given qm_30, commodity price (price), and certain costs (production taxes, variable operating expenses, and fixed operating expenses).
Calculating netcfq for one (scalar) price was easy, but when I wanted to calculate what netcfq_pr for price = $3.00:0.50:$5.00 I ran into trouble.

Accepted Answer

txvmi07
txvmi07 on 10 Jul 2014
Answering my own question...
First, I was trying to put a matrix where a scalar would be and I fixed this by setting the output for payout_pr from payout_pr(ii) to payout_pr(ii,:) and the same for the variable using the pvvar function. For the other calculations within the for loop I didn't realize I needed to pre-allocate a spot for them (such as netcfq_pr) and thus the loop was only running over the first row of data.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!