Generation of a nested loop structure

Hello,

In the attached procedure file I would like to generate a second loop so that in case of years = 5, I would arrive at five different outcomes, namely the vaues for year one; years one and two, years one, two, and three, years one up to and including four, and years one up to and including five. I have tried to generate a nested loop structure, but all what I achieved so far is that I only got the final year outcome (that is, the year 5 value in the example). The attached lines of code only relate to my first loop, that does what it is required to do. I have shown none of my failed attempts to generate a nested loop. I would highly appreciate any help. Thank you in advance.

Eduard
why not creating a results wave in your function:

Make/O/N=(years) Results

and then store the result per year in it, such that your do-while loop contains something like this:

Results[i] = //here comes your expression

then you don't need an extra loop and you don't need to think about how to return the five values.

Cheers
C

BTW: it would be better if you can post your code here as text, instead of posting a picture ....then it's easier to work on it




Or to combine ChrLie's approach with your function:
Make/O/N=(years) Results
results=Marketvalue(...,p)

where p is the value for the 'years' parameter.
Hello,

Many thanks for the ideas both of you provided. I followed the first advice, partly because I did not see how to implement an approach of combining ChrLie's approach with my own function. Is it meant that the code results = Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, p) can be used and if so, where am I suggested to put this line of code?

Think that after all I found the appropriately way to show the code as text (see the lines below).

#pragma rtGlobals=1     // Use modern global access method.
Function Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years)
Variable Inflation, Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years

variable present, sumpresent

Make/O/N=(years) Results

Variable sum=0
Variable i=0


do
    sum += 1
    i += 1

    Present = (Rent-Opcosts)*((1+Inflation)^i)/((1+Discountrate)^i)- Maintenance*((1+Buildingcostrise)^i)/((1+Discountrate)^i)
    Sumpresent += Present
    Results[i] = Sumpresent + WOZ*WOZfactor*(((1+Inflation)^i)/((1+Discountrate)^i))
       
while(i +1< years)
   
End


The function now does exactly what I like to do with it.

Eduard
Eduard Kas wrote:
Many thanks for the ideas both of you provided. I followed the first advice, partly because I did not see how to implement an approach of combining ChrLie's approach with my own function. Is it meant that the code results = Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, p) can be used and if so, where am I suggested to put this line of code?

#pragma rtGlobals=1     // Use modern global access method.
Function Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years)
Variable Inflation, Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years
 
variable present, sumpresent
 
Variable i=0
 
do
    i += 1
 
    Present = (Rent-Opcosts)*((1+Inflation)^i)/((1+Discountrate)^i)- Maintenance*((1+Buildingcostrise)^i)/((1+Discountrate)^i)
    Sumpresent += Present
 
while(i +1< years)

return Sumpresent + WOZ*WOZfactor*(((1+Inflation)^i)/((1+Discountrate)^i))
 
End


You can the write
Make/O/N=(years) Results
Results=Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, p)
using the above modified procedure straight into the command window.

BTW what purpose serves the variable sum in your code? This can be ambiguous, because it is also an Igor built-in function.

Andreas
In the past weeks functions such as the one above have been very helpful for me and in the process I also discovered the sum variable and the 'sum += 1' part to be redundant, as Andreas already hinted at.

Eduard
awirsing wrote:

#pragma rtGlobals=1     // Use modern global access method.
Function Marketvalue (Inflation,Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years)
Variable Inflation, Discountrate, Pricerise, Buildingcostrise, Rent, WOZ, Opcosts, Maintenance, WOZfactor, years
 
variable present, sumpresent
 
Variable i=0
 
do
    i += 1
 
    Present = (Rent-Opcosts)*((1+Inflation)^i)/((1+Discountrate)^i)- Maintenance*((1+Buildingcostrise)^i)/((1+Discountrate)^i)
    Sumpresent += Present
 
while(i +1< years)

return Sumpresent + WOZ*WOZfactor*(((1+Inflation)^i)/((1+Discountrate)^i))
 
End



I believe the following will also work, be faster, and allow the return of the "present" value versus "year" in a wave if needed.

Function MMarketValue(...)

    // all before the same except no need for variable i=0 statement
    // use the code below in place of the do-while loop

   // remove the /FREE flag to keep the wave "pwave" containing present value versus year

   make/O/N=(years)/FREE pwave

   // this generates the present value in pwave

   pwave = (Rent-Opcosts)*((1+Inflation)^(p))/((1+Discountrate)^(p))- Maintenance*((1+Buildingcostrise)^(p))/((1+Discountrate)^(p))

   // this sets the year zero and current year values

   pwave[0] = 0; pwave[years-1]=WOZ*WOZfactor*(((1+Inflation)^(years-1))/((1+Discountrate)^(years-1))

   // this sums all the values

   sumpresent = sum(pwave)
   
   return (sumpresent)

end


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville