Sequence a_n =1/n

7 ビュー (過去 30 日間)
Michal Waligora
Michal Waligora 2020 年 1 月 17 日
回答済み: Image Analyst 2020 年 1 月 17 日
I'm a beginner in Matlab
Please could you help me with the following code:
I want to write a sequence s_n = 1/n where n=1,2...100 and plot the result.
I tried this, n=1
for i = 1:100
n= n+1
end
Sn= 1/n
But It only displays the last number for n=100 and divides
I have no idea how to do it and the results I found don't satisfy me.
Thank you

採用された回答

Star Strider
Star Strider 2020 年 1 月 17 日
編集済み: Star Strider 2020 年 1 月 17 日
Use element-wise division:
n = 1:100;
format short
Seq = 1./n
producing:
Seq =
1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 ...
alternatively:
format rat
Seq = 1./n
producing:
Seq =
1 1/2 1/3 1/4 1/5 1/6 ...
EDIT — (17 Jan 2020 at 17:11)
Forgot to include the plot call.
Here it is:
figure
plot(Seq)
grid
This is the same as:
figure
plot((1:numel(Seq)), Seq)
grid

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 1 月 17 日
See The FAQ. It answers the question. Look at the rest of the FAQ for lots more goodies.

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by