Reynevan Reynevan - 1 month ago 6
C Question

How would I go about converting these equations to C code?

I have these two equations

enter image description here
enter image description here

and I need to convert them to C code where you input

k
and
x
. The thing is I don't get that advanced levels of math, neither did I learn C in the past :D

Can anyone show me step by step what built-in functions can be used for this and how exactly should the logic behind the app work?

Cheers!

Answer

Your formula is wrong. As shown here (along with the proof of the derivation) the correct formula is

enter image description here

You have k and n swapped in your summation. The inputs should then be x and n. The correct code is then:

#include <math.h>
double sum_of_sin(double x, int n) {
    if (sin(x/2) == 0.0) {
        return 0.0; //prevent division by 0 for x multiple of 2π
    }
    return sin(n*x/2) * sin((n+1)*x/2) / sin(x/2);
}