Unknown mathematical series in C - c

Does anybody have any idea what this code does? I suspect it has something to do with Taylor series, but I'm not sure since I don't really know what Taylor series is. However, it could be anything. I'm really not sure.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
double x, y, a;
int n;
int z;
x = 25.0;
if (z < 0) {
z = 1;
x = -x;
} else
z = 0;
n = 0;
a = 1;
y = 1;
for (n = n + 1; n < 20; n++) {
a = a * x / n;
y = y + a;
printf("%i \t %.20g \t %g \n", n, y, a);
}
if (z) {
x = -x;
y = 1.0 / y;
}
printf("%i \t %.20g \t %g \n", n, y, a);
return 0;
}

Enable compiler warnings.
if (z < 0) { is a coding error. #Pablo.
It certainly should be
if (x < 0) {
Unrolling
n = 0;
a = 1;
y = 1;
for (n = n + 1; n < 20; n++) {
a = a * x / n;
y = y + a;
The terms are
y = 1 + x/1 + x*x/(1*2) + x*x*x/(1*2*3) + .... pow(x,20)/(1*2*3*...*20)
Review Taylor series to discern which one.
BTW, For x==25, code Taylor's series does not converge enough. Either more terms are needed or better - an alternate/additional approach is needed.

Related

Wrong results with cosx (custom function) with functions(noobie)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
double sin1x(double converted,int i);
double cos1x(double converted,int i);
int main(){
int r,i = 1;
double converted,p,results,results1;
printf("Insert degrees from 0..2π: ");
scanf("%d", &r);
if(r < 0 || r > 360)
{
printf("NOT ACCEPTABLE DEGREES\n");
exit(0);
}
converted = r * PI / 180;
printf("Conversion from degrees to rad = %.3f", converted);
results = sin1x(converted,i);
results1 = cos1x(converted,i);
printf("\nsin(%d) = %.3f\n", r, results);
printf("\nsin(%d) of c = %.3f\n", r,sin(converted));
printf("\ncos(%d) = %.3f\n", r, results1);
printf("\ncos(%d) of c = %.3f\n", r,cos(converted));
return 0;
}
double sin1x(double x, int i)
{
int j = 3;
double sinx,numerator = x,pr;
sinx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
sinx = sinx + numerator;
else
{
if(i % 2 == 1)
sinx = sinx - numerator;
}
i++;
j+=2;
}
while(fabs(pr - numerator) > 0.000001);
return sinx;
}
double cos1x(double x, int i)
{
int j = 2;
double cosx,numerator = x,pr;
cosx = numerator;
do
{
pr = numerator;
numerator = pr * x * x / (j * (j - 1));
if(i % 2 == 0)
cosx = cosx + numerator;
else
{
if(i % 2 == 1)
cosx = cosx - numerator;
}
i++;
}
while(fabs(pr - numerator) > 0.000001);
return cosx;
}
Hello I try to make a program with cosx and sinx and for some reason I have a problem with cosx. I cannot find any issues with my program but the cos results are wrong.Also I have the sin() and cos() functions to compare the results. I tried changing j or making another
variable to factorial but it didn't change anything.
At least these problems:
Wrong initialization
// double cosx,numerator = x,pr;
double cosx,numerator = 1.0,pr;
Missing change to j
// Add to `cos1x()` do loop
j += 2;
Coarse PI
No reason to not use a more precise value.
// #define PI 3.14159265
#define PI 3.1415926535897932384626433832795
Spelling
Convertion --> Conversion
Candidate simplification
double cos1x_alt(double x) {
double xx = x * x;
double term = 1.0;
double sum = term;
for (int i = 1; 1.0 + term != 1.0; i += 2) {
term *= -xx / (i * (i + 1));
sum += term;
}
return sum;
}

Miscalculation of Lagrange interpolation formula for higher degree

I am approximating Runge’s function using Lagrange’s interpolation formula for 50 interpolation points. I have written the following program to do this, but I am getting the wrong value for x= -0.992008. That wrong value is 4817543.091313, but it should be 5197172.55933613. I have got this value from the following link: Link The code used are as follows:
#include <stdio.h>
#include <math.h>
double
runge(double x)
{
return (1 / (1 + (25 * x * x)));
}
double
ab(double x)
{
if (x < 0)
return -1 * x;
return x;
}
double
lag_func(double x, double *y_i, double *x_i, int n)
{
double ex = 0.0;
for (int i = 0; i <= n; i++) {
double numer = 1.0,
denom = 1.0,
prod = 1.0;
for (int j = 0; j <= n; j++) {
if (i != j) {
numer = (x - x_i[j]);
denom = (x_i[i] - x_i[j]);
prod *= numer / denom;
}
}
ex += (prod) * y_i[i];
}
return ex;
}
int
main()
{
int n;
scanf("%d", &n);
double y_i[n + 1],
x_i[n + 1];
for (int i = 0; i < n + 1; i++) {
x_i[i] = ((2 * (double) i) / (double) n) - 1;
y_i[i] = runge(x_i[i]);
}
printf("%lf\n", lag_func(-0.992008, y_i, x_i, n));
return 0;
}
The web site is rounding its Runge coefficients to six digits. Given the magnitudes of the terms involved, up to 3.9978•1011, this introduces multiple errors up to around 2•105.
This can be seen by inserting y_i[i] = round(y_i[i] * 1e6) / 1e6; after y_i[i] = runge(x_i[i]);. Then the output of the program is 5197172.558199, matching the web site’s inaccurate result.
The web site is wrong; the result of the code in the question is better.

Calculating Pi with C

I want to calculate pi but I am getting 3.058403 as a result. How can I fix this code?
#include <stdio.h>
int main(){
double x = 0;
float g =0;
printf("How many terms to calculate pi to? ");
scanf("%f", &g);
int n;
for (n = 0; n < g ; n++){
double z = 1.0 / (2 * n + 1);
if ((n % 2) == 1){
z = z * -1;
}
x = (x + z);
}
double p = 4 * x;
printf("The value of pi is: %f", p);
return 0;
}
This is not really an answer because the actual answer has been discussed in the comment section.
Corrected version of your code:
removed pointless parentheses
use meaningful variable names
use of int for integer comparison
declaration of variables as close as possible to their scope
code formatted properly
#include <stdio.h>
int main() {
printf("How many terms to calculate pi to? ");
int nbofterms;
scanf("%d", &nbofterms);
double x = 0;
for (int n = 0; n < nbofterms; n++) {
double z = 1.0 / (2 * n + 1);
if (n % 2 == 1) {
z *= -1;
}
x = (x + z);
}
double pi = 4 * x;
printf("The value of pi is: %f", pi);
return 0;
}

Find the sum of a mathematical series

Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........]
Test Data:
Input the Value of x :2
Input the number of terms : 5
Expected Output:
the sum = -0.415873
Number of terms = 5
Here is the code I wrote, no compilation error, I just wasnt getting the answer right:
#include <stdio.h>
#include <math.h>
int main()
{
float sum=0;
float ans;
int c, y, fac=1;
int a,i, x=2;
float z;
for (i = 1; i<=2; i++)
{
a= 2*i;
y = pow(2,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (float) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
You didn't used number of terms.
You didn't set fac to 1 to the end of the for.
y is equal to pow(x,a) not pow(2,a).
Int can store numbers between -2,147,483,648 and 2,147,483,647, so I recommend to use long long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807). And also, double instead of float. You can take a look here for more details: https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-160.
Here is the code:
#include <stdio.h>
#include <math.h>
int main()
{
double sum=0;
double ans;
long long c, y, fac=1;
long long a,i, x=2, numberOfTerms = 5;
float z;
for (i = 1; i<= numberOfTerms; i++)
{
a= 2*i;
y = pow(x,a);
for (c = 1; c<=a; c++)
{
fac= fac*c;
}
z = (double) y/fac;
if (i%2 == 0) {
sum = sum + z;
}
else{
sum = sum - z;
}
fac = 1;
}
ans = 1 + sum;
printf("The answer is %f" , ans);
return 0;
}
Try to use functions.
If you do not have to, do not use float. Use double for floating point calculations.
Your code does not reflect the formula at all.
double fact(unsigned n)
{
double result = 1.0;
for(unsigned x = 1; x <= n; x++)
result *= x;
return result;
}
double series(double x, unsigned nterms)
{
double result = 1;
for(unsigned term = 1; term <= nterms; term++)
{
result += (1.0 - ((term & 1) << 1)) * pow(x, (double)term * 2.0) / fact(term * 2);
}
return result;
}
int main(void)
{
for(unsigned nterms = 2; nterms < 20; nterms++)
{
printf("nterms = %2u series(4) = %.32f\n", nterms, series(4.0, nterms));
}
}
https://godbolt.org/z/8c3xM4

How to use raise to the power of x in c

My question is how do I compute 2^(x) in c. I know there is something like shifting that does the same thing. I tried to do total = x << 1, but that didn't work. I know that if i shift one bit it is the same as multiplying it by two. Or something like that.
int x;
for(x=0; x<4; x++){
total += x <<1; //
}
When this is done executing I expect the total to be 15 ( 20 + 21 + 22 + 23 )
Any ideas on what I am doing wrong? my total starts off as being 0 and then messes up.
Thanks!
It's the other way around. 1 << x will give you '2 ^ x'.
This should do what you want. Call pow(2, x) to get 2x.
int abs (int x) {
if (x < 0) return -x;
return x;
}
int signum (int x) {
if (x < 0) return -1;
if (x > 0) return 1;
return 0;
}
int add (int x, int y) {
for (int i = 0; i < abs(y); ++i) {
if (y > 0) ++x;
else --x;
}
return x;
}
int mult (int x, int y) {
int sign = signum(x) * signum(y);
x = abs(x);
y = abs(y);
int res = 0;
for (int i = 0; i < y; ++i) {
res = add(res, x);
}
return sign * res;
}
int pow (int x, int y) {
if (y < 0) return 0;
int res = 1;
for (int i = 0; i < y; ++i) {
res = mult(res, x);
}
return res;
}
Left shift is limited to the word size of your CPU, either 32 or 64 bits, which limits the maximum exponent which you can safely use, before the result is undefined (2^31 or 2^63).
The following works for larger exponents but uses floating point arithmetic. If you need exact results you should consider using a infinite precision maths library such as GMP
#include <math.h>
int main() {
double base = 2;
double exponent = 4;
double result = pow(base, exponent);
return 0;
}

Resources