How do I correctly use Recursion in MATLAB?
Solution 1
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
Solution 2
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 (i.e. the Euler method, which can display significant errors or instabilities when the time steps that are taken are too large).
Solution 3
velocity = terminal(m,c,t+2,velocity)
should work.
Solution 4
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.
James Mertz
I am an author for the Real Python website and Software Assurance Engineer at NASA's Jet Propulsion Lab. You can follow me on twitter: @mertz_james Or check out my blog: Mertz Musings There are no winners or losers in the race of life, only finishers and quitters. #SOreadytohelp on the following topics: Python DOORS DXL
Updated on July 09, 2022Comments
-
James Mertz almost 2 years
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?
-
James Mertz over 13 yearsPlease explain what and where you mean exactly? Do I do this within the terminating if statement or at the end when I recursively call the function?
-
Clement J. over 13 years"vi" can be replaced by "velocity" (using only this variable). (and "for t = 0:2:18" if you want exactly the same comportment)
-
Clement J. over 13 years@KronoS : that the second part of my comment.
-
gnovice over 13 yearsIf the idea is for the final value of
velocity
to represent the summation of the incremental effects that gravitational acceleration and drag will have for each time step given an initial velocityvi
, then you should changevi
tovelocity
in the loop and add the initializationvelocity = vi;
before the loop is entered. -
Ivo Flipse over 13 yearsI'd go with his answer @Kronos, as it's better to have a correct answer than 'just' doing what the book said
-
James Mertz about 12 yearswhile it's been a while from when I worked on this, I believe that the programming was the more important aspect of the issue at hand. I wasn't able to find any questions on how to correctly perform recursion in matlab, and thought that I'd use this as an example. However, you are correct in that this is more exact.