How can I make a for loop with two diferent indexes

%My problem:
syms w;
for i=1:20
lam=i;
xi = fsolve(@(w) -tan(w/2) + w/2-(4./(lam))*(w/2).^3,[pi()-0.1 3*pi()-0.1]);
X(i) = xi(2)/pi()
end
%This is fine, but now I want to break the intervals in smaller ones:
syms w;
for i=1:0.2:20
lam=i;
xi = fsolve(@(w) -tan(w/2) + w/2-(4./(lam))*(w/2).^3,[pi()-0.1 3*pi()-0.1]);
X(i) = xi(2)/pi()
end
%And I want the values of xi(2)/pi() to be saved in a vector, but it want let me. I understand why, but I can't resolve it.
%Thank you very much to you all.

2 件のコメント

Nasser M. Abbasi
Nasser M. Abbasi 2012 年 12 月 6 日
編集済み: Nasser M. Abbasi 2012 年 12 月 6 日
You are mixing syms with numerics. Decide what you want to do. Either use numerics or symbolic. Very confusing code. Why calling fsolve() with syms?
But in order to make a symbolic vector, do
range=1:0.2:20;
X = sym(zeros(1, length(range)));
Nuno
Nuno 2012 年 12 月 8 日
Thanks. I forgot to take out the syms part, indeed it isn't necessary.

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

 採用された回答

Matt Fig
Matt Fig 2012 年 12 月 6 日
編集済み: Matt Fig 2012 年 12 月 6 日

0 投票

syms w;
cnt = 1:.2:20;
for ii=1:length(cnt)
xi = fsolve(@(w) -tan(w/2) + w/2-4/ii*(w/2).^3,[1 3]*pi-.1);
X(ii) = xi(2)/pi;
end

4 件のコメント

Nasser M. Abbasi
Nasser M. Abbasi 2012 年 12 月 6 日
Matt;
no need for syms in the above. Also preallocating X as in
X = zeros(1, length(cnt));
would be good to add.
Matt Fig
Matt Fig 2012 年 12 月 6 日
Nuno comments:
Thank you for the answers. The length of the vector is correct, but the value that is in position 20 of the vector should appear in the last position. What I'm trying to do is resolve that equation for lam=1:0.2:20, and for each value of lam I should get 2 values of w, and is the second value of w that I want to save in a vector. Thank you again for the support.
Matt Fig
Matt Fig 2012 年 12 月 6 日
Nuno, did you run the code? It does exactly what you describe. You want to solve the equation for every value of lam in 1:.2:20 and store the second solution in a vector X. That is what the code does....
Nasser, True enough! I just copied Nuno's code and made the minimal changes.
Nuno
Nuno 2012 年 12 月 8 日
Thank you for the answer Matt Fig.

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

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2012 年 12 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by