Post Reply 
[CAS problem] High-precision operations in numerical solution equations
04-01-2020, 01:03 PM (This post was last modified: 04-01-2020 01:24 PM by yangyongkang.)
Post: #1
[CAS problem] High-precision operations in numerical solution equations
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.
Code:
subst(tan(x)-x,x=fsolve(tan(x)=x,x=100000.5*pi))
Calculation output:142106699.971
Very large error。

mathematica supports high-precision operations
Code:
FindRoot[Tan[x] - x, {x, 100000.5*Pi}, WorkingPrecision -> 30]
Calculation output:{x -> 314160.836152123035796438894350}


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.


Attached File(s) Thumbnail(s)
   

study hard, improve every day
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
[CAS problem] High-precision operations in numerical solution equations - yangyongkang - 04-01-2020 01:03 PM



User(s) browsing this thread: 1 Guest(s)