getPositiveValues won't return the values - c

What is not working:
In the code below, the values input in scanf under getPositiveValue will not return. They return as 0 no matter what the input is.
I have no clue how to get around this. Can someone show me why it is not working?
What I have tried:
I tried using return CHAN; and even return CHAN.n; and all the other members but that did not work.
My code:
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
int main(void)
{
CHANNEL CHAN;
void getPositiveValue(CHANNEL);
void displayTable(CHANNEL);
//Function declarations
printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN.n, &CHAN.S, &CHAN.B, &CHAN.D);
if(CHAN.n < 0 || CHAN.S < 0 || CHAN.B < 0 || CHAN.D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
} while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2 / 3));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}

The input problem you are having is because of the fact that functions are call by value in C. This means that when you pass a struct to a function, it is a copy of the struct that is worked with in the function, not the original. Any changes made to the struct within the getPositiveValue() function are not visible once control returns to main().
To fix this problem, pass a pointer to the structure. Use the -> operator to dereference the pointer and access members in one shot. Here is a modified version of your code. I also took the liberty of moving your function declarations to the top of the program.
There is also an error in the call to the pow() function found in computeVelocity():
U = pow(U, (2 / 3));
should be:
U = pow(U, (2.0 / 3.0));
The expression 2 / 3 performs integer division, with the result zero, so after this call to pow(), U is always 1. This can be easily fixed by forcing floating point division, as in the second line above.
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
void getPositiveValue(CHANNEL *);
void displayTable(CHANNEL);
int main(void)
{
CHANNEL CHAN;
printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(&CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL *CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN->n, &CHAN->S, &CHAN->B, &CHAN->D);
if(CHAN->n < 0 || CHAN->S < 0 || CHAN->B < 0 || CHAN->D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
}while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2.0 / 3.0));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}
Sample program interaction:
Enter the name of the channel: chan
Give the coefficient for roughness, slope, width, and maxdepth: 0.035 0.0001 10 4.2
Channel data for chan
Coefficient of roughness: 0.035000
Slope: 0.000100
Width: 10.000000
Maximum depth: 4.200000
Depth Average Velocity
0.168000 0.917961
0.336000 0.566077
0.504000 0.423161
0.672000 0.342380
0.840000 0.289368
1.008000 0.251450
1.176000 0.222759
1.344000 0.200172
1.512000 0.181859
1.680000 0.166669
1.848000 0.153840
2.016000 0.142843
2.184000 0.133301
2.352000 0.124935
2.520000 0.117535
2.688000 0.110939
2.856000 0.105020
3.024000 0.099677
3.192000 0.094829
3.360000 0.090410
3.528000 0.086363
3.696000 0.082644
3.864000 0.079214
4.032000 0.076040
4.200000 0.073095

There are many compiler error in your code. Here is my first try to fix it
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ {
char name[50];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
void getPositiveValue(CHANNEL);
void displayTable(CHANNEL);
int main(void) {
CHANNEL CHAN;
printf("Enter the name of the channel: ");
fgets(CHAN.name, 50, stdin);
getPositiveValue(CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL CHAN) {
int Flag; //sentinel
do {
Flag = FALSE;
printf("Give the coefficient for roughness: \n Give the slope: \n Give the channel width: \n Give the maximum depth of the channel: ");
scanf("%lf %lf %lf %lf", &CHAN.n, &CHAN.S, &CHAN.B, &CHAN.D);
if(CHAN.n < 0 || CHAN.S < 0 || CHAN.B < 0 || CHAN.D < 0) {
Flag = TRUE;
printf("The values must be positive.\n");
}
} while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN) {
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N];
double arrayvelocity[N];
int i;
for ( i = 0; i < N; i++) {
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2 / 3));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}
The first error would be struct definition. In C, you can define the struct and at the same time define a variable. But you should not use the same name to confuse yourself and the compiler. Also you need to understand void function does not return a value and cannot be on the right side of an = expression.
Use typedef can save you to type struct keyword each time you need it. You also need to use %s to output a string. Also typos here and there.

Related

How do I change the for loop to a while loop

The for(t = 0; t < tmax …) located at the bottom of the program loop should be a while loop (e.g. while (s.distance > 0)) that runs while the altitude is greater than 0. Depending on what thrust the user selects, the tmax may or may not be enough time for the lander to land. In short I need to change the for loop to a while loop to speed the fuel consumption.
#include "stdio.h"
#include <math.h>
#include "string.h"
#include "ctype.h"
struct _State //different to shorten variable names
{
double mass;
double thrust;
double accel;
double velo;
double distance;
double fuel;
char gameMode; // variable for keeping track of selected mode (A - 65 - Autopilot / M - 77 - Manual)
};
typedef struct _State State;
double getThrust(State* s) // Accepts user input for thrust and validates it
{
printf("Input Thrust bellow. \n--> ");
scanf("%lf", &s->thrust);
if (s->thrust < 0)
{
return 0;
}
else if (s->thrust > 45000)
{
return 45000;
}
else
return s->thrust;
}
void Update(State* s, double t, double stepSize)
{
const double gMoon = -1.6;
//double mass = 9000;
//double fuel = 1800;
s->thrust = getThrust(s);
s->mass = s->mass - (s->thrust / 3000);
s->accel = (s->thrust / s->mass) + gMoon;
s->velo += s->accel * stepSize;
s->distance += s->velo * stepSize;
s->fuel = s->fuel - (s->thrust / 3000);
}
void manualControl(State* s) //manual controll function
{
printf("\nManual mode: on\n");
printf("-----------------\n\n");
if (s->distance == 0) // Sets height value to the initial height value for calculations
{
s->distance = 15000;
}
if (s->velo == 0) // Sets velocity value to the initial velocity value for calculations
{
s->velo = -325;
}
}
void autopilotControl(State* s) // Autopilot controll function
{
printf("\nAutopilot: on\n");
printf("-----------------\n\n");
}
void modeHandler(State* s) // mode controller. Calls appropriate function depending on the mode selected
{
if (s->gameMode == 65)
{
autopilotControl(s);
}
else if (s->gameMode == 77)
{
manualControl(s);
}
}
void modeSelect(State* s) // modeSelect allows user to pick the game mode and validates their input.
{
printf("--> ");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);
if (s->gameMode == 65 || s->gameMode == 77)
{
modeHandler(s);
}
else
{
printf("\nInvalid input. Please try again!\n");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);
modeSelect(s);
}
}
int main(void)
{
State s = { 9000,0,0,-325,15000,1800 };
printf("Welcome to the Lunar Lander simulation!\n\n");
printf("Your goal is to land the vehicle safely on the moon.\n");
printf("You will be provided with information about the lander\nbased on which you will have to make a decision about\nhow much force should be applied to slow the vehicle down.\n");
printf("-----------------------------------------------------------\n\n");
printf("Choose your game mode.\n(Type \"A\" for autopilot) or (\"M\" for manual mode)\n");
modeSelect(&s);
double t = 0;
double tmax = 150;
const double stepSize = 1;
FILE* f = fopen("lander.csv", "wt");
if (!f)
{
printf("Unable to open file.\n");
system("pause");
return 1;
}
for (t = 0; t < tmax; t++)
{
while (t < tmax)
{
fprintf(f, "%.2f,%.2f,%.2f,%.2f%.2f,\n", t, s.accel, s.velo, s.distance, s.fuel);
Update(&s, t, stepSize);
printf("Current height: %.2f m\n", s.distance);
printf("Current velocity: %.2f m/s\n", s.velo);
printf("Fuel left : %.2f kg\n", s.fuel);
printf("---------------------------\n\n");
}
}
system("pause");
}
Here the ouput:
Choose your game mode.
(Type "A" for autopilot) or ("M" for manual mode)
--> M
Manual mode: on
-----------------
Input Thrust bellow.
--> 20
Current height: 14673.40 m
Current velocity: -326.60 m/s
Fuel left : 1799.99 kg
---------------------------
Input Thrust bellow.
-->
It seems the fuel decrement can be defined within the Update(...) method. So, would not it be the case of defining something like the following suggestion?
// ...
int t = 0;
int tmax = 150;
// ...
while ((s.fuel > 0.0) && (t < tmax))
{
fprintf(f, "%.2f,%.2f,%.2f,%.2f%.2f,\n", t, s.accel, s.velo, s.distance, s.fuel);
Update(&s, t, stepSize);
printf("Current height: %.2f m\n", s.distance);
printf("Current velocity: %.2f m/s\n", s.velo);
printf("Fuel left : %.2f kg\n", s.fuel);
printf("---------------------------\n\n");
t++;
}
// ...

how to print multiple max values using loop

Trying to calculate at which frequencies voltage hits max, i am able to print the most recent max but there may be lower values of frequency in which it is able to max voltage.
I am able to get the highest or lowest freq by switching the loop from + to - from or to 1000000 in increments of 10.
Tried nested if statement inside of VO > voMax
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.14f
#define Vi 5
#define L 4.3e-4
#define C 5.1e-6
int getFreq();
long getResist();
float getVO(float XL, float XC, int R);
float getXC(int f);
float getXL(int f);
int main()
{
long resist, freq, fMax;
float XL, XC, VO, voMax;
voMax = 0;
fMax = 0;
resist = getResist();
for (freq = 1000000; freq >= 0; freq -= 10)
{
XL = getXL(freq);
XC = getXC(freq);
VO = getVO(XL, XC, resist);
if (1000000 == freq)
{
fMax = freq;
voMax = VO;
}
else if (VO > voMax)
{
fMax = freq;
voMax = VO;
}
}
printf("VO = %f Frequency = %d\n", voMax, fMax);
getch();
return 0;
}
float getXL(long f)
{
float XL;
XL = 2 * PI * f * C;
return XL;
}
float getXC(long f)
{
float XC;
XC = 1 / (2 * PI * f * C);
return XC;
}
float getVO(float XL, float XC, long R)
{
float VO;
VO = (Vi * R) / sqrt((XL - XC) * (XL - XC) + R * R);
return VO;
}
int getFreq()
{
int freq;
freq = 0;
printf("please enter a frequency:");
scanf("%d", &freq);
return freq;
}
long getResist()
{
int resist;
resist = 0;
printf("please enter a resistance:");
scanf("%d", &resist);
return resist;
}
I want the voltage to print max at multiple freq.
Well, what you want is to generate "a lot" of data, and then make some analysis. I would actually implement it in two steps:
Generate the data (and save it in an array or in a file)
Do any analysis you need on that data.
After you get the desired result with this clear approach, you can move to the next step and try to optimize the algorithm, according to any optimization rule you need.
I want the voltage to print max at multiple freq.
I think you need a small code update. You have the following sequence:
voMax = 0;
fMax = 0;
resist = getResist();
for (freq = 1000000; freq >= 0; freq -= 10)
{
you should probably have:
fMax = 0;
resist = getResist();
for (freq = 1000000; freq >= 0; freq -= 10)
{
voMax = 0;
(I moved "voMax = 0;" inside the "for").
In that way, you can calculate max voltages for all frequencies, without interference from the other frequencies.

How to use exp and sqrt properties correctly

-use double precision
-use sqrt() and exponential function exp()
-use * to compute the square
-do not use pow()
I am getting values they are just not anything as to what I expected. I tried making them all signed but it didn't change anything and I've tried printing out with 12 decimal places and nothing seems to be working.I have linked the math library and defined it as well.
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double raise = 1.0/2.0*((x-mu)/sigma);
double func1 = func * exp(raise);
double comp_func = (func1 * func1);
return comp_func;
}
int main(void)
{
// create two constant variables for μ and σ
const double sigma, mu;
//create a variable for x - only dynamic variable in equation
unsigned int x;
//create a variable for N values of x to use for loop
int no_x;
//scaniing value into mu
printf("Enter mean u: ");
scanf("%lf", &mu);
//scanning value into sigma
printf("Enter standard deviation: ");
scanf("%lf", &sigma);
//if sigma = 0 then exit
if(sigma == 0)
{
printf("error you entered: 0");
exit(0);
}
//storing number of x values in no_x
printf("Number of x values: ");
scanf("%d", &no_x);
//the for loop where i am calling function normal N times
for(int i = 1; i <= no_x; i++)
{
//printing i for the counter in prompted x values
printf("x value %d : ", i);
// scanning in x
scanf("%lf", &x);
x = normal(x,sigma,mu);
printf("f(x) = : %lf.12", x);
printf("\n");
}
return 0;
}
C:>.\a.exe
Enter mean u: 3.489
Enter std dev s: 1.203
Number of x values: 3
x value 1: 3.4
f(X) = 0.330716549275
x value 2: -3.4
f(X) = 0.000000025104
x value 3: 4
f(X) = 0.303015189801
But this is what I am receiving
C:\Csource>a.exe
Enter mean u: 3.489
Enter standard deviation: 1.203
Number of x values: 3
x value 1 : 3.4
f(x) = : 15086080.000000
x value 2 : -3.4
f(x) = : 15086080.000000
x value 3 : 4
f(x) = : 1610612736.000000
Insert these lines:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Change:
const double sigma, mu;
to:
double sigma, mu;
Change:
unsigned int x;
to:
double x;
Replace the definition of the normal function with:
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp(-t*t/2);
}
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#include<math.h>
#include<stdio.h>
#include <stdlib.h>
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp((-0.5*t)* t);
}
I Finally got this code above working after tweaking with it literally all day lol, C math can be rather tricky, thank you for the help above as well.

How would you use the while statement to find the square root using the while loop

I have to write a program that will find a square root using the while loop. I was given this new_guess = (old_guess + (n / old_guess)) / 2.0; but I dont fully understand what to do with it, this is what I have:
int main(void)
{
double n, x, new_guess, old_guess, value;
printf("Enter a number:");
scanf("%lf", &n);
x = 1.00000;
while (new_guess >= n) {
new_guess = (old_guess + (n / old_guess)) / 2.0;
printf("%10.5lf\n", fabs(new_guess));
}
return 0;
}
x is the initial guess. Im really lost on how to do it. This is C also. I know its really wrong but I really dont understand how to make it start because when I enter a number it just stop right away.
Your program has undefined behavior because both new_guess and old_guess are uninitialized when you enter the loop.
The condition is also incorrect: you should stop when new_guess == old_guess or after a reasonable maximum number of iterations.
Here is a modified version:
#include <math.h>
#include <stdio.h>
int main(void) {
double n, x;
int i;
printf("Enter numbers:");
while (scanf("%lf", &n) == 1 && n >= 0.0) {
x = 1.0;
/* Using a while loop as per the assignment...
* a for loop would be much less error prone.
*/
i = 0;
while (i < 1024) {
double new_guess = (x + (n / x)) / 2.0;
if (new_guess == x)
break;
x = new_guess;
i++;
}
printf("%g: %.17g, %d iterations, diff=%.17g\n",
n, x, i, sqrt(n) - x);
}
return 0;
}
Given the start value, the number of iterations grows with the size of n, exceeding 500 for very large numbers, but usually less than 10 for small numbers. Note also that this algorithm fails for n = 0.0.
Here is a slightly more elaborate method, using the floating point break up and combine functions double frexp(double value, int *exp); and double ldexp(double x, int exp);. These functions do not perform any calculation but allow for a much better starting point, achieving completion in 4 or 5 iterations for most values:
#include <math.h>
#include <stdio.h>
int main(void) {
double n, x;
int i, exp;
printf("Enter a number:");
while (scanf("%lf", &n) == 1 && n >= 0.0) {
if (n == 0) {
x = 0.0;
i = 0;
} else {
frexp(n, &exp);
x = ldexp(1.0, exp / 2);
for (i = 0; i < 1024; i++) {
double new_guess = (x + (n / x)) / 2.0;
if (new_guess == x)
break;
x = new_guess;
}
}
printf("%g: %.17g, %d iterations, diff=%.17g\n",
n, x, i, sqrt(n) - x);
}
return 0;
}

fast & efficient least squares fit algorithm in C?

I am trying to implement a linear least squares fit onto 2 arrays of data: time vs amplitude. The only technique I know so far is to test all of the possible m and b points in (y = m*x+b) and then find out which combination fits my data best so that it has the least error. However, I think iterating so many combinations is sometimes useless because it tests out everything. Are there any techniques to speed up the process that I don't know about? Thanks.
Try this code. It fits y = mx + b to your (x,y) data.
The arguments to linreg are
linreg(int n, REAL x[], REAL y[], REAL* b, REAL* m, REAL* r)
n = number of data points
x,y = arrays of data
*b = output intercept
*m = output slope
*r = output correlation coefficient (can be NULL if you don't want it)
The return value is 0 on success, !=0 on failure.
Here's the code
#include "linreg.h"
#include <stdlib.h>
#include <math.h> /* math functions */
//#define REAL float
#define REAL double
inline static REAL sqr(REAL x) {
return x*x;
}
int linreg(int n, const REAL x[], const REAL y[], REAL* m, REAL* b, REAL* r){
REAL sumx = 0.0; /* sum of x */
REAL sumx2 = 0.0; /* sum of x**2 */
REAL sumxy = 0.0; /* sum of x * y */
REAL sumy = 0.0; /* sum of y */
REAL sumy2 = 0.0; /* sum of y**2 */
for (int i=0;i<n;i++){
sumx += x[i];
sumx2 += sqr(x[i]);
sumxy += x[i] * y[i];
sumy += y[i];
sumy2 += sqr(y[i]);
}
REAL denom = (n * sumx2 - sqr(sumx));
if (denom == 0) {
// singular matrix. can't solve the problem.
*m = 0;
*b = 0;
if (r) *r = 0;
return 1;
}
*m = (n * sumxy - sumx * sumy) / denom;
*b = (sumy * sumx2 - sumx * sumxy) / denom;
if (r!=NULL) {
*r = (sumxy - sumx * sumy / n) / /* compute correlation coeff */
sqrt((sumx2 - sqr(sumx)/n) *
(sumy2 - sqr(sumy)/n));
}
return 0;
}
Example
You can run this example online.
int main()
{
int n = 6;
REAL x[6]= {1, 2, 4, 5, 10, 20};
REAL y[6]= {4, 6, 12, 15, 34, 68};
REAL m,b,r;
linreg(n,x,y,&m,&b,&r);
printf("m=%g b=%g r=%g\n",m,b,r);
return 0;
}
Here is the output
m=3.43651 b=-0.888889 r=0.999192
Here is the Excel plot and linear fit (for verification).
All values agree exactly with the C code above (note C code returns r while Excel returns R**2).
There are efficient algorithms for least-squares fitting; see Wikipedia for details. There are also libraries that implement the algorithms for you, likely more efficiently than a naive implementation would do; the GNU Scientific Library is one example, but there are others under more lenient licenses as well.
From Numerical Recipes: The Art of Scientific Computing in (15.2) Fitting Data to a Straight Line:
Linear Regression:
Consider the problem of fitting a set of N data points (xi, yi) to a straight-line model:
Assume that the uncertainty: sigmai associated with each yi and that the xi’s (values of the dependent variable) are known exactly. To measure how well the model agrees with the data, we use the chi-square function, which in this case is:
The above equation is minimized to determine a and b. This is done by finding the derivative of the above equation with respect to a and b, equate them to zero and solve for a and b. Then we estimate the probable uncertainties in the estimates of a and b, since obviously the measurement errors in the data must introduce some uncertainty in the determination of those parameters. Additionally, we must estimate the goodness-of-fit of the data to the
model. Absent this estimate, we have not the slightest indication that the parameters a and b in the model have any meaning at all.
The below struct performs the mentioned calculations:
struct Fitab {
// Object for fitting a straight line y = a + b*x to a set of
// points (xi, yi), with or without available
// errors sigma i . Call one of the two constructors to calculate the fit.
// The answers are then available as the variables:
// a, b, siga, sigb, chi2, and either q or sigdat.
int ndata;
double a, b, siga, sigb, chi2, q, sigdat; // Answers.
vector<double> &x, &y, &sig;
// Constructor.
Fitab(vector<double> &xx, vector<double> &yy, vector<double> &ssig)
: ndata(xx.size()), x(xx), y(yy), sig(ssig), chi2(0.), q(1.), sigdat(0.)
{
// Given a set of data points x[0..ndata-1], y[0..ndata-1]
// with individual standard deviations sig[0..ndata-1],
// sets a,b and their respective probable uncertainties
// siga and sigb, the chi-square: chi2, and the goodness-of-fit
// probability: q
Gamma gam;
int i;
double ss=0., sx=0., sy=0., st2=0., t, wt, sxoss; b=0.0;
for (i=0;i < ndata; i++) { // Accumulate sums ...
wt = 1.0 / SQR(sig[i]); //...with weights
ss += wt;
sx += x[i]*wt;
sy += y[i]*wt;
}
sxoss = sx/ss;
for (i=0; i < ndata; i++) {
t = (x[i]-sxoss) / sig[i];
st2 += t*t;
b += t*y[i]/sig[i];
}
b /= st2; // Solve for a, b, sigma-a, and simga-b.
a = (sy-sx*b) / ss;
siga = sqrt((1.0+sx*sx/(ss*st2))/ss);
sigb = sqrt(1.0/st2); // Calculate chi2.
for (i=0;i<ndata;i++) chi2 += SQR((y[i]-a-b*x[i])/sig[i]);
if (ndata>2) q=gam.gammq(0.5*(ndata-2),0.5*chi2); // goodness of fit
}
// Constructor.
Fitab(vector<double> &xx, vector<double> &yy)
: ndata(xx.size()), x(xx), y(yy), sig(xx), chi2(0.), q(1.), sigdat(0.)
{
// As above, but without known errors (sig is not used).
// The uncertainties siga and sigb are estimated by assuming
// equal errors for all points, and that a straight line is
// a good fit. q is returned as 1.0, the normalization of chi2
// is to unit standard deviation on all points, and sigdat
// is set to the estimated error of each point.
int i;
double ss,sx=0.,sy=0.,st2=0.,t,sxoss;
b=0.0; // Accumulate sums ...
for (i=0; i < ndata; i++) {
sx += x[i]; // ...without weights.
sy += y[i];
}
ss = ndata;
sxoss = sx/ss;
for (i=0;i < ndata; i++) {
t = x[i]-sxoss;
st2 += t*t;
b += t*y[i];
}
b /= st2; // Solve for a, b, sigma-a, and sigma-b.
a = (sy-sx*b)/ss;
siga=sqrt((1.0+sx*sx/(ss*st2))/ss);
sigb=sqrt(1.0/st2); // Calculate chi2.
for (i=0;i<ndata;i++) chi2 += SQR(y[i]-a-b*x[i]);
if (ndata > 2) sigdat=sqrt(chi2/(ndata-2));
// For unweighted data evaluate typical
// sig using chi2, and adjust
// the standard deviations.
siga *= sigdat;
sigb *= sigdat;
}
};
where struct Gamma:
struct Gamma : Gauleg18 {
// Object for incomplete gamma function.
// Gauleg18 provides coefficients for Gauss-Legendre quadrature.
static const Int ASWITCH=100; When to switch to quadrature method.
static const double EPS; // See end of struct for initializations.
static const double FPMIN;
double gln;
double gammp(const double a, const double x) {
// Returns the incomplete gamma function P(a,x)
if (x < 0.0 || a <= 0.0) throw("bad args in gammp");
if (x == 0.0) return 0.0;
else if ((Int)a >= ASWITCH) return gammpapprox(a,x,1); // Quadrature.
else if (x < a+1.0) return gser(a,x); // Use the series representation.
else return 1.0-gcf(a,x); // Use the continued fraction representation.
}
double gammq(const double a, const double x) {
// Returns the incomplete gamma function Q(a,x) = 1 - P(a,x)
if (x < 0.0 || a <= 0.0) throw("bad args in gammq");
if (x == 0.0) return 1.0;
else if ((Int)a >= ASWITCH) return gammpapprox(a,x,0); // Quadrature.
else if (x < a+1.0) return 1.0-gser(a,x); // Use the series representation.
else return gcf(a,x); // Use the continued fraction representation.
}
double gser(const Doub a, const Doub x) {
// Returns the incomplete gamma function P(a,x) evaluated by its series representation.
// Also sets ln (gamma) as gln. User should not call directly.
double sum,del,ap;
gln=gammln(a);
ap=a;
del=sum=1.0/a;
for (;;) {
++ap;
del *= x/ap;
sum += del;
if (fabs(del) < fabs(sum)*EPS) {
return sum*exp(-x+a*log(x)-gln);
}
}
}
double gcf(const Doub a, const Doub x) {
// Returns the incomplete gamma function Q(a, x) evaluated
// by its continued fraction representation.
// Also sets ln (gamma) as gln. User should not call directly.
int i;
double an,b,c,d,del,h;
gln=gammln(a);
b=x+1.0-a; // Set up for evaluating continued fraction
// by modified Lentz’s method with with b0 = 0.
c=1.0/FPMIN;
d=1.0/b;
h=d;
for (i=1;;i++) {
// Iterate to convergence.
an = -i*(i-a);
b += 2.0;
d=an*d+b;
if (fabs(d) < FPMIN) d=FPMIN;
c=b+an/c;
if (fabs(c) < FPMIN) c=FPMIN;
d=1.0/d;
del=d*c;
h *= del;
if (fabs(del-1.0) <= EPS) break;
}
return exp(-x+a*log(x)-gln)*h; Put factors in front.
}
double gammpapprox(double a, double x, int psig) {
// Incomplete gamma by quadrature. Returns P(a,x) or Q(a, x),
// when psig is 1 or 0, respectively. User should not call directly.
int j;
double xu,t,sum,ans;
double a1 = a-1.0, lna1 = log(a1), sqrta1 = sqrt(a1);
gln = gammln(a);
// Set how far to integrate into the tail:
if (x > a1) xu = MAX(a1 + 11.5*sqrta1, x + 6.0*sqrta1);
else xu = MAX(0.,MIN(a1 - 7.5*sqrta1, x - 5.0*sqrta1));
sum = 0;
for (j=0;j<ngau;j++) { // Gauss-Legendre.
t = x + (xu-x)*y[j];
sum += w[j]*exp(-(t-a1)+a1*(log(t)-lna1));
}
ans = sum*(xu-x)*exp(a1*(lna1-1.)-gln);
return (psig?(ans>0.0? 1.0-ans:-ans):(ans>=0.0? ans:1.0+ans));
}
double invgammp(Doub p, Doub a);
// Inverse function on x of P(a,x) .
};
const Doub Gamma::EPS = numeric_limits<Doub>::epsilon();
const Doub Gamma::FPMIN = numeric_limits<Doub>::min()/EPS
and stuct Gauleg18:
struct Gauleg18 {
// Abscissas and weights for Gauss-Legendre quadrature.
static const Int ngau = 18;
static const Doub y[18];
static const Doub w[18];
};
const Doub Gauleg18::y[18] = {0.0021695375159141994,
0.011413521097787704,0.027972308950302116,0.051727015600492421,
0.082502225484340941, 0.12007019910960293,0.16415283300752470,
0.21442376986779355, 0.27051082840644336, 0.33199876341447887,
0.39843234186401943, 0.46931971407375483, 0.54413605556657973,
0.62232745288031077, 0.70331500465597174, 0.78649910768313447,
0.87126389619061517, 0.95698180152629142};
const Doub Gauleg18::w[18] = {0.0055657196642445571,
0.012915947284065419,0.020181515297735382,0.027298621498568734,
0.034213810770299537,0.040875750923643261,0.047235083490265582,
0.053244713977759692,0.058860144245324798,0.064039797355015485
0.068745323835736408,0.072941885005653087,0.076598410645870640,
0.079687828912071670,0.082187266704339706,0.084078218979661945,
0.085346685739338721,0.085983275670394821};
and, finally fuinction Gamma::invgamp():
double Gamma::invgammp(double p, double a) {
// Returns x such that P(a,x) = p for an argument p between 0 and 1.
int j;
double x,err,t,u,pp,lna1,afac,a1=a-1;
const double EPS=1.e-8; // Accuracy is the square of EPS.
gln=gammln(a);
if (a <= 0.) throw("a must be pos in invgammap");
if (p >= 1.) return MAX(100.,a + 100.*sqrt(a));
if (p <= 0.) return 0.0;
if (a > 1.) {
lna1=log(a1);
afac = exp(a1*(lna1-1.)-gln);
pp = (p < 0.5)? p : 1. - p;
t = sqrt(-2.*log(pp));
x = (2.30753+t*0.27061)/(1.+t*(0.99229+t*0.04481)) - t;
if (p < 0.5) x = -x;
x = MAX(1.e-3,a*pow(1.-1./(9.*a)-x/(3.*sqrt(a)),3));
} else {
t = 1.0 - a*(0.253+a*0.12); and (6.2.9).
if (p < t) x = pow(p/t,1./a);
else x = 1.-log(1.-(p-t)/(1.-t));
}
for (j=0;j<12;j++) {
if (x <= 0.0) return 0.0; // x too small to compute accurately.
err = gammp(a,x) - p;
if (a > 1.) t = afac*exp(-(x-a1)+a1*(log(x)-lna1));
else t = exp(-x+a1*log(x)-gln);
u = err/t;
// Halley’s method.
x -= (t = u/(1.-0.5*MIN(1.,u*((a-1.)/x - 1))));
// Halve old value if x tries to go negative.
if (x <= 0.) x = 0.5*(x + t);
if (fabs(t) < EPS*x ) break;
}
return x;
}
Here is my version of a C/C++ function that does simple linear regression. The calculations follow the wikipedia article on simple linear regression. This is published as a single-header public-domain (MIT) library on github: simple_linear_regression. The library (.h file) is tested to work on Linux and Windows, and from C and C++ using -Wall -Werror and all -std versions supported by clang/gcc.
#define SIMPLE_LINEAR_REGRESSION_ERROR_INPUT_VALUE -2
#define SIMPLE_LINEAR_REGRESSION_ERROR_NUMERIC -3
int simple_linear_regression(const double * x, const double * y, const int n, double * slope_out, double * intercept_out, double * r2_out) {
double sum_x = 0.0;
double sum_xx = 0.0;
double sum_xy = 0.0;
double sum_y = 0.0;
double sum_yy = 0.0;
double n_real = (double)(n);
int i = 0;
double slope = 0.0;
double denominator = 0.0;
if (x == NULL || y == NULL || n < 2) {
return SIMPLE_LINEAR_REGRESSION_ERROR_INPUT_VALUE;
}
for (i = 0; i < n; ++i) {
sum_x += x[i];
sum_xx += x[i] * x[i];
sum_xy += x[i] * y[i];
sum_y += y[i];
sum_yy += y[i] * y[i];
}
denominator = n_real * sum_xx - sum_x * sum_x;
if (denominator == 0.0) {
return SIMPLE_LINEAR_REGRESSION_ERROR_NUMERIC;
}
slope = (n_real * sum_xy - sum_x * sum_y) / denominator;
if (slope_out != NULL) {
*slope_out = slope;
}
if (intercept_out != NULL) {
*intercept_out = (sum_y - slope * sum_x) / n_real;
}
if (r2_out != NULL) {
denominator = ((n_real * sum_xx) - (sum_x * sum_x)) * ((n_real * sum_yy) - (sum_y * sum_y));
if (denominator == 0.0) {
return SIMPLE_LINEAR_REGRESSION_ERROR_NUMERIC;
}
*r2_out = ((n_real * sum_xy) - (sum_x * sum_y)) * ((n_real * sum_xy) - (sum_x * sum_y)) / denominator;
}
return 0;
}
Usage example:
#define SIMPLE_LINEAR_REGRESSION_IMPLEMENTATION
#include "simple_linear_regression.h"
#include <stdio.h>
/* Some data that we want to find the slope, intercept and r2 for */
static const double x[] = { 1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83 };
static const double y[] = { 52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46 };
int main() {
double slope = 0.0;
double intercept = 0.0;
double r2 = 0.0;
int res = 0;
res = simple_linear_regression(x, y, sizeof(x) / sizeof(x[0]), &slope, &intercept, &r2);
if (res < 0) {
printf("Error: %s\n", simple_linear_regression_error_string(res));
return res;
}
printf("slope: %f\n", slope);
printf("intercept: %f\n", intercept);
printf("r2: %f\n", r2);
return 0;
}
The original example above worked well for me with slope and offset but I had a hard time with the corr coef. Maybe I don't have my parenthesis working the same as the assumed precedence? Anyway, with some help of other web pages I finally got values that match the linear trend-line in Excel. Thought I would share my code using Mark Lakata's variable names. Hope this helps.
double slope = ((n * sumxy) - (sumx * sumy )) / denom;
double intercept = ((sumy * sumx2) - (sumx * sumxy)) / denom;
double term1 = ((n * sumxy) - (sumx * sumy));
double term2 = ((n * sumx2) - (sumx * sumx));
double term3 = ((n * sumy2) - (sumy * sumy));
double term23 = (term2 * term3);
double r2 = 1.0;
if (fabs(term23) > MIN_DOUBLE) // Define MIN_DOUBLE somewhere as 1e-9 or similar
r2 = (term1 * term1) / term23;
as an assignment I had to code in C a simple linear regression using RMSE loss function. The program is dynamic and you can enter your own values and choose your own loss function which is for now limited to Root Mean Square Error. But first here are the algorithms I used:
now the code... you need gnuplot to display the chart, sudo apt install gnuplot
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#define BUFFSIZE 64
#define MAXSIZE 100
static double vector_x[MAXSIZE] = {0};
static double vector_y[MAXSIZE] = {0};
static double vector_predict[MAXSIZE] = {0};
static double max_x;
static double max_y;
static double mean_x;
static double mean_y;
static double teta_0_intercept;
static double teta_1_grad;
static double RMSE;
static double r_square;
static double prediction;
static char intercept[BUFFSIZE];
static char grad[BUFFSIZE];
static char xrange[BUFFSIZE];
static char yrange[BUFFSIZE];
static char lossname_RMSE[BUFFSIZE] = "Simple Linear Regression using RMSE'";
static char cmd_gnu_0[BUFFSIZE] = "set title '";
static char cmd_gnu_1[BUFFSIZE] = "intercept = ";
static char cmd_gnu_2[BUFFSIZE] = "grad = ";
static char cmd_gnu_3[BUFFSIZE] = "set xrange [0:";
static char cmd_gnu_4[BUFFSIZE] = "set yrange [0:";
static char cmd_gnu_5[BUFFSIZE] = "f(x) = (grad * x) + intercept";
static char cmd_gnu_6[BUFFSIZE] = "plot f(x), 'data.temp' with points pointtype 7";
static char const *commands_gnuplot[] = {
cmd_gnu_0,
cmd_gnu_1,
cmd_gnu_2,
cmd_gnu_3,
cmd_gnu_4,
cmd_gnu_5,
cmd_gnu_6,
};
static size_t size;
static void user_input()
{
printf("Enter x,y vector size, MAX = 100\n");
scanf("%lu", &size);
if (size > MAXSIZE) {
printf("Wrong input size is too big\n");
user_input();
}
printf("vector's size is %lu\n", size);
size_t i;
for (i = 0; i < size; i++) {
printf("Enter vector_x[%ld] values\n", i);
scanf("%lf", &vector_x[i]);
}
for (i = 0; i < size; i++) {
printf("Enter vector_y[%ld] values\n", i);
scanf("%lf", &vector_y[i]);
}
}
static void display_vector()
{
size_t i;
for (i = 0; i < size; i++){
printf("vector_x[%lu] = %lf\t", i, vector_x[i]);
printf("vector_y[%lu] = %lf\n", i, vector_y[i]);
}
}
static void concatenate(char p[], char q[]) {
int c;
int d;
c = 0;
while (p[c] != '\0') {
c++;
}
d = 0;
while (q[d] != '\0') {
p[c] = q[d];
d++;
c++;
}
p[c] = '\0';
}
static void compute_mean_x_y()
{
size_t i;
double tmp_x = 0.0;
double tmp_y = 0.0;
for (i = 0; i < size; i++) {
tmp_x += vector_x[i];
tmp_y += vector_y[i];
}
mean_x = tmp_x / size;
mean_y = tmp_y / size;
printf("mean_x = %lf\n", mean_x);
printf("mean_y = %lf\n", mean_y);
}
static void compute_teta_1_grad()
{
double numerator = 0.0;
double denominator = 0.0;
double tmp1 = 0.0;
double tmp2 = 0.0;
size_t i;
for (i = 0; i < size; i++) {
numerator += (vector_x[i] - mean_x) * (vector_y[i] - mean_y);
}
for (i = 0; i < size; i++) {
tmp1 = vector_x[i] - mean_x;
tmp2 = tmp1 * tmp1;
denominator += tmp2;
}
teta_1_grad = numerator / denominator;
printf("teta_1_grad = %lf\n", teta_1_grad);
}
static void compute_teta_0_intercept()
{
teta_0_intercept = mean_y - (teta_1_grad * mean_x);
printf("teta_0_intercept = %lf\n", teta_0_intercept);
}
static void compute_prediction()
{
size_t i;
for (i = 0; i < size; i++) {
vector_predict[i] = teta_0_intercept + (teta_1_grad * vector_x[i]);
printf("y^[%ld] = %lf\n", i, vector_predict[i]);
}
printf("\n");
}
static void compute_RMSE()
{
compute_prediction();
double error = 0;
size_t i;
for (i = 0; i < size; i++) {
error = (vector_predict[i] - vector_y[i]) * (vector_predict[i] - vector_y[i]);
printf("error y^[%ld] = %lf\n", i, error);
RMSE += error;
}
/* mean */
RMSE = RMSE / size;
/* square root mean */
RMSE = sqrt(RMSE);
printf("\nRMSE = %lf\n", RMSE);
}
static void compute_loss_function()
{
int input = 0;
printf("Which loss function do you want to use?\n");
printf(" 1 - RMSE\n");
scanf("%d", &input);
switch(input) {
case 1:
concatenate(cmd_gnu_0, lossname_RMSE);
compute_RMSE();
printf("\n");
break;
default:
printf("Wrong input try again\n");
compute_loss_function(size);
}
}
static void compute_r_square(size_t size)
{
double num_err = 0.0;
double den_err = 0.0;
size_t i;
for (i = 0; i < size; i++) {
num_err += (vector_y[i] - vector_predict[i]) * (vector_y[i] - vector_predict[i]);
den_err += (vector_y[i] - mean_y) * (vector_y[i] - mean_y);
}
r_square = 1 - (num_err/den_err);
printf("R_square = %lf\n", r_square);
}
static void compute_predict_for_x()
{
double x = 0.0;
printf("Please enter x value\n");
scanf("%lf", &x);
prediction = teta_0_intercept + (teta_1_grad * x);
printf("y^ if x = %lf -> %lf\n",x, prediction);
}
static void compute_max_x_y()
{
size_t i;
double tmp1= 0.0;
double tmp2= 0.0;
for (i = 0; i < size; i++) {
if (vector_x[i] > tmp1) {
tmp1 = vector_x[i];
max_x = vector_x[i];
}
if (vector_y[i] > tmp2) {
tmp2 = vector_y[i];
max_y = vector_y[i];
}
}
printf("vector_x max value %lf\n", max_x);
printf("vector_y max value %lf\n", max_y);
}
static void display_model_line()
{
sprintf(intercept, "%0.7lf", teta_0_intercept);
sprintf(grad, "%0.7lf", teta_1_grad);
sprintf(xrange, "%0.7lf", max_x + 1);
sprintf(yrange, "%0.7lf", max_y + 1);
concatenate(cmd_gnu_1, intercept);
concatenate(cmd_gnu_2, grad);
concatenate(cmd_gnu_3, xrange);
concatenate(cmd_gnu_3, "]");
concatenate(cmd_gnu_4, yrange);
concatenate(cmd_gnu_4, "]");
printf("grad = %s\n", grad);
printf("intercept = %s\n", intercept);
printf("xrange = %s\n", xrange);
printf("yrange = %s\n", yrange);
printf("cmd_gnu_0: %s\n", cmd_gnu_0);
printf("cmd_gnu_1: %s\n", cmd_gnu_1);
printf("cmd_gnu_2: %s\n", cmd_gnu_2);
printf("cmd_gnu_3: %s\n", cmd_gnu_3);
printf("cmd_gnu_4: %s\n", cmd_gnu_4);
printf("cmd_gnu_5: %s\n", cmd_gnu_5);
printf("cmd_gnu_6: %s\n", cmd_gnu_6);
/* print plot */
FILE *gnuplot_pipe = (FILE*)popen("gnuplot -persistent", "w");
FILE *temp = (FILE*)fopen("data.temp", "w");
/* create data.temp */
size_t i;
for (i = 0; i < size; i++)
{
fprintf(temp, "%f %f \n", vector_x[i], vector_y[i]);
}
/* display gnuplot */
for (i = 0; i < 7; i++)
{
fprintf(gnuplot_pipe, "%s \n", commands_gnuplot[i]);
}
}
int main(void)
{
printf("===========================================\n");
printf("INPUT DATA\n");
printf("===========================================\n");
user_input();
display_vector();
printf("\n");
printf("===========================================\n");
printf("COMPUTE MEAN X:Y, TETA_1 TETA_0\n");
printf("===========================================\n");
compute_mean_x_y();
compute_max_x_y();
compute_teta_1_grad();
compute_teta_0_intercept();
printf("\n");
printf("===========================================\n");
printf("COMPUTE LOSS FUNCTION\n");
printf("===========================================\n");
compute_loss_function();
printf("===========================================\n");
printf("COMPUTE R_square\n");
printf("===========================================\n");
compute_r_square(size);
printf("\n");
printf("===========================================\n");
printf("COMPUTE y^ according to x\n");
printf("===========================================\n");
compute_predict_for_x();
printf("\n");
printf("===========================================\n");
printf("DISPLAY LINEAR REGRESSION\n");
printf("===========================================\n");
display_model_line();
printf("\n");
return 0;
}
Look at Section 1 of this paper. This section expresses a 2D linear regression as a matrix multiplication exercise. As long as your data is well-behaved, this technique should permit you to develop a quick least squares fit.
Depending on the size of your data, it might be worthwhile to algebraically reduce the matrix multiplication to simple set of equations, thereby avoiding the need to write a matmult() function. (Be forewarned, this is completely impractical for more than 4 or 5 data points!)
The fastest, most efficient way to solve least squares, as far as I am aware, is to subtract (the gradient)/(the 2nd order gradient) from your parameter vector. (2nd order gradient = i.e. the diagonal of the Hessian.)
Here is the intuition:
Let's say you want to optimize least squares over a single parameter. This is equivalent to finding the vertex of a parabola. Then, for any random initial parameter, x0, the vertex of the loss function is located at x0 - f(1) / f(2). That's because adding - f(1) / f(2) to x will always zero out the derivative, f(1).
Side note: Implementing this in Tensorflow, the solution appeared at w0 - f(1) / f(2) / (number of weights), but I'm not sure if that's due to Tensorflow or if it's due to something else..

Resources