Mathematica入门习题[13]–迭代公式

  • A+

 

第十三题

 

 

xs = {{1, 1}, {8, 3}, {10, .1}};
Do[
{a, b} = xs[[k]];
r1 = (-b + Sqrt[b^2 + 4*a])/2;
r2 = (-b - Sqrt[b^2 + 4*a])/2;
Print["r1:" <> ToString@N@r1];
f[0] = 1;
f[x_] := a/(b + f[x - 1]);

Do[
If[Abs[f[i] - f[i - 1]] <= 10^(-5),
Print["迭代结果:" <> ToString[N@f[i]]];
Print["误差:" <> ToString[TraditionalForm[N@(f[i] - r1)]]];
Print["\n"]; Break[]];
If[i == 300, Print["迭代结果:" <> ToString[N@f[i]]];
Print["误差:" <> ToString[TraditionalForm@N[(f[i] - r1)]]];];
, {i, 1, 300}];
ClearAll[f];
, {k, 1, 3}]

 

 

 

  • 微信公众号
  • 关注微信公众号
  • weinxin
  • QQ群
  • 我们的QQ群号
  • weinxin
王 茂南

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: