在Matlab中迭代地收集for循环中的“solve”元素

时间:2015-11-28 14:05:14

标签: matlab for-loop

我正在尝试从solve内的for loop命令中收集x y值。我试图将每个运行“r”值的x y值放入一个数组中。

这是我的代码:

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?谢谢。

1 个答案:

答案 0 :(得分: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

耶!