简体   繁体   中英

How do I correctly use Recursion in MATLAB?

I have a simple m file that I have created as a recursive function:

function[velocity] = terminal(m,c,t,vi)
%inputs:
% m = mass
% c = coeffcient of drag
% t = time
% vi = initial velocity

if t==18, velocity = vi+(9.8-c/m*(vi))*2;
    return 
end

velocity = vi+(9.8-c/m*(vi))*2;
velocity  %used to print out velocity for debugging
terminal(m,c,t+2,velocity);
end

The calculation of velocity is being done correctly as it prints out every recursion. However the "ans" that is returned at the end is the first calculated value of recursion. My question is how do I correctly setup a matlab function recursively? Or can it be done, and is it better to use a loop?

Although my answer will stray away from programming and into the realm of calculus, it should be noted that you can solve your problem both without recursion or a loop since you can exactly solve for an equation v(t) using integration. It appears that you are modeling Stokes' drag on a falling body, so you can use the fourth formula from this integration table to compute a final velocity vFinal that is achieved after falling for a time tDelta given a starting velocity vInitial . Here is the resulting formula you would get:

vFinal = 9.8*m/c + (vInitial - 9.8*m/c)*exp(-c*tDelta/m);

This will be a more accurate answer than approximating vFinal by making sequential steps forward in time (ie the Euler method , which can display significant errors or instabilities when the time steps that are taken are too large).

Bear with me, haven't done a lot of Matlab for some time now.

But I would simply call your function iteratively:

velocity = vi
for t = 0:2:18
    velocity = velocity+(9.8-c/m*(velocity))*2;
end

Then for each instance of t it would calculate velocity for a given initial velocity and update that value with it's new velocity.

To have it take incremental steps with a size of 2, simply add your step size to it.

Updated in response to the comments

velocity = terminal(m,c,t+2,velocity)

应该管用。

The problem with recursion here is it adds function call overhead for each time through. This will make your code far slower and less efficient in matlab. Use a loop instead and you will be far ahead of things. Yes, there are cases where recursion is a valuable tool. But in many cases a carefully written loop is the better solution.

Finally, when you DO use recursion, learn when to apply tools like memoization to improve the behavior of the algorithm. Memoization simply means to not recompute that which you have already done. For example it is possible to use recursion for a fibonacci sequence, but this is a terribly inefficient way to do so.

The return value of your function is velocity . Matlab will return the last value assigned to velocity in the function body. Even though velocity is assigned deeper into the recursion, this doesn't matter to Matlab. velocity in the next call to terminal() is a different variable!

Changing the final line to

function[velocity] = terminal(m,c,t,vi)
%inputs:
% m = mass
% c = coeffcient of drag
% t = time
% vi = initial velocity

if t==18, velocity = vi+(9.8-c/m*(vi))*2;
    return 
end

velocity = vi+(9.8-c/m*(vi))*2;
velocity  %used to print out velocity for debugging
velocity = terminal(m,c,t+2,velocity);
end

Gives the expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM