- added 5.4.2 (optim.) and started 4.1.2 (lin. interpol.)

This commit is contained in:
Patrice Matz 2018-01-16 22:24:13 +01:00
parent 754667facf
commit 7bbe3e29f0
4 changed files with 296 additions and 0 deletions

109
Mex/a411.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
#include "math.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, sizeof(double)); //create solution array
double* xx = (double *)mxCalloc(variables, sizeof(double));
double* yy = (double *)mxCalloc(variables, sizeof(double));
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA)
for (int i = 0; i <= variables; i++){
GA[i] = (double *)mxCalloc(variables+1, sizeof(double));
}
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];
}
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;
for(int j=0; j < variables; j++){
plhs[j]=mxCreateDoubleScalar(x[j]);
}
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");
for (int i = 0; i <= variables; i++ ){
free(GA[i]);
}
free(GA);
free(x);
return;
}

18
Mex/a411.m Normal file
View File

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

109
Mex/a412.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
#include "math.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, sizeof(double)); //create solution array
double* xx = (double *)mxCalloc(variables, sizeof(double));
double* yy = (double *)mxCalloc(variables, sizeof(double));
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA)
for (int i = 0; i <= variables; i++){
GA[i] = (double *)mxCalloc(variables+1, sizeof(double));
}
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];
}
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;
for(int j=0; j < variables; j++){
plhs[j]=mxCreateDoubleScalar(x[j]);
}
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");
for (int i = 0; i <= variables; i++ ){
free(GA[i]);
}
free(GA);
free(x);
return;
}

60
Mex/a542.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
#include "math.h"
double g_von_theta(long double x, long double y)
{
return -(x*x + y*y);
}
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
double x = -2, y = -2,
xnew, ynew,
increments = 0.1,
z = g_von_theta(x, y),
znew = 0, zold = 1,
zinitial = z;
int xTOy = 1, iAThalf = 1000; //iAThalf: max iterations/2
int i;
for( i = 0 ; i <= 2 * iAThalf + 1; ++i) // add. exit crit. needed
{
if (xTOy == 1)
{
xnew = x;
ynew = y;
if (zinitial / 2 == z) //appropriate criteria?
{
iAThalf = i;
ynew = y + increments;
}
else
xnew = x + increments;
}
else
{
xnew = x;
ynew = y + increments;
}
znew = g_von_theta(xnew, ynew);
zold = z;
if(znew > z)
{
z = znew;
x = xnew;
y = ynew;
}
else
if (xTOy == 1)
xTOy = 2;
}
// Output
plhs[0] = mxCreateDoubleScalar(x);
plhs[1] = mxCreateDoubleScalar(y);
plhs[2] = mxCreateDoubleScalar(z);
plhs[3] = mxCreateDoubleScalar(i);
return;
}