Вопрос

I wrote a program that gets from the user a value x and an integer n, the program then prints arcsin(x) using the taylor serie for arcsin http://tedmuller.us/Math/img/Taylor-arcsin.gif

but for some reason its not working. when i enter x=1 i get an output of 1.19 rather than pi/2.

here's my code:

#include <stdio.h>
#include <conio.h>
void main()
{
    int i,n;
    double x,sum,last;
    printf("Please enter the x you wish to calculate arcsin(x) for \n");
    scanf("%lf",&x);
    printf("Enter n\n");
    scanf("%d",&n);
    last=x;
    sum=last;
    for(i=1;i<=n;i++)
    {
        last*=((x*x)*(2*i-1))/((2*i)*(2*i+1));
        sum+=last;
    }
    printf("arcsin(%lf) = %lf",x,sum);
    getch();
}

basic idea is this: last and sum both start with value of x. then i advance last to be the next number in the serie, add it to sum, advance last again, add to sum...rinse and repeat n times.

Это было полезно?

Решение

Looks like you have a mistake in your expansion.

Try this: last*=((x*x)*(2*i-1)*(2*i-1))/((2*i)*(2*i+1))

You'll still need lots of terms. With 1000 terms I get arcsin(1.0) ~= 1.552963 . ( Your code 1.194958 )

10000 terms arcsin(1.0) ~= 1.565155. ( Your code 1.194958 )

Другие советы

One problem is that that series converges logarithmically quickly when x=1. Notice that x=1 is at the boundary of arcsin's radius of convergence. So it's not surprising that you'll get extremely slow convergence.

A bigger problem is that you didn't actually implement the series you thought you had implemented. You're summing something that converges linearly. It looks like it might be the integral of something well-knkown, but it's certainly not arcsin.

The Taylor series you expanded has six-place accuracy for |x| < .5, five-place for |x| < .6, four-place for |x| < .7, three-place for |x| < .8, and two-place for |x| < .9.

Of course, there's no reason to think the Taylor polynomial is the best polynomial of a given degree. Welcome to Numerical Analysis.

It takes too many terms to get a good estimation for |x| = 1, because the derivative of arcsin(x) has a pole at x = 1, so that its Taylor series converges very slowly. This means the Taylor expansion is not an efficient way of approximating arcsin(x) except for small x. If you printed each term of the expansion as you calculate it, you'd see it's extremely small to make the series converge in a reasonable amount of time.

For your aid, Milton Abramowitz and Irene Stegun in their book "Handbook of Mathematical Functions", p.81 derive this approximation formula:

arcsin(x) = pi/2 - sqrt(1 - x)(a0 + a1*x + a2*x^2 + a3*x^3)

where

a0 = 1.5707288
a1 = -0.2121144
a2 = 0.0742610
a3 = -0.0187293

which performs much better near 1.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top