-added mex files for 1.2, 1.3 and 2.1

This commit is contained in:
Patrice Matz 2018-01-03 18:49:09 +01:00
parent 2caaebac69
commit daef6be487
6 changed files with 146 additions and 0 deletions

35
Mex/a11.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[], /* Output variables */
int nrhs, const mxArray *prhs[]) /* Input variables */
{
if (nrhs != 3 || ! mxIsNumeric (prhs[0]))
mexErrMsgTxt ("ARG1 must be a 3 dimensional matrix");
if( mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[1]) ||
mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[2]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
double *heights, *d, *densities;
heights = mxGetPr (prhs[0]);
d = mxGetPr (prhs[1]);
densities = mxGetPr (prhs[2]);
double mass = 0;
for (int i = mxGetNumberOfElements(prhs[0]); i--;)
{
mass = mass + ((heights[i] / 10) * (d[i] / 20)*(d[i] / 20) * 3.14159265359 * densities[i]);
}
//return value
plhs[0] = mxCreateDoubleScalar(mass);
return;
}

3
Mex/a11.m Normal file
View File

@ -0,0 +1,3 @@
%function accepts 3 double arrays (heights, radii, densities)
%passed arrays have to be of same size
%function returns a double value

34
Mex/a12.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[], /* Output variables */
int nrhs, const mxArray *prhs[]) /* Input variables */
{
if (nrhs != 4 || ! mxIsNumeric (prhs[0]))
mexErrMsgTxt ("ARG1 must be a 4 dimensional matrix");
if( mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[1]) ||
mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[2]) ||
mxGetNumberOfElements(prhs[0]) != mxGetNumberOfElements(prhs[3]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
double *heights, *d, *densities, *resistances;
heights = mxGetPr (prhs[0]);
d = mxGetPr (prhs[1]);
densities = mxGetPr (prhs[2]);
resistances = mxGetPr(prhs[3]);
double resistance = 0;
for (int i = mxGetNumberOfElements(prhs[0]); i--;)
{
resistance = resistance + ((heights[i] / 1000) * resistances[i]) / ((d[i] / 20)*(d[i] / 20) * 3.14159265359);
}
//return value
plhs[0] = mxCreateDoubleScalar(resistance);
return;
}

3
Mex/a12.m Normal file
View File

@ -0,0 +1,3 @@
%function accepts 4 double arrays (heights, radii, densities, resistances)
%passed arrays have to be of same size
%function returns a double value

62
Mex/a21.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
int variables = *mxGetPr(prhs[0]);
double* x = (double *)mxCalloc(variables+1, sizeof(double)); //create solution array
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA)
for (int i = 0; i <= variables+1; i++){
GA[i] = (double *)mxCalloc(variables+1, sizeof(double));
}
for(int i = 0; i < variables*(variables+1); i++){ //copy input array into GA
GA[i%variables][i/variables] = mxGetPr(prhs[1])[i];
}
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
}
for(int j=0; j < variables; j++){
plhs[j]=mxCreateDoubleScalar(x[j]);
}
for (int i = 0; i <= variables; i++ ){
free(GA[i]);
}
free(GA);
free(x);
return;
}

9
Mex/a21.m Normal file
View File

@ -0,0 +1,9 @@
%function accepts 1 double and 1 double matrix (#of_variables, [row1;row2])
%each row1 contains #of_variables+1 elements
%returns solution vector
%bsp.:
% 6x+12y=30
% 3x+ 3y= 9
% [a1,a2]=a21(2,[6,12,30;3,3,9])
% a1 = 1
% a2 = 2