-3

I'm new to programming. I have a problem, if you know could you help me please. My program is about finding coefficients of polynomial interpolation. I used numerical recipes subroutines in code. What could be the error?

#include<stdio.h>
#include<math.h>
#include "nrutil.h"
#include <stdlib.h>
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
{
int i,m,ns=1;
float den,dif,dift,ho,hp,w;
float *c,*d;

dif=fabs(x-xa[1]);
c=vector(1,n);
d=vector(1,n);
for (i=1;i<=n;i++) {
    if ( (dift=fabs(x-xa[i])) < dif) {
        ns=i;
        dif=dift;
    }
    c[i]=ya[i];
    d[i]=ya[i];
}
*y=ya[ns--];
for (m=1;m<n;m++) {
    for (i=1;i<=n-m;i++) {
        ho=xa[i]-x;
        hp=xa[i+m]-x;
        w=c[i+1]-d[i];
        if ( (den=ho-hp) == 0.0) nrerror("Error in routine polint");
        den=w/den;
        d[i]=hp*den;
        c[i]=ho*den;
    }
    *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
}
free_vector(d,1,n);
free_vector(c,1,n);
}
void polcof(float xa[], float ya[], int n, float cof[])
{
void polint(float xa[], float ya[], int n, float x, float *y, float *dy);
int k,j,i;
float xmin,dy,*x,*y;

x=vector(0,n);
y=vector(0,n);
for (j=0;j<=n;j++) {
    x[j]=xa[j];
    y[j]=ya[j];
}
for (j=0;j<=n;j++) {
    polint(x-1,y-1,n+1-j,0.0,&cof[j],&dy);
    xmin=1.0e38;
    k = -1;
    for (i=0;i<=n-j;i++) {
        if (fabs(x[i]) < xmin) {
            xmin=fabs(x[i]);
            k=i;
        }
        if (x[i]) y[i]=(y[i]-cof[j])/x[i];
    }
    for (i=k+1;i<=n-j;i++) {
        y[i-1]=y[i];
        x[i-1]=x[i];
    }
}
free_vector(y,0,n);
free_vector(x,0,n);
} 
int main () {
FILE * fp;
fp = fopen("q1.dat", "w+");
int i, j, n=41;
float x[41], f[41], cof[i];
for (i=0; i<=40; i++) {
x[i]=0.05*i-1;
f[i]=(1+tanh(2*10*x[i]))/2;
printf("%f\t%f\n", x[i], f[i]);
fprintf(fp, "%f\n", f[i]); }
for (j=0; j<=40; j++) {
polcof(x, f, n, cof);
printf("%f\n", cof[j]); }
fclose(fp);
return 0; }

-1.000000   0.000000
-0.950000   0.000000
-0.900000   0.000000
-0.850000   0.000000
-0.800000   0.000000
-0.750000   0.000000
-0.700000   0.000000
-0.650000   0.000000
-0.600000   0.000000
-0.550000   0.000000
-0.500000   0.000000
-0.450000   0.000000
-0.400000   0.000000
-0.350000   0.000001
-0.300000   0.000006
-0.250000   0.000045
-0.200000   0.000335
-0.150000   0.002473
-0.100000   0.017986
-0.050000   0.119203
0.000000    0.500000
0.050000    0.880797
0.100000    0.982014
0.150000    0.997527
0.200000    0.999665
0.250000    0.999955
0.300000    0.999994
0.350000    0.999999
0.400000    1.000000
0.450000    1.000000
0.500000    1.000000
0.550000    1.000000
0.600000    1.000000
0.650000    1.000000
0.700000    1.000000
0.750000    1.000000
0.800000    1.000000
0.850000    1.000000
0.900000    1.000000
0.950000    1.000000
1.000000    1.000000
TheDorkSide
  • 17
  • 1
  • 5

1 Answers1

3
int i;
float x[41], f[41], cof[i];

This is a problem. i is not initialized at the time cof is declared. i should be initialized to the number of floats you want to cof to hold. Since your for loop iterates from 0 to 39, you presumably want cof to have 40 floats.

Alternatively (to conform to pre-C99 requirements), you can use malloc:

float * cof = (float *) malloc(sizeof(float)*40);

In addition to conforming to pre-C99 requirements, this will allow you to reallocate a larger size if necessary. It will, however, require deallocation via free() if you don't want memory leaks.

There might be other problems in your code, but this is definitely one of them.

master_latch
  • 434
  • 5
  • 12
  • 2
    1. C has [Variable-length arrays](http://en.wikipedia.org/wiki/Variable-length_array) since C99. (What is wrong here though is that `i` is used uninitialized) 2. [Don't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Kninnug Dec 02 '13 at 17:43
  • yes, that was my stupid careless mistake of writing i inside cof, but why should I allocate memory? Could you clarify, please? – TheDorkSide Dec 02 '13 at 17:48
  • Dynamic memory allocation using malloc, calloc, realloc gives you run-time control (as opposed to compile-time control) not only over how long it should start out, but whether or not the length should change. It's a completely different type of memory. – ciphermagi Dec 02 '13 at 17:51
  • @kninnug Thanks. I am pretty ignorant of many of the C99 improvements as I tend to prefer making my code more backward compatible. – master_latch Dec 02 '13 at 18:42