How can I write a matlab code for this iterative function.
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
biratu lemessa
am 6 Apr. 2023
Bearbeitet: Dyuman Joshi
am 6 Apr. 2023
x(n+1)=(1/(n+1)+(1-(1/(n+1)))*x(n)+5*(x(n)-1)/(exp(x(n))))
3 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 6 Apr. 2023
Bearbeitet: Dyuman Joshi
am 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 Kommentare
Weitere Antworten (1)
Chunru
am 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
Siehe auch
Kategorien
Mehr zu Function Creation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!