How to plot recursive definition in Matlab/Simulink?

8 views (last 30 days)
Sammy
Sammy on 14 Aug 2021
Commented: Peter O on 20 Aug 2021
where n is 1 to 10
A(n)=1.03*A(n-1)-700;
B(n)=(B(n-1)+50)*1.03;
(n-1) being the previous term of n
is it possible to plot those recursive definitions as two lines on the same graph in Matlab?
if so how?

Accepted Answer

Peter O
Peter O on 14 Aug 2021
Sure. Use a while loop and call functions for each. However, for this problem, consider using a for loop. It will be much more efficient since you can use the values immediately previous.
You will need to define A(1) and B(1). I'll assume them to be 1 here.
nmx = 10;
% Assuming A(1) and B(1) are 1, because they weren't otherwise defined.
A = ones(nmx,1);
B = ones(nmx,1);
for ix=2:nmx
A(ix) = 1.03*A(ix-1)-700;
B(ix) = 1.03*(B(ix-1)+50);
end
% Call the recursive function for each value of n
Ar = ones(nmx,1);
Br = ones(nmx,1);
for ix=1:nmx
[Ar(ix)] = A_recurse(ix);
[Br(ix)] = B_recurse(ix);
end
figure(1)
plot(1:nmx,A,'p');
hold on
plot(1:nmx,B,'o');
plot(1:nmx,Ar,'s');
plot(1:nmx,Br,'x');
hold off
legend('A','B','Ar','Br');
function A = A_recurse(n)
if n == 1
A = 1;
else
A = 1.03*A_recurse(n-1)-700;
end
end
function B = B_recurse(n)
if n == 1
B = 1;
else
B = 1.03*(B_recurse(n-1)+50);
end
end
  2 Comments
Peter O
Peter O on 20 Aug 2021
That's correct!
A(1) = 10000;
B(1) = 2000;
Two other comments:
First, structuring using a for loop is generally a good idea when you don't know how many times you will recurse or are looping a large number of values. The number of nested function calls can grow dramatically and it's harder for computers to optimize. A point of note is the way I coded the recursion above was a way to capture the output at each value, so it's even worse.
For the above case on n=10:
n=10 waits on n=9 which waits on n=8... 10 calls
n=9 waits on n=8... 9 calls
...
55 calls
If you were only interested in the final value for getRecurse(10), then you'd have 10 evaluations using either the FOR loop or recursion. It's much easier to capture running values or build arrays using a loop than using recursion.
Second, a stylistic thing. In MATLAB it's good practice to preallocate your arrays when you know their size because it reduces the amount of work the computer has to do to keep it in memory as you fill it. I used a ones call to fill the above to skip allocating A(1)=1, which is not necessarily the best practice. If you're going to use a different value to start, consider evaluating to a more "blank" value with an explicit step to initialize. Common choices are setting to zero or nan to start.
A = zeros(nmx,1);
B = zeros(nmx,1);
A(1) = 10000;
B(1) = 2000;
The intention in above style would be more clear to someone reading your code than initializing all your values to the initial value.
A = 10000*ones(nmx,1);
B = 2000*ones(nmx,1);

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by