From daef6be4878828669dcf3ed0e726038385a4ccdf Mon Sep 17 00:00:00 2001 From: Patrice Matz Date: Wed, 3 Jan 2018 18:49:09 +0100 Subject: [PATCH] -added mex files for 1.2, 1.3 and 2.1 --- Mex/a11.cpp | 35 ++++++++++++++++++++++++++++++ Mex/a11.m | 3 +++ Mex/a12.cpp | 34 +++++++++++++++++++++++++++++ Mex/a12.m | 3 +++ Mex/a21.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Mex/a21.m | 9 ++++++++ 6 files changed, 146 insertions(+) create mode 100644 Mex/a11.cpp create mode 100644 Mex/a11.m create mode 100644 Mex/a12.cpp create mode 100644 Mex/a12.m create mode 100644 Mex/a21.cpp create mode 100644 Mex/a21.m diff --git a/Mex/a11.cpp b/Mex/a11.cpp new file mode 100644 index 0000000..df54969 --- /dev/null +++ b/Mex/a11.cpp @@ -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; +} \ No newline at end of file diff --git a/Mex/a11.m b/Mex/a11.m new file mode 100644 index 0000000..5acb420 --- /dev/null +++ b/Mex/a11.m @@ -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 \ No newline at end of file diff --git a/Mex/a12.cpp b/Mex/a12.cpp new file mode 100644 index 0000000..04bc0d9 --- /dev/null +++ b/Mex/a12.cpp @@ -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; +} \ No newline at end of file diff --git a/Mex/a12.m b/Mex/a12.m new file mode 100644 index 0000000..1e3321d --- /dev/null +++ b/Mex/a12.m @@ -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 \ No newline at end of file diff --git a/Mex/a21.cpp b/Mex/a21.cpp new file mode 100644 index 0000000..905870c --- /dev/null +++ b/Mex/a21.cpp @@ -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= 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