0

I have a formula which I have implemented in C, I have done so but the result I receive is different from MATLAB

The code:

  double sumVector(float x[], int M){
    
    
        double y = 0;
        int i;
    
        for(i = 1; i<M ; i++){
    
    
            y += (0.5*x[i]) + ((x[i])*(x[i])* (cos(floor(x[i]/4) - 32)));
    
    
        }
    
        return y;
    }

Where x[] is an array with elements 0:0.001:255

  • The result in C is 37022697.82
  • The result in Matlab is -12767828.5

Why is there such a large variation and what causes this?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Sagar
  • 11

2 Answers2

2

I guess you mean that your array x[] contains the elements 0.0, 0.001, 0.002 ... 255.0. Is that correct?

With this assumption, the following C code gives -12767828.504138, which agrees with your MATLAB result (within numerical accuracy). As pointed out in the comments, you sould be careful with float and double, and you probably want your function to loop over all elements of x, including x[0]. Besides that, I don't know how you initialize your array and how you call the function.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

double sumVector(double x[], int M){


  double y = 0.0;
  int i;

  for(i = 0; i<M ; i++){


    y += (0.5*x[i]) + ((x[i])*(x[i])* (cos(floor(x[i]/4.0) - 32.0)));


  }

  return y;
}

int main()
{
  const int M = 255001;
  const double delta = 0.001;
  double *x = malloc(M * sizeof(double));
  int i;

  // Fill the array. Is that what you want?
  for(i = 0; i < M; i++) {
    x[i] = i * delta;
  }

  printf("Result = %f\n", sumVector(x, M));

  free(x);

  return 0;
}
Figaro
  • 112
  • 5
0

@WeatherVane and @TroyHaskin have an answer for your question in the comments: You are likely not correctly iterating through your array, so your vector calculation in C is probably not what you want it to be. (We can only assume that your Matlab implementation is correct.)

In C, array indexes start at 0, not at 1. We can only assume that M is the number of elements in the array x and that you really wanted to include all M elements in your for loop. So the elements of x range from x[0] through x[M-1]. In your code, you are not including the first element x[0] in your calculation. Try this instead:

double sumVector(double x[], int M) {
  double y = 0.0;
  int i;

  for(i=0; i<M; i++){
    y += 0.5*x[i] + x[i]*x[i]*cos(floor(x[i]/4.0) - 32.0));
  }
  return y;
}

Notice that I have changed the type of your x to an array of double. In Matlab, double is the default precision of floating point numbers. Think as if the same were true for C: use double unless you really know what you're doing.

This i=0; i<MAX; i++ format is common practice in C. Get used to it. It should look weird to start at i=1 or to test for i<=MAX. (It doesn't mean it's necessarily wrong, but it should catch the eye.)

Also, @Olaf's comment warns you to be careful about division in C. If both operands are of an integer type, it will perform integer division. If you use, for example, 1.0 instead of 1, it forces floating-point division. It also makes it more clear to the human reader that you are using floating point constants instead of integer constants.

In Matlab, cos accepts radians and the C library cos() also uses radians.

Community
  • 1
  • 1
e0k
  • 6,961
  • 2
  • 23
  • 30