Calculating the taylor series of sinx in C [duplicate] - c

Task: According to the Taylor Series of sin(x) calculate with using a double function named mysin pass it to a double variable. Take a x value from user and use the mysin function to calculate sin(x).
Problem is program gives me wrong value of sin(x). I have been trying to solve that issue about 4 hours but couldn't find it. Is it because of sin(x) function or have I missed something in my code?
My Code:
#include <stdio.h>
double mysin(double x)
{
double value = x;
double sum = x;
int neg_pos = 1;
int fac = 1;
int counter = 0;
while(1)
{
neg_pos *= -1;
fac += 2;
value = value/(fac*(fac-1));
value = value*x*x*neg_pos;
sum += value;
//printf("Hello");
counter++;
if (counter == 100) break;
}
return sum;
}
int main()
{
double number;
scanf("%lf",&number);
printf("%g",mysin(number));
//printf("%g",number);
}

The problem is that you're multiplying by neg_pos each step, which toggles between +1 and -1. That means the terms change sign only half the time, whereas they should change sign each time.
The fix is to just multiply by -1 each time rather than neg_pos.
Here's a working, slightly-simplified form of your program that calculates sin for a range of numbers from 0 to 3, showing the stdlib calculation to compare.
#include <math.h>
#include <stdio.h>
double mysin(double x) {
double value = x;
double sum = x;
int fac = 1;
for (int counter = 0; counter < 100; counter++) {
fac += 2;
value = -value*x*x/fac/(fac-1);
sum += value;
}
return sum;
}
int main() {
for (double x = 0.0; x < 3.0; x += 0.1) {
printf("%g: %g %g\n", x, mysin(x), sin(x));
}
}
You can also avoid the separate fac and counter variables, perhaps like this:
double mysin(double x) {
double term=x, sum=x;
for (int f = 0; f < 100; f++) {
term = -term*x*x/(2*f+2)/(2*f+3);
sum += term;
}
return sum;
}

From what I'm understanding you are not calculating the power correctly
Firtsly use this:
#include <math.h>
Then create a factorial function:
int factorial(int x){
int result = 1;
for(int i = 1; i < x; i++){
result += result * i;
}
return result;
}
And finally:
while(1)
{
neg_pos *= -1;
fac += 2;
power = pow(x,fac);
fac = factorial(fac);
sum += power/fac;
//printf("Hello");
counter++;
if (counter == 100) break;
}

Related

How to code the summation of a function in C?

EDIT: I've added the main, factorial, and trapGamma function to give the full picture but I am specifically talking about the for loop for iSum in the I function.
Basically I've run out of ideas and exhausted everywhere I know of to find an answer to this. I need to code a program that will compute a complex function which represents an M/M/1 queue.
The function includes sub functions such as calculating the integral of a gamma function and computing factorials. I've written all the code for the computations but my sum is giving me huge numbers when I would expect nothing higher than about .35
#include <math.h>
#include <stdio.h>
double I(int k, double t);
double trapGamma(double z);
unsigned long long int factorial(unsigned int n);
int main()
{
int k;
int i = 0;
double dt = 0.1;
printf("Ikx = [ \n");
for (t = 14.0 ; t <= 15.0; t += dt)
{
printf("%f " , t);
for (k = 1 ; k <= 10 ; k++)
{
I(k, t);
printf("%f " , I(k, t));
}
printf("\n");
}
printf(" ];\n");
return (0);
}
double I(int k, double t)
{
unsigned long long int x;
unsigned int n = 20;
double numerator, y, pow1, c;
double iSum;
double Ix;
int i = 0;
iSum = 0.0;
Ix = 0.0;
a = .25 * pow(t , 2);
b = pow(a, i);
x = factorial(n);
y = trapGamma(k + i + 1);
iSum = (b / (x * y));
//This is the sum loop that I'm having trouble with, I've broke the iSum equation down for my own readability while coding right above this comment
for (i = 0; i <= 100 ; i++)
{
iSum += i;
}
Ix = (pow((.5 * t), k) ) * iSum;
return Ix;
}
/*
I've checked both the factorial and trapGamma functions and they are giving me the expected results.
*/
unsigned long long int factorial(unsigned int n)
{
if(n <= 1)
return 1;
else
return (n * factorial(n - 1));
}
double trapGamma (double z)
{
int i , N = 100;
double gamma;
double a = 0.0;
double b = 15.0;
double x1, x2, y1, y2;
double areai;
double w = (b - a) / N;
gamma = 0.0;
for (i = 1; i < N; i++)
{
x1 = a + ((i - 1) * w); //the left bound point
x2 = a + (i*w); //the right bound point
y1 = pow(x1,z - 1)*exp(-x1); //the height of our left bound
y2 = pow(x2, z - 1)*exp(-x2); //the height of our right bound
areai = ((y1 + y2) / 2.0) * (x2 - x1);
gamma += areai;
}
return gamma;
}
This is building upon another project where I used a bessel function to create the M/M/1 queue over a 60 second span so I can see what this one is supposed to be. I've checked both my trapGamma and factorial functions results on there own and they are both working as expected.
How are summations supposed to be coded?
If the intent of the posted code is to calculate the modified Bessel function I, there are some pitfalls and useful semplifications to be aware of. Given
Trying to calculate the factorial, the value of the Gamma function, their product and the powers separately for each term of the sum leads to integer overflow sooner than later.
It's better to update the value of each addend of the sum instead.
Also, given that k is a whole, we have Γ(n) = (n - 1)!
The addends are increasingly smaller and, after some iterations, too small to be added to the sum, given the limited precision of type double.
// Evaluates x^k / k! trying not to overflow
double power_over_factorial(double x, int k)
{
double result = 1.0;
for ( int i = 1; i <= k; ++i )
{
result *= x / i;
}
return result;
}
#define MAX_ITERS 20
double modified_Bessel_I(int k, double x)
{
x /= 2;
const double xx = x * x;
double partial = power_over_factorial(x, k);
double old_sum, sum = partial;
int m = 1;
do
{
old_sum = sum;
partial *= xx / ((m + k) * m);
sum += partial;
}
while ( old_sum != sum && ++m < MAX_ITERS );
return sum;
}
Testable here.

Calculating cos(x) through Maclaurin series approximation using a factorial function and a cosine one

I have an assignment to code a program to calculate cos(x) through the Maclaurin approximation. However I must use a function for the cos(x) and another one to calculate the exponentials that go on the denominators inside the cos(x) function. I think most of this is right, but I'm probably missing on something and I can't figure out what.
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int fat(int);
float cosx(float);
int main()
{
float x1;
/* Original code: **x1 = x1 * 3.14159 / 180;** `transforms the value to radians` */
x1 = x1 * 3.14159 / 180; /* transforms the value to radians */
printf("Insert number:\n");
scanf("%f", &x1);
printf("Cosine of %f = %f", x1, cosx(x1));
return 0;
}
int fat(int y)
{
int n, fat = 1;
for(n = 1; n <= y; n++)
{
fat = fat * n;
}
return fat;
}
float cosx(float x)
{
int i=1, a = 2, b, c = 1, e;
float cos;
while(i < 20)
{
b = c * (pow(x,a)) / e;
cos = 1 - b;
a += 2;
e = fat(a);
c *= -1;
i++;
}
return cos;
}
If I input 0 it returns -2147483648.000000, which is clearly wrong.
First error is uninitialized variable x1, and right after that you have use:
int x1; // <<< uninitiated variable;
**x1 = x1 * 3.14159 / 180;** `transforms the value to radians
this will produce random value, you should put
int x = 0; // or some other value of your choice
In my opinion you should move x1 = x1 * 3.14159/100; after scanf("%d", x1).
Than again uninitiated value e before use.
int i=1, a = 2, b, c = 1, e;
...
b = c * (pow(x,a)) / e;
...
than you have in the line b = c * pow(x,a) where you go out of range of int variable potentially. If e = 1, x = 2 and a > 31 you are out of range for b. Another problem is pow(x,a) is rising much faster than `e. thus you get bigger and bigger values thus you are getting another overflow. And here is the code that works:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long double fact(int);
long double cosx(double);
long double my_pow (double b, int e);
int main()
{
double x1 = 45.00;
printf("Insert number:\n");
scanf("%lf", &x1);
x1 = x1 * 3.14159 / 180; // ** `transforms the value to radians`
printf("Cosine of %f = %.10LF", x1, cosx(x1));
return 0;
}
long double fact(int y)
{
int n;
double fact = 1;
for(n = 1; n <= y; n++)
{
fact *= n;
}
return fact;
}
long double cosx(double x)
{
int a = 2, c = -1;
long i = 0, lim = 500;
long double cos = 1;
long double b = 0, e = 0;
while(i < lim) {
e = fact(a);
b = c * my_pow(x,a);
cos += b/e;
// printf ("%le %le %le\n", e, b, cos);
a += 2;
c *= -1;
i++;
}
return cos;
}
long double my_pow (double b, int e) {
long double pow = 1;
for (;e > 0; --e, pow *= b)
;
return pow;
}

Using functions to solve other functions (sinx, cosx, exp) in C

my professor gave me a confusing problem and I'm completely lost on what to do. Basically he wants me to solve cos(x) and exp(x) (he gave an example for sinx) without using the built in math.h functions. Not only that, but he wants me to use both the fact() and power() functions to solve it. I could most likely solve it through straight math but I'm a bit lost on using the functions. I understand the general concept but I suck at math (especially trig) and I'm not exactly sure what they functions are doing arithmetically. Any helps or points to the right direction will be greatly appreciated.
#include <stdio.h>
#include <math.h>
int main()
{
char more;
double x, mySin(), myCos(), myExp();
printf("\n\n\t\tInput X: ");
scanf("%lf", &x);
do {
printf("\n\n\t\t\tLibraryResult MyResult");
printf("\n\n\tsin< %5.2f> %7.5f %7.5f", x, sin(x), mySin(x));
printf("\n\n\tcos< %5.2f> %f %f", x, cos(x), myCos(x));
printf("\n\n\texp< %5.2f> %f %f", x, exp(x), myExp(x));
printf("\n\n\t\tDo More (Y/N)? ");
scanf("%s",&more);
}
while (more == 'y'||more == 'Y');
}
double power(double x, int n)
{
int i = 0;
double prod = 1.;
for ( ; i++ < n; )
prod = prod * x;
return prod;
}
double fact (int n)
{
int i;
double prod = 1.;
for (i = 1; i <= n; i++)
prod = prod * i;
return prod;
}
double mySin(double x)
{
int i, sign;
double sum = 0;
for (i = 0, sign = 1; i < 21; i++, sign = -sign)
sum = sum + sign * power(x, 2 * i + 1)/ fact(2 * i + 1);
return sum;
}
double myCos(double x)
{
}
double myExp(double x)
{
}

End condition for precision integration loop

I'm trying to integrate the function 1/((1+x^2)x^0.5) using the trapezium rule. I need the precision to be as great as possible so I am therefore increasing the number of strips, N, until the computer cannot recognise a change between the total for consecutive N. However, the end condition is not currently working, leading to continuous integration. Does anyone have any better suggestions than my current code?
Many thanks,
Beth
#include<stdio.h>
#include<math.h>
#include<float.h>
double inter(double x, double h, double y, double N, double total)
{
total= total +0.5*(1/((1+pow(x,2))*sqrt(x)));
x=x+h;
while (x<y)
{
total=total+(1/((1+pow(x,2))*sqrt(x)));
x=x+h;
//printf("x - %.16lf \n", x);
}
total= total +0.5*(1/((1+pow(x,2))*sqrt(x)));
total=total*h;
//printf("t - %lf \n", total);
return total;
}
main()
{
double x,y,total,h,value,newvalue,f, N;
int finish;
x=0.1;
y=1000;
total=0;
N=1000;
finish=0;
value=0;
while(finish==0)
{
h=(y-x)/(N-1);
newvalue=inter(x,h,y,N,total);
printf("h-%.16lf\n", h);
printf("N-%.16lf\n", N);
printf("New value %.16lf\n", newvalue);
printf("c-%.16lf\n", value);
if(value==newvalue)
{
finish=1;
printf("finish-%d\n", finish);
}
else
{
value=newvalue;
newvalue=newvalue-3;
N=N+1000;
printf("newvalue-%lf\n", newvalue);
printf("value-%lf\n", value);
}
}
printf("%lf\n", value);
}
If you wish to create an automatic refinement of your numerical integration, one technique is to look at the relative convergence of your integration.
double previous = 0;
double current = inter( x, (y-x)/(N-1), y, N, total ); // Solve some baseline
do
{
N = N + 1000;
h = (y-x)/(N-1);
previous = current;
current = inter( x, h, y, N, total );
} while( abs( current - previous ) / current > 0.001 );
That code will stop after you observe less than 0.1% relative refinement in your estimation. Decreasing 0.001 will effectively increase your accuracy. Usually the best way to compare doubles is through a tolerance check like:
abs( a - b ) < k
where k is some factor of the order of accuracy you wish to achieve.
This integral is difficult because the f(x) -> ∞ as x -> 0. In this example, I changed the range to 1 to 1000. I also used a summation function to minimize rounding error when summing up a large number of values. The integral from wolframalpha ~= .487474, this program results in ~=.487475 . The exact integral can be found using this link:
integral 1/((1+x^2)sqrt(x))
#include<stdio.h>
#include<math.h>
#include<float.h>
/* clear array */
void clearsum(double asum[2048])
{
size_t i;
for(i = 0; i < 2048; i++)
asum[i] = 0.;
}
/* add a number into array */
void addtosum(double d, double asum[2048])
{
size_t i;
while(1){
/* i = exponent of d */
i = ((size_t)((*(unsigned long long *)&d)>>52))&0x7ff;
if(i == 0x7ff){ /* max exponent, could be overflow */
asum[i] += d;
return;
}
if(asum[i] == 0.){ /* if empty slot store d */
asum[i] = d;
return;
}
d += asum[i]; /* else add slot to d, clear slot */
asum[i] = 0.; /* and continue until empty slot */
}
}
/* return sum from array */
double returnsum(double asum[2048])
{
double sum = 0.;
size_t i;
for(i = 0; i < 2048; i++)
sum += asum[i];
return sum;
}
double fx(double x)
{
return 1./((1.+x*x)*sqrt(x));
}
double inter(double x, double y, double n)
{
double asum[2048]; /* for summation functions */
double h;
double d;
if(n < 1.){
n = 1.;
h = 0.;
} else {
h = (y-x)/(n-1.0);
}
y -= h/2.;
clearsum(asum);
d = .5*h*fx(x);
addtosum(d, asum);
for( ; x < y; x += h){
d = h*fx(x);
addtosum(d, asum);
}
d = .5*h*fx(x);
addtosum(d, asum);
d = returnsum(asum);
return d;
}
int main()
{
double x,y,n,value,newvalue;
x=1.0;
y=1000.;
value=0.;
for(n = 100000000.; 1; n += 100000000.)
{
newvalue=inter(x,y,n);
printf("new value %.16lf %.0lf\n", newvalue, n);
if(fabs(newvalue-value) < (newvalue*1E-7))
break;
value = newvalue;
}
return 0;
}
Using Simpson's rule, the results are more accurate and converge at much smaller values for n:
#include<stdio.h>
#include<math.h>
#include<float.h>
/* clear array */
void clearsum(double asum[2048])
{
size_t i;
for(i = 0; i < 2048; i++)
asum[i] = 0.;
}
/* add a number into array */
void addtosum(double d, double asum[2048])
{
size_t i;
while(1){
/* i = exponent of d */
i = ((size_t)((*(unsigned long long *)&d)>>52))&0x7ff;
if(i == 0x7ff){ /* max exponent, could be overflow */
asum[i] += d;
return;
}
if(asum[i] == 0.){ /* if empty slot store d */
asum[i] = d;
return;
}
d += asum[i]; /* else add slot to d, clear slot */
asum[i] = 0.; /* and continue until empty slot */
}
}
/* return sum from array */
double returnsum(double asum[2048])
{
double sum = 0.;
size_t i;
for(i = 0; i < 2048; i++)
sum += asum[i];
return sum;
}
double fx(double x)
{
return 1./((1.+x*x)*sqrt(x));
}
double simpson(double x, double y, double n)
{
double asum[2048]; /* for summation functions */
double h;
double a;
if(n < 1.){
n = 1.;
h = 0.;
} else {
h = (y-x)/(n-1.0);
}
y += h/2.;
clearsum(asum);
for( ; x < y; x += h){
a = h/6.*(fx(x) + 4.*fx(x + h/2.) + fx(x + h));
addtosum(a, asum);
}
a = returnsum(asum);
return a;
}
int main()
{
double x,y,n,value,newvalue;
x=1.0;
y=1000.;
value=0.;
for(n = 1000.; 1; n += 1000.)
{
newvalue=simpson(x,y,n);
printf("new value %.16lf %.0lf\n", newvalue, n);
if(fabs(newvalue-value) < (newvalue*1E-10))
break;
value = newvalue;
}
return 0;
}

Hopalong fractals, checking c synatx

I have written following code in order to produce simple list of double pairs to import in plot program.
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
double x=2,y=3;
for(i = 0; i < 1000; i++){
x = y- x/fabs(x)*sqrt(fabs(x+0.7));
y = 0.3-x;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}
I don't get what I expect from this functions. Instead of hopalong fractal I get linear progression graph. Is this only syntax error?
When you assign y, you use the new value of x, which has just been updated. The calculation requires the x value from the last step. Make a copy and use that:
int main(void)
{
double x = 2;
double y = 3;
int i;
for(i = 0; i < 1000; i++) {
double xx = x;
x = y - x/fabs(x)*sqrt(fabs(x + 0.7));
y = 0.3 - xx;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}

Resources