How can I write a matlab code for this iterative function.
20 views (last 30 days)
Show older comments
x(n+1)=(1/(n+1)+(1-(1/(n+1)))*x(n)+5*(x(n)-1)/(exp(x(n))))
3 Comments
Accepted Answer
Dyuman Joshi
on 6 Apr 2023
Edited: Dyuman Joshi
on 6 Apr 2023
tolerance=0.000001;
%As it is not known at which iteration the sequence will converge, I have
%assumed 1000 iterations for preallocation of x
k=1000;
x=zeros(1,k);
x(1)=0.5;
for n=1:k
x(n+1)=1/(n+1)+(1-1/(n+1))*x(n)-5*(x(n)-1)/(exp(x(n)));
%Check for tolerance, another option for checking tolerance is
%abs(x(n+1)-x(n))/abs(x(n))<tol, note that the result might be
%different in that case, it's upto you to choose which method to use
if abs(x(n+1)-x(n))<tolerance
fprintf('The sequence converges after %d iterations for tolerance = %f', n, tolerance)
break;
end
end
x=x(1:n+1); %discard the zeros
format long
disp(x)
0 Comments
More Answers (1)
Chunru
on 6 Apr 2023
% initial value of x(0)
x = 0;
for n=1:10
x = (1/n)- (1-(1/n+1)) *x +5*exp(x); % check the formula. this one is diverging
fprintf("i=%3d x=%9.3f\n", i, x)
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!