How can I write a matlab code for this iterative function.

25 ビュー (過去 30 日間)
biratu lemessa
biratu lemessa 2023 年 4 月 6 日
編集済み: Dyuman Joshi 2023 年 4 月 6 日
x(n+1)=(1/(n+1)+(1-(1/(n+1)))*x(n)+5*(x(n)-1)/(exp(x(n))))
  3 件のコメント
Walter Roberson
Walter Roberson 2023 年 4 月 6 日
are you sure it is 1/n+1 and not 1/(n+1) ?
biratu lemessa
biratu lemessa 2023 年 4 月 6 日
the question that I need to iterate is this x(n+1)=1/(n+1)+(1-1/(n+1))*x(n)-5*(x(n)-1)/(exp(x(n))). But still it doen't run how can I do?

サインインしてコメントする。

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 4 月 6 日
編集済み: Dyuman Joshi 2023 年 4 月 6 日
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
The sequence converges after 94 iterations for tolerance = 0.000001
x=x(1:n+1); %discard the zeros
format long
disp(x)
Columns 1 through 9 0.500000000000000 2.266326649281583 1.187675646656826 0.854617807067713 1.192955435194717 0.868156620583146 1.163680903337047 0.887604915198019 1.131425095116121 Columns 10 through 18 0.906311497659785 1.104084879025953 0.922883104252097 1.082035542164216 0.937164596743137 1.064428383729875 0.949286820328331 1.050404246023775 0.959447912093624 Columns 19 through 27 1.039260635834704 0.967862007062625 1.030437602503192 0.974745666029741 1.023484504028818 0.980311300399718 1.018034285987070 0.984761319457214 1.013786112572562 Columns 28 through 36 0.988282805885377 1.010493442171635 0.991043533899946 1.007955166924509 0.993189800022176 1.006008432754297 0.994846025193740 1.004522475351900 0.996115758361740 Columns 37 through 45 1.003393206646930 0.997083599564924 1.002538465503489 0.997817595201277 1.001893904290104 0.998371760512707 1.001409486226271 0.998788499089087 1.001046554104671 Columns 46 through 54 0.999100787825405 1.000775416334113 0.999334068735385 1.000573387255714 0.999507837165259 1.000423216016248 0.999636944276976 1.000311840561424 0.999732645922297 Columns 55 through 63 1.000229408743413 0.999803435150452 1.000168515568749 0.999855695291484 1.000123613117731 0.999894207380366 1.000090556922328 0.999922541260303 1.000066259190022 Columns 64 through 72 0.999943354996391 1.000048425021510 0.999958622773054 1.000035352648487 0.999969807492606 1.000025782755712 0.999977990924539 1.000018785234436 0.999983971469759 Columns 73 through 81 1.000013674344976 0.999988337348532 1.000009945349267 0.999991521223690 1.000007227307356 0.999993840856691 1.000005248001301 0.999995529293015 1.000003807929629 Columns 82 through 90 0.999996757223015 1.000002761066885 0.999997649512350 1.000002000655724 0.999998297399099 1.000001448733896 0.999998767477791 1.000001048427057 0.999999108306091 Columns 91 through 95 1.000000758285673 0.999999355255947 1.000000548129853 0.999999534070717 1.000000396004657

その他の回答 (1 件)

Chunru
Chunru 2023 年 4 月 6 日
% 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
i= 0 x= 6.000 i= 0 x= 2020.644 i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf i= 0 x= Inf

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by