Miscalculation of Lagrange interpolation formula for higher degree - c

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.

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;
}

Loop to get sum of factorial / exponent

Hello I wrote a loop to get the sum of the factorial of given value n divided by n with increasing exponent. To describe it better it looks like this:
But for some reason my loop is always returning the value 1 whenever I input a number.
Here's my loop:
int nVal, i, j, k, nProduct = 1, nSum = 0, nFactorial = 1;
float fResult;
for (i = 1; i <= nVal; i++)
{
for (j = 1; j <= nVal; j++)
{
nFactorial *= j;
nSum += nFactorial;
}
for (k = 1; k <= nVal; k++)
{
nProduct *= k;
}
fResult += (nSum * 1.0) / (nProduct * 1.0);
}
Any fixes I can try?
OP's code is incorrect in the numerator and denominator calculation. Also, the integer math readily overflows.
To better handle large n, form each term based on the prior term with floating point math.
double sum_fact_expo(int n) {
double sum = 0.0;
double ratio = 1.0;
for (int i = 1; i <= n; i++) {
ratio *= 1.0*i/n;
sum += ratio;
}
return sum;
}
How big will n get? This can dictate what the type for nProduct and nFactorial should be (e.g. int or long long or __int128 or double).
The nested/inner loops are wrong. You only want/need the for loop for i.
And you'll [possibly/probably] overflow/underflow if you wait to calculate the ratio. So, do this at each iteration.
Your factorial calculation line was okay. But, the nProduct calculation was incorrect. The multiplier has to be n [and not j].
You don't initialize fResult, so it starts with a random value (i.e. undefined behavior). This would be flagged if you compiled with -Wall [and -O2] to enable warnings.
Remember that:
k! = (k - 1)! * k
And, that:
n**k = n**(k - 1) * n
So, we can build up nFactorial subterms and nProduct subterms iteratively, in a single loop.
Here's what I think your code should be like:
// pick _one_ of these:
#if 0
typedef int acc_t;
#endif
#if 0
typedef long long acc_t;
#endif
#if 0
typedef __int128 acc_t;
#endif
#if 1
typedef double acc_t;
#endif
double
calc1(int nVal)
{
int i;
acc_t nProduct = 1;
acc_t nFactorial = 1;
double ratio;
double fResult = 0;
for (i = 1; i <= nVal; ++i) {
nFactorial *= i;
nProduct *= nVal;
ratio = (double) nFactorial / (double) nProduct;
fResult += ratio;
}
return fResult;
}
#include <stdio.h>
int main(void) {
int nVal, i, j, k, nProduct = 1, nFactorial = 1;
float fResult = 0;
scanf("%d", &nVal);
for (i = 1; i <= nVal; i++)
{
for (j = 1; j <= i; j++)
{
//calculate 1! 2! 3! ... n!
//actually calculate i!
nFactorial *= j;
}
for (k = 1; k <= i; k++)
{
//calculate n^1 n^2 n^3 ... n^n
//actually calculate n^i
nProduct *= nVal;
}
fResult += (nFactorial * 1.0) / (nProduct * 1.0);
nProduct = 1;
nFactorial = 1;
}
printf("%f\n", fResult);
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

Gradient descent returning nan

I need to write a function to get a curve fit of a dataset. The code below is what I have. It attempts to use gradient descent to find polynomial coefficients which best fit the data.
//solves for y using the form y = a + bx + cx^2 ...
double calc_polynomial(int degree, double x, double* coeffs) {
double y = 0;
for (int i = 0; i <= degree; i++)
y += coeffs[i] * pow(x, i);
return y;
}
//find polynomial fit
//returns an array of coefficients degree + 1 long
double* poly_fit(double* x, double* y, int count, int degree, double learningRate, int iterations) {
double* coeffs = malloc(sizeof(double) * (degree + 1));
double* sums = malloc(sizeof(double) * (degree + 1));
for (int i = 0; i <= degree; i++)
coeffs[i] = 0;
for (int i = 0; i < iterations; i++) {
//reset sums each iteration
for (int j = 0; j <= degree; j++)
sums[j] = 0;
//update weights
for (int j = 0; j < count; j++) {
double error = calc_polynomial(degree, x[j], coeffs) - y[j];
//update sums
for (int k = 0; k <= degree; k++)
sums[k] += error * pow(x[j], k);
}
//subtract sums
for (int j = 0; j <= degree; j++)
coeffs[j] -= sums[j] * learningRate;
}
free(sums);
return coeffs;
}
And my testing code:
double x[] = { 0, 1, 2, 3, 4 };
double y[] = { 5, 3, 2, 3, 5 };
int size = sizeof(x) / sizeof(*x);
int degree = 1;
double* coeffs = poly_fit(x, y, size, degree, 0.01, 1000);
for (int i = 0; i <= degree; i++)
printf("%lf\n", coeffs[i]);
The code above works when degree = 1, but anything higher causes the coefficients to come back as nan.
I've also tried replacing
coeffs[j] -= sums[j] * learningRate;
with
coeffs[j] -= (1/count) * sums[j] * learningRate;
but then I get back 0s instead of nan.
Anyone know what I'm doing wrong?
I tried degree = 2, iteration = 10 and got results other than nan (values around a few thousands) Adding one to iteration seems making magnitude of the results larger by about 3 times after that.
From this observation, I guessed that the results are being multiplied by count.
In the expression
coeffs[j] -= (1/count) * sums[j] * learningRate;
Both of 1 and count are integers, so integer division is done in 1/count and it will become zero if count is larger than 1.
Instead of that, you can divide the result of multiplication by count.
coeffs[j] -= sums[j] * learningRate / count;
Another way is using 1.0 (double value) instead of 1.
coeffs[j] -= (1.0/count) * sums[j] * learningRate;
Aside:
A candidate NAN source is adding opposite signed values where one is an infinity. Given OP is using pow(x, k), which grows rapidly, using other techniques help.
Consider a chained multiplication rather than pow(). The result is usually more numerically stable. calc_polynomial() for example:
double calc_polynomial(int degree, double x, double* coeffs) {
double y = 0;
// for (int i = 0; i <= degree; i++)
for (int i = degree; i >= 0; i--)
//y += coeffs[i] * pow(x, i);
y = y*x + coeffs[i];
}
return y;
}
Similar code could be used for the main() body.

How to calculate iterative and recursive solutions?

Following needs to be calculated (iterative und recursive Solution)
Sum = 1 – 1/2 + 1/3 – 1/4 + - . . . 1/n (n > 0) with float reihe (int n)
We have the following so far:
#include <stdio.h>
float reihe(int n)
{
float sum = 0;
if (n = 1)
sum = 1;
else
{
for (int i = 1; i < n; i++)
{
sum += (1 / i) - (1 / (1 + i));
}
}
return sum;
}
int main(void)
{
float z;
z = reihe(5);
printf("%d", z);
return 0;
}
Would appreciate any of your help. Have a good day.
What about something like this? Not tested, but it compiles and runs...
BTW/ take a close look at the use of 1.0. If you use 1 it is interpreted as an integer and not a float and you division is always rounded to 1 or 0 and you lose all decimal values...
float reihe(int n)
{
float sum = 0;
float base = 1.0;
for (int i = 1; i <= n; i++) {
if ( i % 2 ) {
sum += base/i;
}
else {
sum -= base/i;
}
}
return sum;
}

Resources