24小时热门版块排行榜    

查看: 514  |  回复: 3

ilovexiaomu

金虫 (小有名气)

[求助] mathematica中如何实现c++程序转换为mathematica语句 已有1人参与

各位虫友:
我遇到了一个比较复杂的2重积分,我的积分函数表达式如下:
如图片所示
在对上面的kx及ky求二重积分时无法用mathematica中的语句直接计算,因为积分函数相当复杂,用直接的mathematica语句无法积分,在网上查阅了一个用c++编写出来的版本,现在我想把他转换成mathematica的语句。希望对此熟悉的朋友能帮忙转换一下。
c++语句如下:
/* ============================================================= *
* trapzd2d. Computes the nth stage of refinement of an extended *
* 2d trapezian rule. func is a pointer to a function with two   *
* real variables, it is to be integrated                         *
* between ax<=x<=ay and ay<=y<=by. Works analogous to the one-  *
* dimensional trapzd (Numerical Recipes, p.137).                 *
* ============================================================= */

typedef double real;      /* can be put e.g. to float if single precission is sufficient */
#define FUNC(x,y) ((*func)(x,y))

real trapzd2d(real (*func)(real, real), real ax, real bx, real ay, real by, int n)
{
   real x, y, tnm, sum, delx, dely;
   static real s;
   int it, j, k;

   if (n==1) {
      /* 1. stage: Function is evaluated only at the corners. */
      return (s=0.25*(bx-ax)*(by-ay)*(FUNC(ax,ay)+FUNC(bx,ay)+FUNC(ax,by)+FUNC(bx,by)));
   } else {
      for (it=1, j=1; j<n-1; j++) it <<= 1;
      tnm = it;
      /* delx, dely are the spacings of the points to be added */
      delx = (bx-ax)/tnm;
      dely = (by-ay)/tnm;
      x = ax + 0.5*delx;
      /* Evaluates function at the borders of the integration area */
      for (sum=0.0, j=1; j<=it; j++, x+=delx) {
         sum += 0.5*(FUNC(x,ay) + FUNC(x,by));
      }
      y = ay + 0.5*dely;
      for (j=1; j<=it; j++, y+=dely) {
         sum += 0.5*(FUNC(ax,y) + FUNC(bx,y));
      }
      /* Interiour of the area */
      x = ax + 0.5*delx;
      for (j=1; j<=it; j++, x+= delx) {
         y = ay + 0.5*dely;
         for (k=1; k<=it; k++, y+=dely) {
            sum += FUNC(x,y);
         }
      }
      x = ax + delx;
      for (j=1; j<it; j++, x+=delx) {
         y = ay + 0.5*dely;
         for (k=1; k<=it; k++, y+=dely) {
            sum += FUNC(x,y);
         }
      }
      x = ax + 0.5*delx;
      for (j=1; j<=it; j++, x+=delx) {
         y = ay + dely;
         for (k=1; k<it; k++, y+=dely) {
            sum += FUNC(x,y);
         }
      }
      /* Replace s by its refined value */
      s = 0.25*(s + (bx-ax)*(by-ay)*sum/(tnm*tnm));
      return s;
   }
}

#undef FUNC

或者各位虫友能有更好的解决这个积分的办法,也可以麻烦告知,多谢
mathematica中如何实现c++程序转换为mathematica语句
积分函数.png
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xzczd

木虫 (小有名气)

【答案】应助回帖

★ ★
感谢参与,应助指数 +1
ilovexiaomu: 金币+2, 谢谢,我下来试试 2015-05-13 16:35:27
既然你只是要数值积分的话那用NIntegrate不就得了吗?还有就算是Integrate你那语法也明显是错的。用软件前要仔细看看自带帮助!
小木虫Mathematica版块已毁(当然原本也不咋的),建议大家前往百度贴吧或Stackexchange。
2楼2015-05-13 10:38:53
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

walk1997

金虫 (著名写手)

目测这种2维积分对Mathematica应该很简单的吧
另外 看C代码的名字,可能用的是比较简单的求和规则做积分
建议自己先把被积函数的表达式写成Mathematica代码 这样别人也方便调试
3楼2015-05-13 16:23:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ilovexiaomu

金虫 (小有名气)

问题已经解决
4楼2015-05-19 11:38:12
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 ilovexiaomu 的主题更新
信息提示
请填处理意见