cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Formula that uses previous result

MR_10045948
2-Guest

Formula that uses previous result

Hello!

I am trying to write this formula:

MR_10045948_0-1633514673630.png

It uses the previous result (i-1) to calculate the next result (i). 

How should it be written in mathcad? Right now I get an overflow or infinite recursion error.

 

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:MR_10045948)

It's no problem to define a recursive function in Mathcad, but you have to provide a starting value.

LucMeekes_0-1633516502248.png

 

In your case: Where does i start, and what is the corresponding Ts,i value ?

 

Be sure to attach your worksheet, that'll help us better to help you.

 

Success!

Luc

View solution in original post

4 REPLIES 4
LucMeekes
23-Emerald III
(To:MR_10045948)

It's no problem to define a recursive function in Mathcad, but you have to provide a starting value.

LucMeekes_0-1633516502248.png

 

In your case: Where does i start, and what is the corresponding Ts,i value ?

 

Be sure to attach your worksheet, that'll help us better to help you.

 

Success!

Luc

Thank you, I got it working! Recursive functions was what I was looking for. Although mathcad seems to get very slow when calculating recursive functions. For i=20, it takes around 10 seconds to calculate it, but at i=25 it already takes over a minute.

I suspect that this sure could be speeded up significantly.

Ts.,i-1 occurs twice in your formula and I guess you are calling the recursive function twice, which could be avoided by either rearranging the formula so T.s,i-1 occurs just once or by calculating it once in the function and store it in a variable.

Furthermore I suspect that T.g is provided as a vector of values and you are looking for the vector T.s and not for a generic function to calculate arbitrary T.s values. If thats true, the calculation should/could be done significantly faster as no recursion down to the first value is necessary.

But without seeing a´what exactly you do and need its hard to say more.


@MR_10045948 wrote:

Thank you, I got it working! Recursive functions was what I was looking for. Although mathcad seems to get very slow when calculating recursive functions. For i=20, it takes around 10 seconds to calculate it, but at i=25 it already takes over a minute.


Following up on Werner's comment, the structure of your problem is near (for values of near) to the Fibonacci recursively-defined function and is a classic example of exponential calculation times when "naively" implemented.  The following screenshots (Mathcad Express 7.0) illustrate the problem.

 

Range VariableRange Variable

 

Literal Recursive ImplementationLiteral Recursive Implementation

 

(Added later):  The reason that this happens is that at each call of fib, it calls fib recursively twice, first for f(n-1) and then for fib(n-2).  In doing so, it makes no use of previous calculations, so fib(n-2) is called twice, once by fib(n) and once by fib(n-1).   In turn each of these calls to fib also leads to two calls (until it hits the base case)).   Consequently, the number of calls grows roughly as the power of 2 - (Ο(((1+√5)/2)ⁿ) or about O(1.618ⁿ) to be a little more exact).

 

Accumulating Recursvive ImplementationAccumulating Recursvive Implementation

 

Stuart

Top Tags