Translating (Transforming) a recursive function

In summary, the conversation discusses the process of shifting and manipulating equations and initial values in order to compute the next values in a sequence. The individual is unsure of the mathematical reasoning behind the process, but is seeking a more formal approach to achieve the desired outcome. The idea of letting the program handle shifts is also mentioned.
  • #1
fred4321
4
0
Hi,

I am having trouble understanding how this works.

I am giving the following:
y[k+2] - y[k+1] + 0.24y[k] = f[k+2] - 2f[k+1];

y[-2] = 1, y[-1] = 2;

f[k] = 0 for k < 0;
f[k] = k for k >= 0;

I would like to have a program compute the next values in the sequence, so, I need y[-2] = 1 to become y[1] = 1 and y[-1] = 2 to become y[2] = 1 (so that the array indexing works, e.g., I can access a negative location of an array).

I let k' = k + 1 so that I'd get:
y[k'+3] - y[k'+2] + 0.24y[k'+1] = f[k'+3] - 2f[k'+1];
Then I made:
f[k] = 0 for k < 0;
f[k] = k for k >= 0;
become
f[k'] = 0 for k' < 3;
f[k'] = k for k' >= 3;
and
y[-2] = 1, y[-1] = 2;
become
y[1] = 1, y[2] = 2;

So now I have:
y[k'+3] - y[k'+2] + 0.24y[k'+1] = f[k'+3] - 2f[k'+1];
f[k'] = 0 for k' < 3;
f[k'] = k for k' >= 3;
y[1] = 1, y[2] = 2;

And now when I let k = 0, k[3] gives me the value that k[0] gave me in the old equation, which is exactly what I want.

My issue is, I don't understand how, mathematically, this works. For example, I don't understand how I went from:
f[k] = 0 for k < 0;
f[k] = k for k >= 0;
to
f[k'] = 0 for k' < 3;
f[k'] = k for k' >= 3;

if k' = k + 1.

It seems as though I've shifted the equation (y[k+2] - y[k+1] + 0.24y[k] = f[k+2] - 2f[k+1];) over by 1 unit in the positive x direction; however, I've shifted the initial values (y[-2] = 1, y[-1] = 2;) and the f's restrictions (k < 0; and k >= 0) over by 3 units.What I'm thinking is:
The original question should be:
f[k'] = 0 for k < k[0];
f[k'] = k for k' >= k[0];
y[k[0]-2] = 1, y[k[0]-1] = 2;

What would be the correct, more formal approach to achieving what I want. Also, should the original question be as I've written above?

I'm pretty sure that the equation, as it's give, only produces the 'correct' answer, when k[0] = -2.

Thank you for your time, I realize that this question is rather lengthy.
 
Last edited:
Mathematics news on Phys.org
  • #2
You simply shouldn't worry about "f(k) for k< -3" at all. Include, in your program,
"Double f(Double x)
{
Double y= 0;
if (x>= 0) y= x;
return y;
}
and let the program handle shifts.
 
  • #3
Thanks for the reply. What do you mean by, "let the program handle the shift"?

Do you mean by changing adding k+n to the original equation so that it works out?

Also, I know that I didn't say, but I'm actually doing this with MATLAB, I don't think that makes a difference. On know C++ though so I understand your code.
 

1. What is a recursive function?

A recursive function is a function that calls itself within its own definition. This allows the function to repeat itself until a certain condition is met, making it useful for solving problems that involve repetitive tasks.

2. How is a recursive function translated or transformed?

To translate or transform a recursive function, you need to break down the function into smaller parts and then reconstruct it in a different way. This can involve changing the base case, modifying the recursive call, or using a different algorithm altogether.

3. Why would you want to translate a recursive function?

Translating a recursive function can make it more efficient, easier to understand, or better suited for a specific purpose. For example, translating a recursive function into an iterative one can improve its performance, while translating it into a tail-recursive function can reduce the risk of stack overflow.

4. What are some common challenges when translating a recursive function?

One common challenge is determining the correct base case, as it can be easy to create an infinite loop if the base case is not properly defined. Another challenge is finding an appropriate way to replace the recursive call, as not all recursive functions can be translated into a non-recursive form.

5. Are there any tools or techniques that can help with translating recursive functions?

There are various tools and techniques that can help with translating recursive functions, such as recursion tracing and debugging tools, algorithms for converting recursive functions to iterative ones, or using different programming languages or paradigms that support recursion differently.

Similar threads

  • General Math
Replies
8
Views
2K
Replies
2
Views
1K
Replies
4
Views
402
Replies
2
Views
239
  • General Math
Replies
8
Views
1K
  • General Math
Replies
3
Views
806
  • General Math
Replies
3
Views
1K
Replies
4
Views
772
Replies
1
Views
2K
Replies
5
Views
1K
Back
Top