2018-01-14 21:34:30 +00:00
|
|
|
#include "mex.h"
|
|
|
|
|
#include "matrix.h"
|
|
|
|
|
#include "stdlib.h"
|
2018-01-15 19:24:56 +00:00
|
|
|
#include "math.h"
|
2018-01-14 21:34:30 +00:00
|
|
|
|
|
|
|
|
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
|
|
|
|
|
int nrhs, const mxArray *prhs[]) // Input variables
|
|
|
|
|
{
|
|
|
|
|
int variables = *mxGetPr(prhs[0]);
|
|
|
|
|
|
2018-01-15 19:24:56 +00:00
|
|
|
double* x = (double *)mxCalloc(variables, sizeof(double)); //create solution array
|
|
|
|
|
|
|
|
|
|
double* xx = (double *)mxCalloc(variables, sizeof(double));
|
|
|
|
|
double* yy = (double *)mxCalloc(variables, sizeof(double));
|
2018-01-14 21:34:30 +00:00
|
|
|
|
|
|
|
|
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA)
|
2018-01-15 19:24:56 +00:00
|
|
|
for (int i = 0; i <= variables; i++){
|
2018-01-14 21:34:30 +00:00
|
|
|
GA[i] = (double *)mxCalloc(variables+1, sizeof(double));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-15 19:24:56 +00:00
|
|
|
for(int i=0;i<variables;i++){
|
|
|
|
|
xx[i]=mxGetPr(prhs[1])[i];
|
|
|
|
|
}
|
|
|
|
|
for(int i=0;i<variables;i++){
|
|
|
|
|
yy[i]=mxGetPr(prhs[2])[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<variables;i++){
|
|
|
|
|
for(int j=0; j<variables;j++){
|
|
|
|
|
GA[i][j] = pow(xx[i],j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i=0;i<variables;i++){
|
|
|
|
|
GA[i][variables] = yy[i];
|
2018-01-14 21:34:30 +00:00
|
|
|
}
|
2018-01-15 19:24:56 +00:00
|
|
|
|
2018-01-14 21:34:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
int i, j, k;
|
|
|
|
|
for (i = 0; i<variables; i++) //Pivotisation
|
|
|
|
|
for (k = i + 1; k<variables; k++)
|
|
|
|
|
if (abs(GA[i][i])<abs(GA[k][i]))
|
|
|
|
|
for (j = 0; j <= variables; j++)
|
|
|
|
|
{
|
|
|
|
|
long double temp = GA[i][j];
|
|
|
|
|
GA[i][j] = GA[k][j];
|
|
|
|
|
GA[k][j] = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i<variables - 1; i++) //gauss elimination
|
|
|
|
|
for (k = i + 1; k<variables; k++)
|
|
|
|
|
{
|
|
|
|
|
long double t = GA[k][i] / GA[i][i];
|
|
|
|
|
for (j = 0; j <= variables; j++)
|
|
|
|
|
GA[k][j] = GA[k][j] - t*GA[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = variables - 1; i >= 0; i--) //back-substitution
|
|
|
|
|
{ //x is an array whose values correspond to the values of x,y,z..
|
|
|
|
|
x[i] = GA[i][variables]; //make the variable to be calculated equal to the rhs of the last equation
|
|
|
|
|
for (j = i + 1; j<variables; j++)
|
|
|
|
|
if (j != i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated
|
|
|
|
|
x[i] = x[i] - GA[i][j] * x[j];
|
|
|
|
|
x[i] = x[i] / GA[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nlhs = variables;
|
2018-01-15 19:24:56 +00:00
|
|
|
for(int j=0; j < variables; j++){
|
|
|
|
|
plhs[j]=mxCreateDoubleScalar(x[j]);
|
|
|
|
|
|
|
|
|
|
}
|
2018-01-14 21:34:30 +00:00
|
|
|
for(int l=0;l<variables;l++){
|
|
|
|
|
char temp;
|
|
|
|
|
|
|
|
|
|
if(x[variables-1-l] != 0)
|
|
|
|
|
{
|
|
|
|
|
if(l!=0){
|
|
|
|
|
mexPrintf("+");
|
|
|
|
|
}
|
|
|
|
|
if(x[variables-1-l] < 0 ){
|
|
|
|
|
mexPrintf("-");
|
|
|
|
|
x[variables-l] *= -1;
|
|
|
|
|
}
|
|
|
|
|
temp=(char)(48+x[variables-1-l]);
|
|
|
|
|
mexPrintf("%c",temp);
|
|
|
|
|
|
|
|
|
|
if(variables-1-l != 0){
|
|
|
|
|
mexPrintf("*");
|
|
|
|
|
mexPrintf("x");
|
|
|
|
|
mexPrintf("^");
|
|
|
|
|
temp=(char)(48+variables-1-l);
|
|
|
|
|
mexPrintf("%c",temp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
mexPrintf("\n");
|
2018-01-15 19:24:56 +00:00
|
|
|
|
2018-01-14 21:34:30 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i <= variables; i++ ){
|
|
|
|
|
free(GA[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(GA);
|
|
|
|
|
free(x);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|