开发者

Help implementing Least Squares algorithm in C [was: want to find the square root of an array]

the formula is pretty complicated. the numerator is num and the denominator is den, in the formula there is a root on the denominator so i have putted den in sqrrt() but sqrrt only accepts doubles

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
void开发者_Python百科 main(void)
{    int i;
    float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
    float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
    float num,den[LEN],r[LEN],xy[LEN],x2[LEN],y2[LEN];
    for(i=0;i<LEN;i++)
    {
        x2[i]=x[i]*x[i];
        y2[i]=y[i]*y[i];
        xy[i]=x[i]*y[i];
    }
    num=sum(xy)-sum(x)*sum(y);
    for(i=0;i<LEN;i++)
    {
        den[i]=((LEN*sum(x2)-(sum(x))*(sum(x)))*(LEN*sum(y2))-(sum(y2))*(sum(y2)));
        r[i]=num /sqrt(den);  /*<----------the problem is here-----> */

    }
    printf("%f",r);
    getch();
}
float sum(float arr[])
    {
    int i;
    float total=0;
    for(i=0;i<=LEN;i++)
    {
        total+=arr[i];
    }
    return total;
    }


Out of sheer boredom I have fixed your code. It is still ugly and extremely inefficient but compiles and should work. I'll leave you or someone else to make it decent.

#include <stdio.h>
#include <math.h>
#define LEN 11


// for the following set of x and y find r by the formula ..
float sum(float arr[]);
int main(void)
{ int i;
      float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
      float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};


      float num,den,r[LEN],xy[LEN],x2[LEN],y2[LEN];
      for(i=0;i<LEN;i++)
          {
                x2[i]=x[i]*x[i];
                y2[i]=y[i]*y[i];
                xy[i]=x[i]*y[i];
              }
      num=LEN*sum(xy)-sum(x)*sum(y);
      den = (LEN*sum(x2)) - sum(x)*sum(x);

      float alpha = sum(y)/LEN - (num/den)*sum(x)/LEN;

      printf("beta = %f,  alpha = %f\n", num/den, alpha);     
      for(i=0;i<LEN;i++)
          {
            float term = y[i] - alpha - (num/den)*x[i];
            r[i] = (term*term);

                printf("%f",r[i]);
          }
}
float sum(float arr[])
  {
      int i;
      float total=0;
      for(i=0;i<=LEN;i++)
          {
                total+=arr[i];
              }
      return total;
      }


To be consistent with the rest of the code, you should presumably be writing:

r[i] = num / sqrt(den[i]);

However, the calculation is not one I recognize. The body of the second loop is going to produce the same result for each value in den and therefore also in r, which is probably not what the question asked for.


You need to give the index den[i] at the denominator....instead in your code you have just passed the base address!

 r[i]=num /sqrt(den[i]);

If this is what you want to achieve, which is quite unclear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜