- 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,103 +7,94 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
int variables = *mxGetPr(prhs[0]);
double inc = *mxGetPr(prhs[1]);
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* x_in = (double *)mxCalloc(variables, sizeof(double)); //input x
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++){
GA[i] = (double *)mxCalloc(variables+1, sizeof(double));
}
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++){
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++){
GA[i][j] = pow(xx[i],j);
GA[i][j] = pow(x_in[i],j);
}
}
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;
for (i = 0; i<variables; i++) //Pivotisation
for (k = i + 1; k<variables; k++)
double temp=x_in[0];
for(int i=0;temp <= x_in[variables-1]+inc;i++){ //generate return array with x values
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]))
for (j = 0; j <= variables; j++)
for (int 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++)
for (int i = 0; i<variables - 1; i++) //gauss elimination
for (int k = i + 1; k<variables; k++)
{
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
}
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[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
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);
}
//polyval: filling yy with the interpolated values
for (int i = 0; i < len; i++){
double tmp = 0;
for (int j = 0; j < variables; j++){
tmp += pow(xx[i], j) * x[j];
}
}
mexPrintf("\n");
for (int i = 0; i <= variables; i++ ){
free(GA[i]);
yy[i]=tmp;
}
free(GA);
free(x);
//return values
nlhs = 2;
plhs[0] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[0]), xx, len*sizeof(double));
plhs[1] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[1]), yy, len*sizeof(double));
//free memory
// causes errors
return;
}

View File

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

View File

@ -2,29 +2,41 @@
#include "matrix.h"
#include "stdlib.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
int nrhs, const mxArray *prhs[]) // Input variables
{
double x = -2, y = -2,
xnew, ynew,
increments = 0.1,
z = g_von_theta(x, y),
char *func_Name;
func_Name = mxArrayToString(prhs[0]);
//x = -2, y = -2
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,
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
for(int i = 0 ; i <= 2 * iAThalf + 1; ++i) // add. exit crit. needed
{
if (xTOy == 1)
{
xnew = x;
ynew = y;
if (zinitial / 2 == z) //appropriate criteria?
if (zinitial / 2 == z)
{
iAThalf = i;
ynew = y + increments;
@ -38,7 +50,7 @@ int i;
ynew = y + increments;
}
znew = g_von_theta(xnew, ynew);
znew = g_von_theta(xnew, ynew, func_Name);
zold = z;
if(znew > z)
{
@ -55,6 +67,6 @@ int i;
plhs[0] = mxCreateDoubleScalar(x);
plhs[1] = mxCreateDoubleScalar(y);
plhs[2] = mxCreateDoubleScalar(z);
plhs[3] = mxCreateDoubleScalar(i);
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