构造多维微分矩阵 [英] Constructing a Multidimensional Differentiation Matrix

查看:98
本文介绍了构造多维微分矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试构造定义为

I have been trying to construct the matrix Dij, defined as

我想为位于 x i = -cos [π(2 i [-1,1]上的+ 1)/(2 N )]相应地取一个函数的导数.我在构建微分矩阵 D ij 时遇到问题.

I want to plot it for points located at xi = -cos[ π (2 i + 1) / (2 N)] on the interval [-1,1] to consequentially take derivatives of a function. I am though having problems constructing the differentiating matrix Dij.

我已将python脚本编写为:

I have written a python script as:

import numpy as np 
N = 100
x = np.linspace(-1,1,N-1)
for i in range(0, N - 1):
   x[i] = -np.cos(np.pi*(2*i + 1)/2*N)

def Dmatrix(x,N):
    m_ij = np.zeros(3)
    for k in range(len(x)):
        for j in range(len(x)):
           for i in range(len(x)):
                m_ij[i,j,k] = -2/N*((k*np.sin(k*np.pi*(2*i + 1)/2*N(np.cos(k*np.pi*(2*j +1))/2*N)/(np.sin(np.pi*(2*i + 1)/2*N)))
    return m_ij

xx = Dmatrix(x,N)

因此返回错误:

IndexError: too many indices for array

有没有一种方法可以更有效地构造它并成功地在所有k上进行计算? 目标是将该矩阵乘以一个函数,然后求和j以获得给定函数的一阶导数.

Is there a way one could more efficiently construct this and successfully compute it over all k ? The goal will be to multiply this matrix by a function and sum over j to get the first order derivative of given function.

推荐答案

单独查看您的x计算

In [418]: N = 10 
     ...: x = np.linspace(-1,1,N-1) 
     ...: y = np.zeros(N) 
     ...: for i in range(N): 
     ...:    y[i] = -np.cos(np.pi*(2*i + 1)/2*N) 
     ...:                                                                       
In [419]: x                                                                     
Out[419]: array([-1.  , -0.75, -0.5 , -0.25,  0.  ,  0.25,  0.5 ,  0.75,  1.  ])
In [420]: y                                                                     
Out[420]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [421]: (2*np.arange(N)+1)                                                    
Out[421]: array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])
In [422]: (2*np.arange(N)+1)/2*N                                                
Out[422]: array([ 5., 15., 25., 35., 45., 55., 65., 75., 85., 95.])

我将xy分开了,因为否则创建x然后覆盖它是没有任何意义的.

I separated x and y, because otherwise it doesn't make any sense to create x and then over write it.

y值看起来并不有趣,因为它们都只是cos的奇数整数倍的cos.

The y values don't look interesting because they are all just cos of odd whole multiples of pi.

请注意我如何使用np.arange而不是在range上循环.

Note how I use np.arange instead of looping on range.

这篇关于构造多维微分矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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