How can I write a matlab code for this iterative function.
21 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
biratu lemessa
le 6 Avr 2023
Modifié(e) : Dyuman Joshi
le 6 Avr 2023
x(n+1)=(1/(n+1)+(1-(1/(n+1)))*x(n)+5*(x(n)-1)/(exp(x(n))))
3 commentaires
Réponse acceptée
Dyuman Joshi
le 6 Avr 2023
Modifié(e) : Dyuman Joshi
le 6 Avr 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 commentaires
Plus de réponses (1)
Chunru
le 6 Avr 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
Voir également
Catégories
En savoir plus sur Function Creation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!