我需要为一些复数值的特定坐标点运行最内层的for循环。但是,我不知道具体的坐标点。我想猜测并检查以获得答案。然而,这是tediuos。因此,我想提出嵌套for循环来为我做检查不同值的工作。当我尝试运行下面的代码时,我得到一个无限循环。我的代码中有错误吗?
for i from -100.0 to 100.0 do
for j from -100.0 to 100.0 do
for m from -100.0 to 100.0 do
for n from -100.0 to 100.0 do
X__0:=[i+j*I, m+n*I]
for K from 1 to 20 while evalf(abs(X[K-1][1]-X[K-2][1]),25)<>0 and evalf(abs(X[K-1][2]-X[K-2][2]),25)<>0
do
X[K]:=evalf(G(X[K-1][1], X[K-1][2]), 25)
end do;
此外,如果有可能,我可以在循环中添加一个条件,不打印与[16.19615242270663188058234,-1.928203230275509174109785]或[5.803847577293368119417661,11.92820323027550917410978]相匹配的X [k]解决方案吗?
答案 0 :(得分:1)
显然,这与您的earlier question有关。所以我试着在这里解决两个问题。
你可能会认为&#34;彻底&#34;搜索网格在查找大多数(或有时是所有)根时效果更好,但随机过程通常可以更快地找到更小的子集。然而,随着随机方法发现越来越多的根,那么机会增长的可能性是每个新生成的随机起始点将收敛到已找到的根。
您在一个问题中提到了Maple 15,因此我在64位Maple 15.01 for Windows中运行了以下代码。
我将你的基本步骤包含在一个proc中,以便于使用和(希望)理解。
我没有改变你的迭代方案(可能是家庭作业),但请注意它需要一个初始点。请注意,X__0与X [0]类似地显示为漂亮打印的2D Math输出,但它们不相同。这是您在问题上的代码以及您的其他问题中的一个示例上的转录代码的问题。如果您不完全清楚,我建议您使用1D Maple Notation作为输入模式。
没有必要使用不推荐使用的linalg [jacobian]。我调整为使用VectorCalculus:-Jacobian。
restart;
f := (x-7)^4+(y-2)^2-100:
g := (x-11)^2+(y-5)^2-75:
G := unapply(convert(Vector([x,y])
-1/VectorCalculus:-Jacobian([f, g],[x, y]).Vector([f,g]),
list),x,y):
p := proc(X0::[complex(numeric),complex(numeric)], G)
local X, K;
X[0]:=X0;
for K to 30 while evalf[Digits+5](abs(X[K-1][1]-X[K-2][1])) <> 0
and evalf[Digits+5](abs(X[K-1][2]-X[K-2][2])) <> 0 do
X[K] := evalf[Digits+5](G(X[K-1][1], X[K-1][2]));
end do;
if not type(X[K-1],[complex(numeric),complex(numeric)]) then
error "nonnumeric results"; end if;
if K<29 then map(simplify@fnormal, evalf(X[K-1]));
else error "did not converge"; end if;
end proc:
p( [17, -2], G );
[9.879819419, -3.587502283]
p( [5, 11], G );
[5.127257522, 11.36481703]
p( [-1.0-11.*I, -2.0*I], G );
[7.144643342 - 2.930435630 I, -3.398413328 + 1.345239163 I]
Digits := 20:
p( [-1.0-11.*I, -2.0*I], G );
[7.1446433421702820770 - 2.9304356302329792484 I,
-3.3984133281207314618 + 1.3452391631560967251 I]
Equate([x,y],%);
[x = 7.1446433421702820770 - 2.9304356302329792484 I,
y = -3.3984133281207314618 + 1.3452391631560967251 I]
eval( [f,g], % );
[ -18 -18 -18 ]
[1 10 + 2 10 I, -1 10 + 0. I]
Digits := 10:
NN:=2: # This attempts (NN+1)^4 iterates
incx,incy:=5.0,5.0:
Sols:={}:
count:=0:
st := time():
for a from -NN to NN do
for b from -NN to NN do
for c from -NN to NN do
for d from -NN to NN do
count:=count+1;
try
cand := p( [a*incx+b*incy*I, c*incx+d*incy*I], G );
if not member(cand,{Sols}) then
Sols := Sols union {cand}; end if;
catch:
end try;
end do;
end do;
end do;
end do;
(time() - st)*'seconds', count*'attempts';
34.695 seconds, 625 attempts
nops( Sols );
8
sort( Sols );
{[3.867005122, 0.08874923598], [5.127257522, 11.36481703],
[5.721021477, 11.86530303], [9.879819419, -3.587502283],
[7.144643342 - 2.930435630 I, -3.398413328 + 1.345239163 I],
[7.144643342 + 2.930435630 I, -3.398413328 - 1.345239163 I],
[8.557804888 - 1.867139097 I, 13.53272982 - 0.5344031829 I],
[8.557804888 + 1.867139097 I, 13.53272982 + 0.5344031829 I]}
seq( eval( max(abs(f),abs(g)), Equate([x,y],xypoint) ), xypoint=Sols );
-8 -7 -8 -8 -8 -8
3 10 , 1 10 , 9 10 , 4 10 , 3.162277660 10 , 3.162277660 10 ,
-7 -7
1.019803903 10 , 1.019803903 10
randomize():
fgen := proc(a::numeric,b::numeric,i::nonnegint:=1)
seq(RandomTools:-Generate(float('range'=a..b,'digits'=5)),
ii=1..i);
end proc:
fgen(-100.0, 100.0); # Usage example, a random point
-26.41
randSols := {}:
A,B := 15, 15:
numattempts:=100:
st := time():
for s from 1 to numattempts do
try
cand := p( [fgen(-A,A)+fgen(-B,B)*I, fgen(-A,A)+fgen(-B,B)*I], G );
if not member(cand,{randSols}) then
randSols := randSols union {cand}; end if;
catch:
end try;
end do;
(time() - st)*'seconds', numattempts*'attempts';
5.756 seconds, 100 attempts
nops(randSols);
5
sort( randSols );
{[3.867005122, 0.08874923598],
[7.144643342 - 2.930435630 I, -3.398413328 + 1.345239163 I],
[7.144643342 + 2.930435630 I, -3.398413328 - 1.345239163 I],
[8.557804888 - 1.867139097 I, 13.53272982 - 0.5344031829 I],
[8.557804888 + 1.867139097 I, 13.53272982 + 0.5344031829 I]}
seq( eval( max(abs(f),abs(g)), Equate([x,y],xypoint) ), xypoint=randSols );
-8 -8 -8 -7 -7
3 10 , 3.162277660 10 , 3.162277660 10 , 1.019803903 10 , 1.019803903 10
答案 1 :(得分:0)
对于每个&#39;做&#39;声明(即循环)你需要一个相应的“结束”,你只有一个。你还需要用冒号或分号终止语句(例如X__0:= [i + j I,m + n I])。如,
for i from -100.0 to 100.0 do
for j from -100.0 to 100.0 do
for m from -100.0 to 100.0 do
for n from -100.0 to 100.0 do
X__0:=[i+j*I, m+n*I];
for K from 1 to 20 while evalf(abs(X[K-1][1]-X[K-2][1]),25)<>0 and evalf(abs(X[K-1][2]-X[K-2][2]),25) <>0 do
X[K]:=evalf(G(X[K-1][1], X[K-1][2]), 25);
end do;
end do;
end do;
end do;
end do;
结束;
您的代码并未提供X [0]的初始定义或定义G,因此很难知道改进内容的建议。