PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How to convert a recursive algorithm to a function that outputs a number, given the time

 October 07, 2022     javascript, math, statistics     No comments   

Issue

I have a recursive algorithm that runs multiple times, replacing currentValue each time with the output of the previous run (except for the first run, where the value is supplied by the user). Each run calculates the value one year later.

value = constant
+ population_factor * coeff_population_factor
+ currentValue * coeff_currentValue
+ initialValue * coeff_initialValue%
+ duration_of_period * coeff_duration_of_period

I would like to be able to convert that into a function of time, so that for a given value t in years, it would return the expected value at that point? I think that would be a regression algorithm, but I'm not sure how to get to that.

I have modelled the curve using the the formula above in excel to get my coefficients, but that obviously only runs on my computer and I'd like to run this in the cloud using node or something, so I can develop some front-ends against it. Ideally some pointers using pure maths or JS, or a library, even, would be greatly appreciated!


Solution

I believe you are looking for a for loop. Try this:

# define all of the variables here
currentValue = #put the first-run value here
for i in range(t):
    currentValue = constant \
                   + (population_factor * coeff_population_factor) \
                   + (currentValue * coeff_currentValue) \
                   + (initialValue * coeff_initialValue) \
                   + (duration_of_period * coeff_duration_of_period)
# currentValue will have the final result once outside of the for loop

It is not recursive and still gives the accurate answer!



Answered By - Seth
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing