Calculate the first n terms of the recursion
Show older comments
Calculate the first n terms of the following recursion

function xn_plus_one = state(n,t)
if n<1 || ~isscalar(n) || fix(n)~=n
error('n must be a non-negative integer');
else
xn_plus_one = zeros(1,length(t));
for jj = 1:length(t)
if t(jj) < 0 || t(jj) > 3
error('t must contain non-negative scalars less than or equal to 3');
else
if n==1
xn_plus_one(jj)=1;
else
xn_plus_one(jj) = integral(@(s) (2.*control(n-1,s)),0,t(jj)-1);
end
end
end
end
end
function C=control(n,v)
C=zeros(1,length(v));
for ii=1:length(v)
if v(ii)>=-1 && v(ii)<0
C(ii)=0;
elseif v(ii)>=0 && v(ii)<=2
C(ii)=integral(@(s) state(n,s),0,v(ii));
elseif v(ii)>2 && v(ii)<=3
C(ii)=0;
else
error('entries in v must vary from -1 to 3')
end
end
end
Calling the state function at n=4 and t=3 takes too much time to run. How can I improve it?
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!