'series'에 해당되는 글 2건

  1. 2007.07.12 Taylor Sin(x) 함수 구현
  2. 2007.07.12 Taylor Series 1
posted by 써니루루 2007. 7. 12. 22:46

Sin 을 처리하는 함수를 유사하게 만들어봅니다.

이상하게 255' 이상은 값이 제대로 나오질 않는군요 ㅎㅎ

문제 해결하시게 되면 트랙백 좀 해주세요 ㅎㅎ

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruru.Math
{
    public class Sin
    {
        public static double GetSin(double x)
        {
            double sum = 0.0;
            double r = 1.0;

            for (int i = 1; i < 13; i++)
            {
                r *= x / (1.0 * i);

                switch (i % 4)
                {
                    case 1: sum += r; break; // 1
                    case 3: sum += (-1) * r; break; // -1
                    case 2:
                    case 4: break; // 0
                }
            }
            return sum;
        }

        public static void PrintSin()
        {
            for (int i = 0; i < 180; i++)
                Console.WriteLine("Sin({0}) = {1}", i, GetSin(System.Math.PI*i/180.0));
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruru.Math
{
    class SinMain
    {
        static void Main(string[] args)
        {
            Sin.PrintSin();
        }
    }
}


posted by 써니루루 2007. 7. 12. 16:42
http://mathworld.wolfram.com/TaylorSeries.html


e^x = e^a[1+(x-a)+1/2(x-a)^2+1/6(x-a)^3+...]



Exp(x) 를 C#으로 짜려면 위 식으로 짜야하는데 10번정도까지만 돌려보면 적당히 비슷한 값이 나온다.

빡시다 -_ -;;


소스는 다음과 같다

        /// <summary>
        /// 1+1x/1! + 1x2/2! + 1x3/3!
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double Expo(double x)
        {
            double sum = 1.0;
            double r = 1.0;

            for (int i = 1; i < 13; i++)
            {
                r *= x / (1.0 * i);
                sum += r;
            }

            return sum;
        }