Generation of a nested loop structure
Eduard Kas
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
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
May 27, 2010 at 12:28 am - Permalink
results=Marketvalue(...,p)
where p is the value for the 'years' parameter.
May 27, 2010 at 01:15 am - Permalink
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).
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
May 27, 2010 at 10:49 pm - Permalink
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
May 28, 2010 at 12:27 am - Permalink
Eduard
June 14, 2010 at 03:12 pm - Permalink
I believe the following will also work, be faster, and allow the return of the "present" value versus "year" in a wave if needed.
// 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
June 14, 2010 at 04:15 pm - Permalink