2015-11-28 69 views
0

我試圖從for loop內的solve命令中收集x y值。我試圖將x y值放入數組中,用於「r」值的每次運行。在Matlab中迭代收集for循環中的「solve」元素

這裏是我的代碼:

syms x y; 

r = 1:10; 

for k = 1:10 

[solx, soly] = solve(x^2*r(k) + y == 0, 2*x + 3*y*r(k) == 50) 

xx(k) = solx(1); 
yy(k) = soly(1); 

end 

我得到較大的值一樣(149^(1/2)*1i)/3 + 1/3 1/3 - (149^(1/2)*1i)/3。你能告訴我如何將這些值以「正常數字」的形式保存到數組中嗎?例如0.3333 - 4.0689i?謝謝。

回答

2

你只需要轉換的solve結果格式化在循環double

clc 
clear 

syms x y; 

r = 1:10; 

for k = 1:10 

[solx, soly] = solve(x^2*r(k) + y == 0, 2*x + 3*y*r(k) == 50); 

%// HERE. Use double() 
xx(k) = double(solx(1)); 
yy(k) = double(soly(1)); 

end 

現在xx,例如,看起來是這樣的:

xx = 

    Columns 1 through 2 

    0.3333 + 4.0689i 0.0833 + 2.0395i 

    Columns 3 through 4 

    0.0370 + 1.3603i 0.0208 + 1.0204i 

    Columns 5 through 6 

    0.0133 + 0.8164i 0.0093 + 0.6804i 

    Columns 7 through 8 

    0.0068 + 0.5832i 0.0052 + 0.5103i 

    Columns 9 through 10 

    0.0041 + 0.4536i 0.0033 + 0.4082i 

耶!