从 Maple 翻译为 C++

发布于 2024-11-02 21:57:51 字数 1366 浏览 1 评论 0原文

嘿, 所以我有一个执行二分法的枫树程序,我必须将其转换为 C++。我尝试根据枫树论坛上的代码生成帮助所说的内容对其进行转换,但它不断抛出错误。我希望得到一些帮助。 谢谢,

这是枫树的代码


使用二分法解决以下数学问题: 一个。方程的最小正根,

f(x):=evalf(1/x-evalf(Pi)*cos(evalf(Pi)*x));

其中 delta = 10^-5 且 eps = 10^-6

plot(f(x),x=.05..10.0);

从上图我们可以得出结论,给定方程的最小正实根位于 0.0 和 2.0 之间

为了获得所需的准确度值,我们调用二分法根隔离间隔(0.01,2.0):

Bisect:=proc(funct_equation,ai,bi,Mi,epsfi,deltaxi) local k,M,a,b,u,v,w,c,e,epsf,deltax,feq, notsolved: M:=Mi: feq:=funct_equation: a:=ai: b:=bi: epsf:=epsfi: deltax:=deltaxi: notsolved:=true: u:=evalf(subs(x=a,feq)): v:=evalf(subs(x=b,feq)): printf("a=%+9.6f   %+12.6e\nb=%+9.6f   %+12.6e\n\n",a,u,b,v); e:=b-a; if (sign(u)<>sign(v)) then   printf(" n       x            f\n");   for k from 1 by 1 while (k<M and notsolved) do:
    e:=0.5*e;
    c:=a+e;
    w:=evalf(subs(x=c,feq)):
    printf("%2d  %+9.6f    %+12.6e\n",k,c,w);
    if (abs(e)<deltax or abs(w)<epsf) then
      notsolved:=false:
    else
      if (sign(w) <> sign(u)) then
        b:=c: v:=w:
      else
        a:=c: u:=w:
      fi:
    fi:    od:    printf("Root = %+9.6f  function = %+12.6e\n",0.5*(a+b),evalf(subs(x=0.5*(a+b),feq))); fi: end: with(plots):

警告,名称更改坐标已被重新定义

Bisect(f(x),0.01,2.0,30,1.0e-6,1.0e-5):

Hey,
So I have a maple program which does bisection method and I have to convert it to C++. I tried converting it according to what the code generation help on the maple forums said but it kept throwing out errors. I would appreciate some help in this.
Thanks,

Here is the code for maple


Use the bisection method to solve the following mathematical problem:
a. smallest positive root of equation

f(x):=evalf(1/x-evalf(Pi)*cos(evalf(Pi)*x));

with delta = 10^-5 and eps = 10^-6

plot(f(x),x=.05..10.0);

From graph above we can conclude that given equation has smallest positive real root located between 0.0 and 2.0

To get their values with accuracy required we invoke bisection method with root isolation interval (0.01,2.0):

Bisect:=proc(funct_equation,ai,bi,Mi,epsfi,deltaxi) local k,M,a,b,u,v,w,c,e,epsf,deltax,feq, notsolved: M:=Mi: feq:=funct_equation: a:=ai: b:=bi: epsf:=epsfi: deltax:=deltaxi: notsolved:=true: u:=evalf(subs(x=a,feq)): v:=evalf(subs(x=b,feq)): printf("a=%+9.6f   %+12.6e\nb=%+9.6f   %+12.6e\n\n",a,u,b,v); e:=b-a; if (sign(u)<>sign(v)) then   printf(" n       x            f\n");   for k from 1 by 1 while (k<M and notsolved) do:
    e:=0.5*e;
    c:=a+e;
    w:=evalf(subs(x=c,feq)):
    printf("%2d  %+9.6f    %+12.6e\n",k,c,w);
    if (abs(e)<deltax or abs(w)<epsf) then
      notsolved:=false:
    else
      if (sign(w) <> sign(u)) then
        b:=c: v:=w:
      else
        a:=c: u:=w:
      fi:
    fi:    od:    printf("Root = %+9.6f  function = %+12.6e\n",0.5*(a+b),evalf(subs(x=0.5*(a+b),feq))); fi: end: with(plots):

Warning, the name change coords has been redefined

Bisect(f(x),0.01,2.0,30,1.0e-6,1.0e-5):

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

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

发布评论

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

评论(1

美人如玉 2024-11-09 21:57:51

如果将 feq 保留为过程,则不需要 subs 调用。

restart:
Bisect:=proc(func::procedure,ai,bi,Mi,epsfi,deltaxi)
local k::integer,
  M::integer,
  a,b,u,v,
  w::float,
  c,e,
  epsf::float,
  deltax,
  notsolved;
  M:=Mi:
  a:=ai: b:=bi: epsf:=epsfi:
  deltax:=deltaxi: notsolved:=true:
  u:=func(a);
  v:=func(b);
  printf("a=%+9.6f   %+12.6e\nb=%+9.6f   %+12.6e\n\n",a,u,b,v);
  e:=b-a;
  if (sign(u)<>sign(v)) then
    printf(" n       x            f\n");
    for k from 1 by 1 while (k<M and notsolved) do
      e:=0.5*e;
      c:=a+e;
      w:=func(c);
      printf("%2d  %+9.6f    %+12.6e\n",k,c,w);
      if (abs(e)<deltax or abs(w)<epsf) then
        notsolved:=false:
      else
       if (sign(w) <> sign(u)) then
         b:=c: v:=w:
       else
         a:=c: u:=w:
       fi:
     fi:
   od:
   printf("Root = %+9.6f  function = %+12.6e\n",0.5*(a+b),func(0.5*(a+b),feq));
 fi:
 0.5*(a+b);
end:

with(plots):

f:=subs(Pi=evalf[16](Pi),proc(x::float) 1/x-Pi*cos(Pi*x); end proc);

Bisect(f,0.01,2.0,30,1.0e-6,1.0e-5);

f(%);

CodeGeneration[C](f);

CodeGeneration[C](Bisect);

另外,如果您从 f 的表达式开始,您始终可以使用 unapply 将其转换为运算符(一种过程,但也可以由代码生成) > 命令。

例如,我还可以通过以下方式创建过程 f。 (请注意,其中一个在生成的 C 代码中生成 Pi 的默认 10 位近似值,另一个生成 16 位近似值。)

f_expression := 1/x-Pi*cos(Pi*x);

f:=unapply(f_expression, [x::float]);

CodeGeneration[C](f);

f:=subs(Pi=evalf[16](Pi),unapply(f_expression, [x::float]));

CodeGeneration[C](f);

You won't need that subs call, if you keep your feq as a procedure.

restart:
Bisect:=proc(func::procedure,ai,bi,Mi,epsfi,deltaxi)
local k::integer,
  M::integer,
  a,b,u,v,
  w::float,
  c,e,
  epsf::float,
  deltax,
  notsolved;
  M:=Mi:
  a:=ai: b:=bi: epsf:=epsfi:
  deltax:=deltaxi: notsolved:=true:
  u:=func(a);
  v:=func(b);
  printf("a=%+9.6f   %+12.6e\nb=%+9.6f   %+12.6e\n\n",a,u,b,v);
  e:=b-a;
  if (sign(u)<>sign(v)) then
    printf(" n       x            f\n");
    for k from 1 by 1 while (k<M and notsolved) do
      e:=0.5*e;
      c:=a+e;
      w:=func(c);
      printf("%2d  %+9.6f    %+12.6e\n",k,c,w);
      if (abs(e)<deltax or abs(w)<epsf) then
        notsolved:=false:
      else
       if (sign(w) <> sign(u)) then
         b:=c: v:=w:
       else
         a:=c: u:=w:
       fi:
     fi:
   od:
   printf("Root = %+9.6f  function = %+12.6e\n",0.5*(a+b),func(0.5*(a+b),feq));
 fi:
 0.5*(a+b);
end:

with(plots):

f:=subs(Pi=evalf[16](Pi),proc(x::float) 1/x-Pi*cos(Pi*x); end proc);

Bisect(f,0.01,2.0,30,1.0e-6,1.0e-5);

f(%);

CodeGeneration[C](f);

CodeGeneration[C](Bisect);

Also, if you start with an expression for f, you can always turn it into an operator (a sort of procedure, but which too can be code-generated) using the unapply command.

For example, I could also have created the procedure f in the following ways. (Note that one of these produces a default 10-digits approximation to Pi in the generated C code, and the other a 16-digit approximation.)

f_expression := 1/x-Pi*cos(Pi*x);

f:=unapply(f_expression, [x::float]);

CodeGeneration[C](f);

f:=subs(Pi=evalf[16](Pi),unapply(f_expression, [x::float]));

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