如何在 Mathematica 中编写欧拉函数?

发布于 2024-12-21 14:05:40 字数 476 浏览 2 评论 0原文

我编写了一个欧拉函数,但读错了指令,所以现在我必须创建一个新函数,但我无法弄清楚。

我制作了以下自动欧拉函数。

f[x_, y_] := -x y^2;
x0 = 0;
y0 = 2;
xend = 2;
steps = 20;
h = (xend - x0)/steps // N;
x = x0;
y = y0;
eulerlist = {{x, y}};
For[i = 1, i <= steps, y = f[x, y]*h + y;
  x = x + h;
  eulerlist = Append[eulerlist, {x, y}];
  i++
]
Print[eulerlist]

但它只是生成我指定的列表。

我想要一个能够生成这种形式的欧拉函数:

Euler[y, 2, -x y^2, {x, 0, 2}, 20]

我似乎没有进一步了解。

I programmed a Euler function but misread the instructions, so now I have to make a new one, but I can't figure it out.

I have made the following automatic Euler function.

f[x_, y_] := -x y^2;
x0 = 0;
y0 = 2;
xend = 2;
steps = 20;
h = (xend - x0)/steps // N;
x = x0;
y = y0;
eulerlist = {{x, y}};
For[i = 1, i <= steps, y = f[x, y]*h + y;
  x = x + h;
  eulerlist = Append[eulerlist, {x, y}];
  i++
]
Print[eulerlist]

But it just generates the list I have specified.

I would like to have a Euler function which is able to generate this form:

Euler[y, 2, -x y^2, {x, 0, 2}, 20]

I don't seem to get any further.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-12-28 14:05:41

目前尚不清楚您在问什么,但如果您想要的是能够输入

Euler[y, 2, -x y^2, {x, 0, 2}, 20] 

并获取

{{0,2},{0.1,2.},{0.2,1.96},{0.3,1.88317},{0.4,1.77678},{0.5,1.6505},{0.6,1.51429},{0.7,1.37671},{0.8,1.24404},{0.9,1.12023},{1.,1.00728},{1.1,0.905822},{1.2,0.815565},{1.3,0.735748},{1.4,0.665376},{1.5,0.603394},{1.6,0.548781},{1.7,0.500596},{1.8,0.457994},{1.9,0.420238},{2.,0.386684}}

那么您需要编写如下函数定义:

Euler[y0_, f_, {x0_, xend_}, steps_Integer?Positive] := (* body *)

注意下划线表示模式,即 :=表示延迟评估和模式规范Integer?Positive

至于函数体——天哪,你能选择一种不那么 Mathematica 风格的方法吗?也许不是。过程循环和 Append 几乎从来都不是在 Mathematica 中执行任何操作的最佳方式。

这是一个更好的解决方案。

Euler[y_, y0_, f_, {x_, x0_, xend_}, steps_Integer?Positive] :=
 With[{h = N[(xend - x0)/steps], ff = Function[{x, y}, f]}, 
  NestList[{#[[1]] + h, ff[#[[1]], #[[2]]]*h + #[[2]]} &, {x0, y0}, 
   steps]]

Euler[y, 2, -x y^2, {x, 0, 2}, 20]

 {{0, 2}, {0.1, 2.}, {0.2, 1.96}, {0.3, 1.88317}, {0.4, 
  1.77678}, {0.5, 1.6505}, {0.6, 1.51429}, {0.7, 1.37671}, {0.8, 
  1.24404}, {0.9, 1.12023}, {1., 1.00728}, {1.1, 0.905822}, {1.2, 
  0.815565}, {1.3, 0.735748}, {1.4, 0.665376}, {1.5, 0.603394}, {1.6, 
  0.548781}, {1.7, 0.500596}, {1.8, 0.457994}, {1.9, 0.420238}, {2., 
  0.386684}}

如果您想要输出 Euler[y, 2, -xy^2, {x, 0, 2}, 20],那么将其输入到笔记本中就是最快的方法。

It is not clear what you are asking, but if what you want is to be able to input

Euler[y, 2, -x y^2, {x, 0, 2}, 20] 

and get

{{0,2},{0.1,2.},{0.2,1.96},{0.3,1.88317},{0.4,1.77678},{0.5,1.6505},{0.6,1.51429},{0.7,1.37671},{0.8,1.24404},{0.9,1.12023},{1.,1.00728},{1.1,0.905822},{1.2,0.815565},{1.3,0.735748},{1.4,0.665376},{1.5,0.603394},{1.6,0.548781},{1.7,0.500596},{1.8,0.457994},{1.9,0.420238},{2.,0.386684}}

Then you need to write a function definition like this:

Euler[y0_, f_, {x0_, xend_}, steps_Integer?Positive] := (* body *)

Notice the underscores to denote patterns, the := to denote delayed evaluation and the pattern specification Integer?Positive.

As for the body of the function -- oh my goodness could you have picked a less Mathematica-style approach? Perhaps not. Procedural loops and Append are almost never the best way to do anything in Mathematica.

Here is a better solution.

Euler[y_, y0_, f_, {x_, x0_, xend_}, steps_Integer?Positive] :=
 With[{h = N[(xend - x0)/steps], ff = Function[{x, y}, f]}, 
  NestList[{#[[1]] + h, ff[#[[1]], #[[2]]]*h + #[[2]]} &, {x0, y0}, 
   steps]]

Euler[y, 2, -x y^2, {x, 0, 2}, 20]

 {{0, 2}, {0.1, 2.}, {0.2, 1.96}, {0.3, 1.88317}, {0.4, 
  1.77678}, {0.5, 1.6505}, {0.6, 1.51429}, {0.7, 1.37671}, {0.8, 
  1.24404}, {0.9, 1.12023}, {1., 1.00728}, {1.1, 0.905822}, {1.2, 
  0.815565}, {1.3, 0.735748}, {1.4, 0.665376}, {1.5, 0.603394}, {1.6, 
  0.548781}, {1.7, 0.500596}, {1.8, 0.457994}, {1.9, 0.420238}, {2., 
  0.386684}}

If you want something that outputs Euler[y, 2, -x y^2, {x, 0, 2}, 20], then typing it into the notebook is the quickest method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文