How to create a loop in matlab codes?

i am calculating a variable theta , and i need to give this theta as a feed bak to one of the formula which i used to calculate it. how should i give a loop. i am totally new to matlab codes. please help. the calculation is,
theta = a+b-c-d, where a b c d are some formulas, in which b and c has a theta term in it. so i have to give the left side theta as feedback to b and c.
in simulink you can just pick a line from theta and connect it to b and c block. but how do i loop this with matlab codes?
please help me.

 採用された回答

Thorsten
Thorsten 2015 年 10 月 19 日
編集済み: Thorsten 2015 年 10 月 19 日

0 投票

theta = 23; % some starting value
for i = 1:10 % run 10 times, for example
a = exp(i); % sample computations for a, b, c, d
b = sin(i);
b = 10*theta
c = theta^2;
theta = a + b - c - d;
end

4 件のコメント

Heartrin
Heartrin 2015 年 10 月 19 日
Hi thorsten,
doesn't theta need some initial values to start the first iteration.
Thorsten
Thorsten 2015 年 10 月 19 日
編集済み: Thorsten 2015 年 10 月 19 日
yes, sure. I added it in the code.
Heartrin
Heartrin 2015 年 10 月 28 日
Hi thanks for your help. but i have a doubt what is that (i) in the sample computation of a & b in the codes you written above?
Thorsten
Thorsten 2015 年 10 月 29 日
編集済み: Thorsten 2015 年 10 月 29 日
i is the loop variable that runs from 1:10, i.e., takes values 1, 2, 3, ..., 10. You can use x instead of i if you do not want to overwrite the Matlab's constant for the imaginary unit i=sqrt(-1).

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2015 年 10 月 19 日

編集済み:

2015 年 10 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by