how to use "while" in writing function?

A geometric series can be approximated using the following identity SUM ar^k = a / (1-r)
Provided that r < 1. Write a script that approximates the geometric series for r = 0.5 using several iterations to get within .001% of the actual value. You may use either a while or for loop.

1 件のコメント

Jan
Jan 2013 年 3 月 5 日
This is not a question, but a command. As for all homework questions, it is recommended to post, what you have done so far and ask a specific question.

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

回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 3 月 5 日
編集済み: Youssef Khmou 2013 年 3 月 5 日

0 投票

hi Duong,
while loop must be used because after each iteration you have to check a if certain condition is met or not :
After iteration n you have : Un and Un-1, so as long as Un-Un-1>1e-5 do the same instructions
tolerance=;% You complete it
U2=0; %Init
U1=0;% Init
A=;% You complete it
Diff=10; % INITIA
while diff>tolerance
U2=U2+............
U1=U2........
Diff=U2-U1;
....

2 件のコメント

Jan
Jan 2013 年 3 月 5 日
"diff" is a built-in Matlab command. Using this symbol as a variable may cause unexpected behavior.
Youssef  Khmou
Youssef Khmou 2013 年 3 月 5 日
alright Jan, i changed it, thnks

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2013 年 3 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by