我正在尝试从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
?谢谢。
答案 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
耶!