- fixed a4.1.1, a4.1.2 and a542 now handels inputs

This commit is contained in:
Patrice Matz 2018-01-18 15:22:20 +01:00
parent 7bbe3e29f0
commit 9a2d6028b4
4 changed files with 135 additions and 155 deletions

View File

@ -7,11 +7,12 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables int nrhs, const mxArray *prhs[]) // Input variables
{ {
int variables = *mxGetPr(prhs[0]); int variables = *mxGetPr(prhs[0]);
double inc = *mxGetPr(prhs[1]);
double* x = (double *)mxCalloc(variables, sizeof(double)); //create solution array double* x = (double *)mxCalloc(variables, sizeof(double)); //create solution array
double* xx = (double *)mxCalloc(variables, sizeof(double)); double* x_in = (double *)mxCalloc(variables, sizeof(double)); //input x
double* yy = (double *)mxCalloc(variables, sizeof(double)); double* y = (double *)mxCalloc(variables, sizeof(double)); //input y
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA) double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA)
for (int i = 0; i <= variables; i++){ for (int i = 0; i <= variables; i++){
@ -19,91 +20,81 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
} }
for(int i=0;i<variables;i++){ for(int i=0;i<variables;i++){
xx[i]=mxGetPr(prhs[1])[i]; x_in[i]=mxGetPr(prhs[2])[i];
} }
for(int i=0;i<variables;i++){ for(int i=0;i<variables;i++){
yy[i]=mxGetPr(prhs[2])[i]; y[i]=mxGetPr(prhs[3])[i];
} }
for(int i=0;i<variables;i++){ for(int i=0;i<variables;i++){ //fill GA with the polynoms
for(int j=0; j<variables;j++){ for(int j=0; j<variables;j++){
GA[i][j] = pow(xx[i],j); GA[i][j] = pow(x_in[i],j);
} }
} }
for(int i=0;i<variables;i++){ for(int i=0;i<variables;i++){
GA[i][variables] = yy[i]; GA[i][variables] = y[i];
} }
double len = (x_in[variables-1]-x_in[0])/inc +1;
double* xx = (double *)mxCalloc(len, sizeof(double)); //return array with x values
double* yy = (double *)mxCalloc(len, sizeof(double)); //return array with interpolated y values
int i, j, k; double temp=x_in[0];
for (i = 0; i<variables; i++) //Pivotisation for(int i=0;temp <= x_in[variables-1]+inc;i++){ //generate return array with x values
for (k = i + 1; k<variables; k++) xx[i]=temp;
temp += inc;
}
// Gauß
for (int i = 0; i<variables; i++) //Pivotisation
for (int k = i + 1; k<variables; k++)
if (abs(GA[i][i])<abs(GA[k][i])) if (abs(GA[i][i])<abs(GA[k][i]))
for (j = 0; j <= variables; j++) for (int j = 0; j <= variables; j++)
{ {
long double temp = GA[i][j]; long double temp = GA[i][j];
GA[i][j] = GA[k][j]; GA[i][j] = GA[k][j];
GA[k][j] = temp; GA[k][j] = temp;
} }
for (i = 0; i<variables - 1; i++) //gauss elimination for (int i = 0; i<variables - 1; i++) //gauss elimination
for (k = i + 1; k<variables; k++) for (int k = i + 1; k<variables; k++)
{ {
long double t = GA[k][i] / GA[i][i]; long double t = GA[k][i] / GA[i][i];
for (j = 0; j <= variables; j++) for (int 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 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 for (int i = variables - 1; i >= 0; i--) //back-substitution
{ //x is an array whose values correspond to the values of x,y,z.. { //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 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++) for (int 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 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][j] * x[j];
x[i] = x[i] / GA[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated x[i] = x[i] / GA[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
} }
nlhs = variables; //polyval: filling yy with the interpolated values
for(int j=0; j < variables; j++){
plhs[j]=mxCreateDoubleScalar(x[j]);
for (int i = 0; i < len; i++){
double tmp = 0;
for (int j = 0; j < variables; j++){
tmp += pow(xx[i], j) * x[j];
} }
for(int l=0;l<variables;l++){ yy[i]=tmp;
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);
} }
} //return values
nlhs = 2;
} plhs[0] = mxCreateDoubleMatrix(1, len, mxREAL);
mexPrintf("\n"); memcpy(mxGetPr(plhs[0]), xx, len*sizeof(double));
plhs[1] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[1]), yy, len*sizeof(double));
for (int i = 0; i <= variables; i++ ){ //free memory
free(GA[i]);
}
free(GA);
free(x);
// causes errors
return; return;
} }

View File

@ -7,103 +7,69 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables int nrhs, const mxArray *prhs[]) // Input variables
{ {
int variables = *mxGetPr(prhs[0]); int variables = *mxGetPr(prhs[0]);
variables++;
double inc = *mxGetPr(prhs[1]);
double* x = (double *)mxCalloc(variables, sizeof(double)); //create solution array
double* xx = (double *)mxCalloc(variables, sizeof(double)); double* x = (double *)mxCalloc(variables, sizeof(double));
double* yy = (double *)mxCalloc(variables, sizeof(double)); double* y = (double *)mxCalloc(variables, sizeof(double));
double** GA = (double **)mxCalloc(variables+1, sizeof(double*)); //create Gauss Array (GA) for(int i=0;i<variables-1;i++){
for (int i = 0; i <= variables; i++){ x[i]=mxGetPr(prhs[2])[i];
GA[i] = (double *)mxCalloc(variables+1, sizeof(double)); }
x[variables-1]=x[variables-2];
x[variables-1]+=inc;
for(int i=0;i<variables-1;i++){
y[i]=mxGetPr(prhs[3])[i];
} }
for(int i=0;i<variables;i++){ y[variables-1]=y[variables-2];
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++){ double len = (x[variables-1]-x[0])/inc +1;
GA[i][j] = pow(xx[i],j);
} double* xx = (double *)mxCalloc(len, sizeof(double));
} double* yy = (double *)mxCalloc(len, sizeof(double));
for(int i=0;i<variables;i++){
GA[i][variables] = yy[i]; double temp=x[0];
for(int i=0;temp <= x[variables-1]+inc;i++){
xx[i]=temp;
temp += inc;
} }
//Code von Marvin Lehmann start
unsigned int savepoint = 0;
int i, j, k; // Zwischen jeweils 2 Punkten
for (i = 0; i<variables; i++) //Pivotisation for (unsigned int i = 0; i < variables-1; i++)
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]; // Für alle Zwischenwerte (xx) y interpolieren (yy)
GA[i][j] = GA[k][j]; for (unsigned int j = savepoint; j < len; 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]; if (xx[j] >= x[i] && xx[j] <= x[i + 1])
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){ // Interpolationsformel
mexPrintf("+"); yy[j] = ((y[i + 1] - y[i]) / (x[i + 1] - x[i])) * (xx[j] - x[i]) + y[i];
} }
if(x[variables-1-l] < 0 ){ else if (xx[j] > x[i + 1])
mexPrintf("-"); {
x[variables-l] *= -1; savepoint = j;
break;
} }
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);
} }
} }
//Code von Marvin Lehmann ende
} nlhs = 2;
mexPrintf("\n");
plhs[0] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[0]), xx, len*sizeof(double));
for (int i = 0; i <= variables; i++ ){ plhs[1] = mxCreateDoubleMatrix(1, len, mxREAL);
free(GA[i]); memcpy(mxGetPr(plhs[1]), yy, len*sizeof(double));
}
free(GA);
free(x);
return; return;
} }

View File

@ -2,29 +2,41 @@
#include "matrix.h" #include "matrix.h"
#include "stdlib.h" #include "stdlib.h"
#include "math.h" #include "math.h"
double g_von_theta(long double x, long double y) double g_von_theta(long double x, long double y, char * func_Name)
{ {
return -(x*x + y*y); //-(x*x + y*y);
mxArray *func_Inputs[2];
mxArray *func_Outputs[1];
func_Inputs[0] = mxCreateDoubleScalar(x);
func_Inputs[1] = mxCreateDoubleScalar(y);
mexCallMATLAB(1, func_Outputs , 2 , func_Inputs , func_Name);
return *mxGetPr(func_Outputs[0]);
} }
void mexFunction(int nlhs, mxArray *plhs[], // Output variables void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables int nrhs, const mxArray *prhs[]) // Input variables
{ {
double x = -2, y = -2, char *func_Name;
xnew, ynew, func_Name = mxArrayToString(prhs[0]);
increments = 0.1, //x = -2, y = -2
z = g_von_theta(x, y), double x = *mxGetPr(prhs[1]);
double y = *mxGetPr(prhs[2]);
double increments = *mxGetPr(prhs[3]);
double xnew, ynew,
z = g_von_theta(x, y, func_Name),
znew = 0, zold = 1, znew = 0, zold = 1,
zinitial = z; zinitial = z;
int xTOy = 1, iAThalf = 1000; //iAThalf: max iterations/2 int xTOy = 1, iAThalf = 1000; //iAThalf: max iterations/2
int i;
for( i = 0 ; i <= 2 * iAThalf + 1; ++i) // add. exit crit. needed for(int i = 0 ; i <= 2 * iAThalf + 1; ++i) // add. exit crit. needed
{ {
if (xTOy == 1) if (xTOy == 1)
{ {
xnew = x; xnew = x;
ynew = y; ynew = y;
if (zinitial / 2 == z) //appropriate criteria? if (zinitial / 2 == z)
{ {
iAThalf = i; iAThalf = i;
ynew = y + increments; ynew = y + increments;
@ -38,7 +50,7 @@ int i;
ynew = y + increments; ynew = y + increments;
} }
znew = g_von_theta(xnew, ynew); znew = g_von_theta(xnew, ynew, func_Name);
zold = z; zold = z;
if(znew > z) if(znew > z)
{ {
@ -55,6 +67,6 @@ int i;
plhs[0] = mxCreateDoubleScalar(x); plhs[0] = mxCreateDoubleScalar(x);
plhs[1] = mxCreateDoubleScalar(y); plhs[1] = mxCreateDoubleScalar(y);
plhs[2] = mxCreateDoubleScalar(z); plhs[2] = mxCreateDoubleScalar(z);
plhs[3] = mxCreateDoubleScalar(i);
return; return;
} }

11
Mex/a542.m Normal file
View File

@ -0,0 +1,11 @@
%function accepts 1 string, 2 double and increments
% a542("func_name", x, y, inc)
% function returns lowest found return of passed function
%
%exp.:
% function z=func(x,y)
% z=-(x*x + y*y);
% end
%
% a542("func",-2,-2, 0.1)
% ans=6.3838e-016