进行迭代方案的最佳方法

发布于 2024-12-26 10:49:37 字数 1735 浏览 2 评论 0原文

我希望以前没有人问过这个问题,如果是的话,我深表歉意。

编辑:为了清楚起见,将使用以下符号:矩阵用粗体大写,向量用粗体小写,标量用斜体。

假设x0是一个向量, AB是矩阵函数,f是向量函数。

我正在寻找在 Mathematica 中执行以下迭代方案的最佳方法:

A0 = A(x0), B0=B(x0), f0 = f(x0)
x1 = Inverse(A0)(B0.x0 + f0)

A1 = A(x1), B1=B(x1), f1 = f(x1)
x2 = Inverse(A1)(B1.x1 + f1)

...

我知道 for-loop 可以做到这一点,但我不太熟悉 Mathematica,而且我担心这是最有效的方法。这是一个合理的担忧,因为我想定义一个函数 u(N):=xN 并在进一步的计算中使用它。

我想我的问题是:

对方案进行编程最有效的方法是什么?

RecurrenceTable 是一种可行的方法吗?

编辑

这比我想象的要复杂一些。我提供更多详细信息,以便获得更彻底的答复。

在进行递归之前,我在理解如何对函数 ABf 进行编程时遇到了问题。

矩阵AB是时间步长dt = 1/T和空间步长dx = 1/M的函数em>,其中 TM 是 {0 x < 10 < t} 地区。对于向量函数 f 也是如此。

ABfx 的依赖性相当棘手:

AB下三角矩阵(就像三对角矩阵;我想我们可以称它们为 >多对角线),其上有定义的常数值对角线。

给定一个点 0 xs < 1,我需要确定它在网格中的代表性xn(最接近的),然后然后替换第n行strong>A 和 B 以及函数 v( x) (当然是转置的),以及第 n 个f 具有函数w(x)。

总结一下,A = A(dt, dx, xs, x)。 Bf也是如此。

然后我需要执行上面提到的循环,定义 u( x) = step[T]

希望我已经解释清楚了。

I hope this hasn't been asked before, if so I apologize.

EDIT: For clarity, the following notation will be used: boldface uppercase for matrices, boldface lowercase for vectors, and italics for scalars.

Suppose x0 is a vector, A and B are matrix functions, and f is a vector function.

I'm looking for the best way to do the following iteration scheme in Mathematica:

A0 = A(x0), B0=B(x0), f0 = f(x0)
x1 = Inverse(A0)(B0.x0 + f0)

A1 = A(x1), B1=B(x1), f1 = f(x1)
x2 = Inverse(A1)(B1.x1 + f1)

...

I know that a for-loop can do the trick, but I'm not quite familiar with Mathematica, and I'm concerned that this is the most efficient way to do it. This is a justified concern as I would like to define a function u(N):=xNand use it in further calculations.

I guess my questions are:

What's the most efficient way to program the scheme?

Is RecurrenceTable a way to go?

EDIT

It was a bit more complicated than I tought. I'm providing more details in order to obtain a more thorough response.

Before doing the recurrence, I'm having problems understanding how to program the functions A, B and f.

Matrices A and B are functions of the time step dt = 1/T and the space step dx = 1/M, where T and M are the number of points in the {0 < x < 1, 0 < t} region. This is also true for vector the function f.

The dependance of A, B and f on x is rather tricky:

A and B are upper and lower triangular matrices (like a tridiagonal matrix; I suppose we can call them multidiagonal), with defined constant values on their diagonals.

Given a point 0 < xs < 1, I need to determine it's representative xn in the mesh (the closest), and then substitute the nth row of A and B with the function v( x) (transposed, of course), and the nth row of f with the function w( x).

Summarizing, A = A(dt, dx, xs, x). The same is true for B and f.

Then I need do the loop mentioned above, to define u( x) = step[T].

Hope I've explained myself.

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

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

发布评论

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

评论(2

囍笑 2025-01-02 10:49:37

我不确定这是否是最好的方法,但我只会使用普通的旧记忆法。您可以将单个步骤表示为

xstep[x_] := Inverse[A[x]](B[x].x + f[x])

然后

u[0] = x0
u[n_] := u[n] = xstep[u[n-1]]

如果您提前知道需要多少个值,并且出于某种原因预先计算所有值是有利的(例如您想要打开一个文件,使用其内容来计算xN,然后释放内存),您可以使用NestList。而不是前两行,您可以这样做

xlist = NestList[xstep, x0, 10];
u[n_] := xlist[[n]]

This will break if n >当然是 10(显然,更改 10 以满足您的实际要求)。

当然,可能值得查看您的特定函数,看看是否可以进行一些代数简化。

I'm not sure if it's the best method, but I'd just use plain old memoization. You can represent an individual step as

xstep[x_] := Inverse[A[x]](B[x].x + f[x])

and then

u[0] = x0
u[n_] := u[n] = xstep[u[n-1]]

If you know how many values you need in advance, and it's advantageous to precompute them all for some reason (e.g. you want to open a file, use its contents to calculate xN, and then free the memory), you could use NestList. Instead of the previous two lines, you'd do

xlist = NestList[xstep, x0, 10];
u[n_] := xlist[[n]]

This will break if n > 10, of course (obviously, change 10 to suit your actual requirements).

Of course, it may be worth looking at your specific functions to see if you can make some algebraic simplifications.

归属感 2025-01-02 10:49:37

我可能会编写一个接受 A0、B0、x0 和 f0 的函数,然后返回 A1、B1、x1 和 f1 - 假设

step[A0_?MatrixQ, B0_?MatrixQ, x0_?VectorQ, f0_?VectorQ] := Module[...]

我会嵌套该函数。如果没有更精确的信息,就很难做到更精确。

另外,如果您的过程是数值过程,那么您当然不想计算Inverse[A0],因为这不是一个数值稳定的操作。相反,您应该编写

A0.x1 == B0.x0+f0

并使用数值稳定的求解器来查找 x1。当然,Mathematica的LinearSolve提供了这样的算法。

I would probably write a function that accepts A0, B0, x0, and f0, and then returns A1, B1, x1, and f1 - say

step[A0_?MatrixQ, B0_?MatrixQ, x0_?VectorQ, f0_?VectorQ] := Module[...]

I would then Nest that function. It's hard to be more precise without more precise information.

Also, if your procedure is numerical, then you certainly don't want to compute Inverse[A0], as this is not a numerically stable operation. Rather, you should write

A0.x1 == B0.x0+f0

and then use a numerically stable solver to find x1. Of course, Mathematica's LinearSolve provides such an algorithm.

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