# How to create a loop calcualtion?

19 ビュー (過去 30 日間)
Kokalz 2012 年 7 月 19 日
Hi Guys! I am writing a GUI that processes some excel data and got to a point, where I need it to do some loop calculation. However, I have absolutely no idea how to do it. I tried going through all Doug's tutorials, but could not find anything, that would explain it. So here's the idea:
Say i need a 1x20 matrix. I know the first value of this vector, but all he other values come from the calculations that require the first value. So here's the sequence of the calculation:
Take the first value > input it into formulas with other variables > get the second value > use the second value to get the 3 rd value > get the 3rd value > use the 3rd value to get the 4th value > and so on.... Then plot all the acquired values on the graph against time.
Could someone help me out with it? Tell me if I made i too confusing..
Thank you, guys!
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
NUR ZAFIRAH MOHD SIDEK 2017 年 6 月 20 日
Hi guys, can some one help me to solve my problem?
i've be facing difficulty to find the number of function evaluation. let's say that i have f(x)=x^3+x^2-2 with initial value of x(0)=1.5
i want to calculate x(n+1)=x(n)-(f(x(n)/fprime(x(n))) until n=5
thus, i want to calculate how many time of f(x(n)) have been use until n=5. but if the first iteration has use f(x(1)) then the second iteration also use f(x(1)), it is considered as we only use f(x(1)) one time only.
can someone help me

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

### 回答 (4 件)

Thomas 2012 年 7 月 19 日
Probably something like this..
num_values=10; % total values
first_val=2;
output=zeros(1,num_values); %preallocate space for output
output(1)=first_val; % first value of output
for count=2:num_values
output(count)=output(count-1)+25; %formula current value depends on previous
end
output
plot(output)
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Ryan 2012 年 7 月 19 日
So you have a 1 x 20 vector you want to fill off of knowing just the first value and each previously value determines the next? Is this what you're looking for:
t = 1:1:20; % for graphing later
x = zeros(1,20);
x(1) = .5; % Assign first value of "x"
for m = 2:numel(x)
x(m) = sin(x(m-1)); % Calculates the current cell "m" off of prior cell "m-1"
end
figure,plot(t,x)
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

C.J. Harris 2012 年 7 月 19 日
Here is an example of a quick loop that calculates a value based on the previous value. The loop simply calculates the value as twice the previous value:
A = zeros(1,20); % Initialize array
A(1) = 1; % Set first value to 1
for nLoop = 2:length(A) % Start loop at second value
A(nLoop) = A(nLoop-1) * 2; % Answer is twice the previous value
end
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Kokalz 2012 年 7 月 19 日
Thanks a lot fellas! I really don't know which question to mark up, as all of them seem to use the more or less same method, so I'l just upvote them!

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

### カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://jp.mathworks.com/matlabcentral/answers/44032-how-to-create-a-loop-calcualtion", "fetch_time": 1722768333000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-33/segments/1722640398413.11/warc/CC-MAIN-20240804102507-20240804132507-00395.warc.gz", "warc_record_offset": 284878462, "warc_record_length": 29012, "token_count": 987, "char_count": 3203, "score": 3.75, "int_score": 4, "crawl": "CC-MAIN-2024-33", "snapshot_type": "latest", "language": "en", "language_score": 0.6898270845413208, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00055-of-00064.parquet"}