taylor series in c++

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 11970 Views - Last Post: 25 July 2010 - 11:34 AM Rate Topic: -----

#1 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

taylor series in c++

Posted 09 July 2010 - 04:11 AM

hello

please see this picture

Posted Image

now my question if x = 2.3145

Q1 how much will be cosine, sine and tangent?

Q2 i tried to ues the above fourmlaes to find the value of x but it's not correct,so could you please show me how can i cuclate it?

please note that i'm really poor in math

then after i know the answers i can do it in c++

waiting for you

Is This A Good Question/Topic? 0
  • +

Replies To: taylor series in c++

#2 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

Re: taylor series in c++

Posted 09 July 2010 - 04:36 AM

now it's correct if i type sin(2.3.145) in cuclater so will equals to 0.735966

but what is the fourmlas for?? the fourmlas make me confuse
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: taylor series in c++

Posted 09 July 2010 - 07:14 AM

1. Read this wiki entry
2. Then check out our snippet section

In short, The Taylor Series is an approximation of the trig function(s).
Was This Post Helpful? 0
  • +
  • -

#4 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

Re: taylor series in c++

Posted 09 July 2010 - 08:28 AM

i really don't understand the math

for the second link it's ok but my question

Quote

it's correct if i type sin(2.3.145) in cuclater so will equals to 0.735966

but what is the fourmlas for?? the fourmlas make me confuse

Was This Post Helpful? 0
  • +
  • -

#5 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: taylor series in c++

Posted 09 July 2010 - 08:37 AM

The formula is used as an approximation for sin(x). For alot of sin(x) values, there is no precise answer, therefore we need a way to aproximate those vlaues. For example, sin(pi/4) is equal to 1/sqrt(2), however sqrt(2) isn't a rational number, so we can only approximate it's value.

If you look at the full series, you get an infinite sum (so getting a precise value isn't possible). So I would imagine that your calculator does something along the lines of evaluating the part of the taylor series until a reasonable approximation is made.

See Here: http://en.wikipedia....ies_definitions

L.N
Was This Post Helpful? 1
  • +
  • -

#6 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

Re: taylor series in c++

Posted 09 July 2010 - 09:17 AM

thanks for your addition

here is my way whan i want to use the fourma for sin(x)

lt say x = 1

sin(1) = 1 - 1^3/3*2*1 + 1^5/5*4*3*2*1 and so on, but this way is not correct

so i;m lookeing for the correct way ( x = 1)

so can anybody show me?

one more Q

what is the relationship between the forumlas above and this forumla Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: taylor series in c++

Posted 09 July 2010 - 09:31 AM

there is a math lib call 'math' that has these functions for you so to get the tangent you would type "float answer=tan(x,y)" where x is your sine and y is your cosine (i think). tangent is defined as sine over cosine. sine is defined as cosine over tangent and cosine is defined sine over tangent.

This post has been edited by xtreampb: 09 July 2010 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: taylor series in c++

Posted 09 July 2010 - 09:31 AM

~edit~
muti post

This post has been edited by xtreampb: 09 July 2010 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#9 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: taylor series in c++

Posted 09 July 2010 - 09:32 AM

The way you described looks right ... makes sure that your calculator is in radians, not in degrees and make sure that you're using double values in your program. Of course, like I said, your answer won't be exact, but it should be a good approximation. And the relationship you provided is just the terms of each taylor series.

L.N

This post has been edited by LivingNightmare: 09 July 2010 - 09:34 AM

Was This Post Helpful? 1
  • +
  • -

#10 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

Re: taylor series in c++

Posted 09 July 2010 - 11:18 AM

thanks #9 LivingNightmare

but my way is not correct

can you please try and show me here?

waiting for you
Was This Post Helpful? 0
  • +
  • -

#11 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: taylor series in c++

Posted 09 July 2010 - 11:23 AM

Hmm well, let me give an example.

To evaluate sin(pi) you would calculate sin(pi) = pi - (pi^3)/3! + (pi^5)/5! - (pi^7)/7! + (pi^9)/9! ... and you would keep going until you have the desired precision. However, in this case sin(pi) is simply equal to 0. Also, note that for a positive integer N, N! = N*(N-1)*(N-2)*...*2*1. So 5! would be equal to 5*4*3*2*1 = 120.

L.N
Was This Post Helpful? 0
  • +
  • -

#12 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: taylor series in c++

Posted 09 July 2010 - 11:47 AM

So here's a quick example i've coded.


#include <iostream>
#include <cmath>
using namespace std;

double factorial(double num)
{
    double ret = 1;

    while(num > 0)
    {
        ret *= num;
        num--;
    }

    return ret;
}

double MySine(double rad, int n)
{
    double value = 0;

    for(int i = 0; i < n; i++)
    {
        value += pow(-1.0, i) / factorial((2*i) + 1) * pow(rad, ((2*i) + 1));
    }

    return value;
}

int main()
{
    double p = 3.1415926;
    cout<<"Sine of pi is: " <<MySine(p, 12) <<endl;

    return 0;
}




The factorial function simply evaluates N! - Then MySine approximates Sin(rad) by using it's taylor series expansion.

L.N
Was This Post Helpful? 0
  • +
  • -

#13 empror9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 09-July 10

Re: taylor series in c++

Posted 09 July 2010 - 02:25 PM

many thanks

and i find the correct way

first i have to do this

3!=6
5!=120
7!=5040
9!=362880

then

sin(1)=1-1/6+1/120-1/5040+1/362880
Was This Post Helpful? 0
  • +
  • -

#14 LivingNightmare  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 129
  • Joined: 07-July 10

Re: taylor series in c++

Posted 09 July 2010 - 02:43 PM

Yeah that would be correct. The implementation of cos/tan/and other trig functions would be really similar to this one, so this should serve as a pretty good guide to follow.

L.N

This post has been edited by LivingNightmare: 09 July 2010 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#15 Guest_empror9*


Reputation:

Re: taylor series in c++

Posted 24 July 2010 - 04:42 PM

back again,

this is my function to approximate sin(x)

float sin(float x) 
{ 
        float result = x; 
 
        int limit = 15; 
        int sign = -1; 
 
        for(int i=3 ; i<limit ; i+=2,sign=-sign) 
                result += sign*result*(x*x)/(i*(i-1)); 
 
        return result; 
}


and this to approximate cos(x)


float cos(float x) 
{ 
        float result = 1; 
 
        int limit = 15; 
        int sign = -1; 
 
        for(int i=2 ; i<limit ; i+=2,sign=-sign) 
                result += sign*result*(x*x)/(i*(i-1)); 
 
        return result; 
} 




but the answer is not clear in some cases and in other cases it displays wrong answers

try to input 1.5707963

cos should equals to 0.0000000

but my function shows -0.263853, so the answer is not that good

waiting for you
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2