Code:
#include <stdio.h>
#include <math.h>
void polinomial(double a, double b, double c, double * x1, double * x2) {
double bSqrMns4ac;
printf("{a, b, c} = {%f, %f, %f}\n", a, b, c);
bSqrMns4ac = pow(b, 2) - 4 * a * c;
printf("bSqrMns4ac = %f\n", bSqrMns4ac);
if (bSqrMns4ac < 0) {
*x1 = 0;
*x2 = 0;
} else {
*x1 = (-b - sqrt(bSqrMns4ac))/ (2 * a);
*x2 = (-b + sqrt(bSqrMns4ac))/ (2 * a);
printf("{x1, x2} = {%f, %f}\n", (-b - sqrt(bSqrMns4ac))/ (2 * a), (-b + sqrt(bSqrMns4ac))/ (2 * a));
}
}
int main (void) {
double beta1 = -1.88138742; // beta1: price coefficient
double beta2 = 0.07182447; // beta2: price^2 coefficient; Note that beta2 is a in the polinomial.
double beta3 = 0.30941525; // beta3: coefficient of MSC labelling effect on wild salmon
double beta4 = 0.70772077; // beta4: coefficient of AB labelling effect on farmed cod
double beta5 = 0.69330401; // beta5: coefficient of AB labelling effect on farmed salmon
double betaLabel[3] = {beta3, beta4, beta5}; // We have three c for the polinomial; each labelling effect coefficient reflects a c.
int priceStar = 8;
double b, wtpMSC[2], wtpABCod[2], wtpABSalmon[2];
printf("\nbeta1 = %f, \nbeta2 = %f, \nbeta3 = %f (%f), \nbeta4 = %f (%f), \nbeta5 = %f (%f)\n\n", beta1, beta2, beta3, betaLabel[0], beta4, betaLabel[1], beta5, betaLabel[2]);
b = 2 * beta2 * priceStar + beta1;
polinomial(beta2, b, betaLabel[0], & wtpMSC[1], & wtpMSC[2]);
printf("WTP(MSC) = %f, %f\n\n", wtpMSC[1], wtpMSC[2]);
polinomial(beta2, b, betaLabel[1], & wtpABCod[1], & wtpABCod[2]);
printf("WTP(AB on cod) = %f, %f\n\n", wtpABCod[1], wtpABCod[2]);
polinomial(beta2, b, betaLabel[2], & wtpABSalmon[1], & wtpABSalmon[2]);
printf("WTP(AB on salmon) = %f, %f\n\n", wtpABSalmon[1], wtpABSalmon[2]);
return 0;
}