在MATLAB中数值计算复数值函数的导数 [英] Numerically compute derivative of complex-valued function in MATLAB

查看:590
本文介绍了在MATLAB中数值计算复数值函数的导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MATLAB中通过数值计算复数值函数(全纯函数)的导数.

I would like to compute the derivative of a complex-valued function (Holomorphic function) numerically in MATLAB.

我已经在复杂平面上的网格中计算了函数,并且尝试使用柯西-黎曼关系来计算导数.

I have computed the function in a grid on the complex plane, and I've tried to compute the derivative using the Cauchy–Riemann relations.

给出: u = real(f),v = imag(f),x = real(point),y = imag(point)

Given: u = real(f), v = imag(f), x = real(point), y = imag(point)

导数应为:f'= du/dx + i dv/dx = dv/dy-i du/dy

The derivative should be given by: f' = du/dx + i dv/dx = dv/dy - i du/dy

其中"d"是导数运算符.

where 'd' is the derivative operator.

我尝试了以下代码:

stepx = 0.01;
stepy = 0.01;
Nx = 2/stepx +1;
Ny = 2/stepy +1;
[re,im] = meshgrid([-1:stepx:1], [-1:stepy:1]);
cplx = re + 1i*im;
z = cplx.^3;

导数应为:

f1 = diff(real(z),1,2)/stepx +1i* diff(imag(z),1,2)/stepx;

f2 = diff(imag(z),1,1)/stepy - 1i* diff(real(z),1,1)/stepy;

但是假定两个相等的导数不匹配.

But the two derivatives, which are suppose to be equal, do not match.

我在做什么错了?

让我们计算少于stepx的元素数量(假设stepx = stepy):

Let's count the number of elements which differs for less than stepx (assuming stepx = stepy):

lm = min(size(f1));
A = f1(1:lm,1:lm);
B = f2(1:lm,1:lm);

sum(sum(abs(A - B) <= stepx))

并使用@A建议的修复程序.达达(Donda)

and using the fix proposed by @A. Donda

f1i = interp1(1 : Ny, f1, 1.5 : Ny);
f2i = interp1(1 : Nx, f2 .', 1.5 : Nx) .';

sum(sum(abs(f1i - f2i) <= stepx))

在第二种情况下,它们的差异都小于应有的stepx,而在第一种情况下,则并非如此.

In the second case they all differ for less than stepx as it should be, while in the first case it's not true.

推荐答案

问题是,使用在Matlab中离散使用的两个不同表达式,您正在计算复杂平面中不同点的近似导数.假设您处于虚数y并计算沿实轴x的差,则第i个差估计(x(i) + x(i + 1))/2处的导数,即两个后续x值之间的所有中点.另一种方法是,在给定x处但在两个后续y值之间的所有中点处估计导数.

The problem is that using the two different expressions, discretized for use in Matlab, you are computing the approximate derivatives at different points in the complex plane. Let's say you are at imaginary value y and you compute the differences along the real axis x, then the ith difference estimates the derivative at (x(i) + x(i + 1))/2, i.e. at all the midpoints between two subsequent x-values. The other way around you estimate the derivative at a given x but at all the midpoints between two subsequent y-values.

这也会导致所得矩阵的大小不同.使用第一个公式,您会得到一个尺寸为201x200的矩阵,另一个公式为200x201.这是因为在第一个变体中,沿x有200个中点,但沿y值有201个中点,反之亦然.

This also leads to different sizes of the resulting matrices. Using the first formula you get a matrix of size 201x200, the other of size 200x201. That's because in the first variant there are 200 midpoints along x, but 201 y-values, and vice versa.

答案是,您没有做错任何事情,只是在错误地解释结果.

So the answer is, you are not doing anything wrong, you are just interpreting the result wrongly.

您可以通过沿另一维(不用于导数的维)进行显式插值来解决该问题:

You can solve the problem by interpolating explicitly along the other dimension (the one not used for the derivative):

f1i = interp1(1 : Ny, f1, 1.5 : Ny);
f2i = interp1(1 : Nx, f2 .', 1.5 : Nx) .';

其中f1是根据您的第一个公式计算的,而f2是根据第二个公式计算的.现在,两个导数都在两个维度上的中点处进行评估,这就是两个矩阵的尺寸均为200x200的原因.

Where f1 is computed according to your first formula and f2 according to the second. Now both derivatives are evaluated at points that are midpoints along both dimensions, which is why both matrices are of size 200x200.

如果现在比较它们,将会发现它们在数值误差之前都是相同的(毕竟,diff仅计算近似导数,而interp1会产生插值误差).对于您的步进大小,此错误最大为1e-4,可以通过使用较小的步进大小来进一步减小.

If you compare them now, you will see that they are identical up to numerical error (after all, diff computes only approximate derivatives and interp1 makes interpolation errors). For your stepsize, this error is maximally 1e-4, and it can be further reduced by using a smaller stepsize.

这篇关于在MATLAB中数值计算复数值函数的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆