04-01-2020, 01:03 PM
Hi everyone, I recently came across an x = tan (x) equation about x. Find x> 0, the solution over the interval [k * pi, (k + 1/2) * pi] (k is a positive integer). It is found that when k is taken large, the error occurs.
Calculation output:142106699.971
Very large error。
mathematica supports high-precision operations
Calculation output:{x -> 314160.836152123035796438894350}
I wrote it in C (dichotomy), compared it, and found that the error increases with increasing k.
Contrast with MMA, found this
Red represents the MMA result, blue represents the C language calculation result, and the accuracy gap is widened.
Code:
subst(tan(x)-x,x=fsolve(tan(x)=x,x=100000.5*pi))
Very large error。
mathematica supports high-precision operations
Code:
FindRoot[Tan[x] - x, {x, 100000.5*Pi}, WorkingPrecision -> 30]
I wrote it in C (dichotomy), compared it, and found that the error increases with increasing k.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pi 3.14159265358
#define n 10000
double MidPoint(double (*function)(double),double x0,double x1,double error)
{
double start=x0,end=x1;
do{
if((*function)((start+end)*0.5)<0)
{
start=(start+end)*0.5;
}else
{
end=(start+end)*0.5;
}
}while(end-start>error);
return (end+start)*0.5;
}
double equation(double x) {return tan(x)-x;}
int main()
{
double next,last=0;
char s[20];
FILE *file=fopen("/Users/yangyongkang/Desktop/a.txt","w");
for(int k=1;k<=n;k++)
{
next=MidPoint(equation,k*pi,(k+0.5)*pi,1e-11);
sprintf(s,"%f\n",last*last*sin(next-last));
fputs(s,file);
last=next;
}
fclose(file);
}
Contrast with MMA, found this
Code:
Show[ListPlot @@@ {{#1^2*Sin[#2 - #1] & @@@
Partition[
ParallelTable[
First@Values@
FindRoot[Tan[x] - x, {x, n*3.14159265358},
WorkingPrecision -> 20], {n, 1.5, 10000.5, 1}], 2, 1],
PlotStyle -> Red}, {ToExpression@
StringSplit@Import["/Users/yangyongkang/Desktop/a.txt"]}}]
Red represents the MMA result, blue represents the C language calculation result, and the accuracy gap is widened.