近似arcsinc


9

目标很简单:xsin(x) = -mx给定输入的情况下m,以最少的字节数输出方程的非零实解。

规格:

  • 您的答案必须是3位有效数字的正确答案。
  • 您可以输出除平凡解决方案以外的任何实际解决方案x=0。您可以假设m存在至少一种解决方案。您也可以假设m!=0

一个使用梯度下降的明显次优的python解决方案:

from math import *
from random import *
a=x=0.001
m = 5.
def dE(x):return 2*(sin(x)+m*x+1)*(cos(x)+m)
for i in xrange(1000): x-=dE(x)*a
print x

测试用例

-0.25 -> ±2.4746
-0.1  -> ±2.8523 or ±7.0682 or ±8.4232
 0.2  -> ±4.1046 or ±4.9063 

1
最好的方法是打印一个固定值,尽管您应该指定需要多少个小数位。我建议包括一个输入参数,例如aresolve sin(x)=-ax。请不要说“您必须实际计算”,因为这样的要求太含糊而无法工作。
xnor

另外,这x=0是一个简单的解决方案。您应指定所需的解决方案。
xnor

您需要对m进行某些限制以确保非零解。
xnor

m=0有解决方案(x=kπ用于整数k)。其值m没有非平凡的实解是那些离太远的值0
彼得·泰勒

1
您是否仅在寻找实值解决方案或是否允许复值解决方案?
英里

Answers:


1

ised32 28字节

使用从π开始的牛顿迭代:

{:x-{sinx+$1*x}/{cosx+$1}:}:::pi

参数传入$1,可以从文件中获取,如下所示:

ised --l inputfile.txt 'code'

稳定性较差,但版本较短:

{:{x-tanx}/{1+$1/cosx}:}:::pi

有时会抛出迭代极限警告,但考虑到条件,其准确性似乎很好。

Unicode版本(相同的字节数):

{λ{x-tanx}/{1+$1/cosx}}∙π

从4开始切下一个字节,似乎收敛到相同的值

{λ{x-tanx}/{1+$1/cosx}}∙4

8

Haskell,34个字节

f m=until(\x->sin x< -m*x)(+1e-3)0

计数x由0.001 0,直到sin(x)< -m*x

Ouput的例子

f -0.2 ->   2.595999999999825
f -0.1 ->   2.852999999999797
f  0.0 ->   3.141999999999765
f  0.1 ->   3.4999999999997256
f  0.2 ->   4.1049999999997056

m=-0.1
彼得·泰勒

@PeterTaylor请注意,如果需要的话,但是它给出2.853,看起来很正确。
xnor

当然,它们都是奇数函数,因此,如果有一个解决方案,那就是一个肯定的解决方案。h
彼得·泰勒

您为什么要回答一个您不清楚的挑战?
Mego

2

Mathematica,28个字节

x/.FindRoot[Sinc@x+#,{x,1}]&

从初始猜测中搜索数字根x=1。测试用例:

% /@ {-0.25, -0.1, 0.2}
(* {2.47458, 2.85234, 4.10462} *)

1

C,99字节

#include<math.h>
float f(float m){float x=1,y;do{x=(y=sin(x)+m*x)+x;}while(fabs(y)>1e-4);return x;}

松散:

#include<math.h>
float f(float m){
 float x=1,y;
 do{x=(y=sin(x)+m*x)+x;}while(fabs(y)>1e-4);
 return x;
}

1

MATL,17个字节

`@2e3/tY,wG_*>}4M

它在正实轴上使用线性搜索,因此速度较慢。在在线编译器中,所有测试用例均在1分钟内结束。

在线尝试!

说明

`         % Do...while
  @       %   Push iteration index, starting at 1
  2e3/    %   Divide by 2000
  t       %   Duplicate
  Y,      %   Sine
  w       %   Swap
  G_*     %   Multiply by minus the input
  >       %   Does the sine exceed that? If so, next iteration
}         % Finally (execute after last iteration, before exiting loop)
   4M     %   Push input of sine function again
          % Implicit end
          % Implicit display

1

C ++ 11,92 91字节

-1个字节供使用 #import

#import<cmath>
using F=float;F f(F m,F x=1){F y=sin(x)+m*x;return fabs(y)>1e-4?f(m,x+y):x;}

0

Python 2,81 78字节

定点迭代

作为递归lambda

from math import*
f=lambda m,x=1:abs(sin(x)+m*x)>1e-4and f(m,sin(x)+m*x+x)or x

作为循环(81个字节):

from math import*
m=input()
x=1
while abs(sin(x)+m*x)>1e-4:x=sin(x)+m*x+x
print x

0

Mathematica,52个字节

NSolve[Sin@x==-x#,x,Reals][[;;,1,2]]~DeleteCases~0.&

匿名函数。以数字作为输入,并返回数字列表作为输出。仅用于NSolve求解近似方程。


如果您替换Sin@x==-x#Sinc@x==-#,则可以取消~DeleteCases~0.

0

公理,364字节

bisezione(f,a,b)==(fa:=f(a);fb:=f(b);a>b or fa*fb>0=>"fail";e:=1/(10**(digits()-3));x1:=a;v:=x2:=b;i:=1;y:=f(v);if(abs(y)>e)then repeat(t:=(x2-x1)/2.0;v:=x1+t;y:=f(v);i:=i+1;if i>999 or t<=e or abs(y)<e then break;if fb*y<0 then(x1:=v;fa:=y)else if fa*y<0 then(x2:=v;fb:=y)else break);i>999 or abs(y)>e=>"fail";v)
macro g(m) == bisezione(x+->(sin(x)+m*x), 0.1, 4.3)

高尔夫

bisezione(f,a,b)==
    fa:=f(a);fb:=f(b)
    a>b or fa*fb>0=>"fail"
    e:=1/(10**(digits()-3))
    x1:=a;v:=x2:=b;i:=1;y:=f(v)
    if(abs(y)>e) then
      repeat
        t:=(x2-x1)/2.0;v:=x1+t;y:=f(v);i:=i+1
        if i>999 or t<=e or abs(y)<e then break
        if      fb*y<0 then(x1:=v;fa:=y)
        else if fa*y<0 then(x2:=v;fb:=y)
        else break
    i>999 or abs(y)>e=>"fail"
    v

macro g(m) == bisezione(x+->(sin(x)+m*x), 0.1, 4.3)

结果

(3) -> g(0.2)
   AXIOM will attempt to step through and interpret the code.
   (3)  4.1046198505 579058527
                                                              Type: Float
(4) -> g(-0.1)
   (4)  2.8523418944 500916556
                                                              Type: Float
(5) -> g(-0.25)
   (5)  2.4745767873 698290098
                                                              Type: Float

0

Haskell,50个字节

我刚刚在calc类中了解了牛顿的方法,因此这里介绍了haskell使用牛顿的方法。

f m=foldl(\x _->x-(sin x+m*x)/(cos x+m))0[1..10]

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.