...

# UIL Solving Exp, Cos, Sin, and Tan functions (Taylor Series)

Ruler58585
12-04-2011, 03:51 AM
Series given are the first four fractions of the series (1st, 2nd, 3rd or 4th). I convert each place to the original x value it was generated from, this assumes they will match and then tests to see if they do indeed match up the equations that I reversed to come up with the x's in each are as follows.

e^x = 1 + x + (x^2)/2 + x^3/6
cos(x) = 1 - (x^2)/2 + (x^4)/24 - (x^6)/720
sin(x) = x - (x^3)/6 + (x^5)/120 - (x^7)/5040
tan(x) = x + x^3 + (2x^5)/15 + (17x^7)/315

I plan on adding tanh(-1/x), Ln (1+x) and any others i can find eventually, but first things first why doesn't this work?

This is my second program and i'd like to learn any quicker/more effective ways to code, criticism (likely lots) is welcome.

//exponential series and log series

#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

static float x1 = 0.0;
static float x2 = 0.0;
static float x3 = 0.0;
static float x4 = 0.0;
static float r1 = 0.0;
static float r2 = 0.0;
static float r3 = 0.0;
static float r4 = 0.0;

int variablereset ()
{
r1 = x1;
r2 = x2;
r3 = x3;
r4 = x4;
}

int main()
{
cout << "1st" << endl;
cin >> x1 ;
cout << "2nd" << endl;
cin >> x2 ;
cout << "3rd" << endl;
cin >> x3 ;
cout << "4th" << endl;
cin >> x4 ;
cout << "Awnser is " ";

variablereset();
cout<< r1 << " " << r2 << " " << r3 << " " << r4;
r3 = r3 * 2.0;
r3 = (pow(r3, 1.0/2.0));
r4 = r4 * 6.0;
r4 = (pow(r4, 1.0/3.0));

if (r1 == 1.0 && r2 == r3 && r3 == r4)
{
cout << pow(2.71828183 , r2/1.0);
cout<< "e";
}

variablereset();
r2 = r2 * -6;
r2 = (pow(r2, 1.0/2.0));
r3 = r3 * 24;
r3 = (pow(r3, 1.0/4.0));
r4 = r4 * -120;
r4 = (pow(r4, 1.0/6.0));

if (r1 == 1 && r2 == r3 && r3 == r4)
{
cout << cos (r2);
cout<< "cos";
}

variablereset();
r2 = r2 * 6;
r2 =(pow(r2, 1.0/3.0));
r3 = r3 * 120;
r3 =(pow(r3, 1.0/4.0));
r4 = r4 * 5040;
r4 =(pow(r4, 1.0/6.0));

if (r1 == r2 && r2 == r3 && r3 == r4)
{
cout<< sin (r2);
cout<< "sin";
}

variablereset();
r2 =(pow(r2,1.0/3.0));
r3 = (r3*15)/2 ;
r3 =(pow(r3,1.0/5.0));
r4 = (r4 * 315)/17;
r4 =(pow(r4,1.0/7.0));

if (r1 == r2 && r2 == r3 && r3 == r4)
{
cout<< tan (r2);
cout<< "tan";
}

cout << " " << endl;
return 0;

}

I played with removing parts of code and testing using (1, 6, 18, 36) 1=1, 6=6, (18*2)^1/2=6, and (36*6)^1/3 i got the values 1,6,6,6 however when i check the 3rd term to the 4th term it comes false, the last "6" doesn't seem to be valid for some reason

I do realize this will not be 100% accurate I assume i'm given real fractional or decimal values and positive x values (though handling negatives as well would be nice)
Help appreciated!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum