This commit is contained in:
Patrice Matz 2018-01-20 12:01:52 +01:00
parent 9254b68273
commit 116d85d22d
24 changed files with 271 additions and 96 deletions

BIN
AP APL Matz 2018.zip Normal file

Binary file not shown.

90
Mex/Matz.m Normal file
View File

@ -0,0 +1,90 @@
% outout file Name input
% double a11 mass ([heights], [radii], [densities])
% double a12 resistance ([heights], [radii], [densities], [resistances])
% [double] a21 Gauß (#of_variables, [row1;row2])
% exp.:
% 6x+12y=30
% 3x+ 3y= 9
% [a1,a2]=a21(2,[6,12,30;3,3,9])
% a1 = 1
% a2 = 2
%
% [double] a22 Cramer (#of_variables, [row1;row2])
% exp.:
% 6x+12y=30
% 3x+ 3y= 9
% [a1,a2]=a21(2,[6,12,30;3,3,9])
% a1 = 1
% a2 = 2
%
% double a31 fixed-point ("func_name", start, limit)
% exp.:
% function y=fixed(x)
% y=(x+10)^(1/4);
% end
% a31("fixed",2,100)
% ans=1.8556
%
% double a32 bisection ("func_name", start, end, limit)
% exp.:
% function y=func(x)
% y=(x^4-x-10);
% end
% a32("func",0,3,100)
% ans=1.8556
%
% double a33 newton ("func_name","func_derv_name", start, limit)
% exp.:
% function y=func(x)
% y=(x^4-x-10);
% end
% function y=func_der(x)
% y=4*x^3-1;
% end
% a33("func","func_der",2,100)
% ans=1.8556
%
% [[double], a411 poly-intper (#of_variables, increments, [x] , [y])
% [double]]
% exp.:
% [a1,a2]=a411(5,0.1,[1,2,3,4,5],[34,22,45,56,66])
% a1 = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000
% a2 = 45.000 38.312 32.856 28.544 25.288 23.000 21.592 20.976 21.064 21.768 23.000 24.672 26.696 28.984 31.448 34.000 36.552 39.016 41.304 43.328 45.000 46.232 46.936 47.024 46.408 45.000 42.712 39.456 35.144 29.688 23.000
%
% [[double], a412 lin-intper (#of_variables, increments, [x] , [y])
% [double]]
% function seems unstable
%
% double a542 optim. ("func_name", x, y, inc)
% exp.:
% function z=func(x,y)
% z=-(x*x + y*y);
% end
% a542("func",-2,-2, 0.1)
% ans=6.3838e-016
%
% [double] a611 diff (#num_of_points, [a], [b])
%
% [double] a612 central-diff (#num_of_points, [a], [b])
% exp.:
% a=[0, 5, 10, 15, 20 ]
% b=[1, 0.8811, 0.7366, 0.5430, 0.1698]
% a612(5,a,b)
% ans= -0.023780 -0.026340 -0.033810 -0.019360 -0.074640
%
% double a621 trap-integ. ("function", interval_start, interval_end, precision)
% exp.:
% a621("sin",0,pi/2,0.000001)
% ans = 1.0000
%
% double a622 quad-integ. ("function", interval_start, interval_end, precision)
% exp.:
% a622("sin",0,pi/2,0.000001)
% ans = 1.0000
%
% [[double], a752 sim (t0, t_end, x0)
% [double]]
% exp.: [a1,a2]=a752(0,10,100)
% a1 = 0 1 2 3 4 5 6 7 8 9 10
% a2 = 100.000 60.000 40.000 30.000 25.000 22.500 21.250 20.625 20.312 20.156 20.078
%

View File

@ -5,12 +5,15 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
if (nrhs != 2 || ! mxIsNumeric (prhs[0]))
mexErrMsgTxt ("Check your input parameters");
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++){
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));
}
@ -18,46 +21,38 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
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++)
// 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 i = 0; i <= variables; i++ ){
free(GA[i]);
}
free(GA);
free(x);
//Output
nlhs = 1;
plhs[0] = mxCreateDoubleMatrix(1, variables, mxREAL);
memcpy(mxGetPr(plhs[0]), x, variables*sizeof(double));
return;
}

View File

@ -73,6 +73,9 @@ double determinant(double** ga, int m)
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
if (nrhs != 2 || ! mxIsNumeric (prhs[0]))
mexErrMsgTxt ("Check your input parameters");
variables = *mxGetPr(prhs[0]);
double* x = (double *)mxCalloc(variables, sizeof(double));
@ -100,21 +103,16 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output variables
for(int i = 0; i < variables; i++)
{
swap_column(CA2, i);
x[i] = determinant(CA2, variables);
x[i] = determinant(CA2, variables)/detA;
}
// Output
for(int j=0; j < variables; j++){
plhs[j]=mxCreateDoubleScalar(x[j]/detA );
}
// free memory
for (int i = 0; i <= variables; i++ ){
free(CA2[i]);
}
free(CA2);
free(x);
nlhs = 1;
plhs[0] = mxCreateDoubleMatrix(1, variables, mxREAL);
memcpy(mxGetPr(plhs[0]), x, variables*sizeof(double));
return;
}

39
Mex/a31.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 3 || ! mxIsNumeric (prhs[0]))
mexErrMsgTxt ("Check your input parameters");
char *func_Name;
func_Name = mxArrayToString(prhs[0]);
double start = *mxGetPr(prhs[1]);
int limit= *mxGetPr(prhs[2]);
//allocate memory
mxArray *func_Inputs[1];
mxArray *func_Outputs[1];
func_Inputs[0] = mxCreateDoubleScalar(start);
for(int i = limit ; i-- ;)
{
func_Inputs[0] = mxCreateDoubleScalar(start);
mexCallMATLAB(1, func_Outputs , 1 , func_Inputs , func_Name);
start = *mxGetPr(func_Outputs[0]);
}
// Output
plhs[0] = mxCreateDoubleScalar(start);
//free allocated memory
mxDestroyArray(func_Inputs[0]);
mxDestroyArray(func_Outputs[0]);
return;
}

11
Mex/a31.m Normal file
View File

@ -0,0 +1,11 @@
%function accepts 1 string, 1 double and the number of iterations
% a31("func_name", start, limit)
% function mus be in appropriate form for fixed point iteration
%
%exp.:
% function y=fixed(x)
% y=(x+10)^(1/4);
% end
%
% a31("fixed",2,100)
% ans=1.8556

View File

@ -7,6 +7,9 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
char *func_Name;
func_Name = mxArrayToString(prhs[0]);

View File

@ -1,5 +1,5 @@
%function accepts 1 string, 2 doubles and the number of iterations
% a31("func_name", start, end, limit)
% a32("func_name", start, end, limit)
% function must be in appropriate form for bisection
%
%exp.:
@ -7,5 +7,5 @@
% y=(x^4-x-10);
% end
%
% a32("func",2,3,100)
% a32("func",0,3,100)
% ans=1.8556

View File

@ -7,6 +7,9 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
char *func_Name_1;
func_Name_1 = mxArrayToString(prhs[0]);
char *func_Name_2;

View File

@ -11,5 +11,5 @@
% y=4*x^3-1;
% end
%
% a32("func","func_der",2,100)
% a33("func","func_der",2,100)
% ans=1.8556

View File

@ -6,6 +6,13 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
if( mxGetNumberOfElements(prhs[2]) != mxGetNumberOfElements(prhs[3]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
int variables = *mxGetPr(prhs[0]);
double inc = *mxGetPr(prhs[1]);

View File

@ -1,18 +1,8 @@
% 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
% function accepts 1 double and 1 double matrix (#of_variables,increments, [x] , [y])
% each row1 contains #of_variables elements
% returns 2 vectors, xx and yy; yy being the interpolated y values
% 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
%
% [a1,a2]=a411(5,0.1,[1,2,3,4,5],[34,22,45,56,66]);
% a1 = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000
% a2 = 45.000 38.312 32.856 28.544 25.288 23.000 21.592 20.976 21.064 21.768 23.000 24.672 26.696 28.984 31.448 34.000 36.552 39.016 41.304 43.328 45.000 46.232 46.936 47.024 46.408 45.000 42.712 39.456 35.144 29.688 23.000

View File

@ -6,6 +6,13 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
if( mxGetNumberOfElements(prhs[2]) != mxGetNumberOfElements(prhs[3]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
int variables = *mxGetPr(prhs[0]);
variables++;
double inc = *mxGetPr(prhs[1]);

6
Mex/a412.m Normal file
View File

@ -0,0 +1,6 @@
% function accepts 1 double and 1 double matrix (#of_variables,increments, [x] , [y])
% each row1 contains #of_variables elements
% returns 2 vectors, xx and yy; yy being the interpolated y values
% bsp.:
%
% [a1,a2]=a412(5,0.1,[1,2,3,4,5],[34,22,45,56,66])

View File

@ -17,6 +17,10 @@ double g_von_theta(long double x, long double y, char * func_Name)
void mexFunction(int nlhs, mxArray *plhs[], // Output variables
int nrhs, const mxArray *prhs[]) // Input variables
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
char *func_Name;
func_Name = mxArrayToString(prhs[0]);
//x = -2, y = -2

View File

@ -7,7 +7,13 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 3)
mexErrMsgTxt ("Check your input parameters");
if( mxGetNumberOfElements(prhs[1]) != mxGetNumberOfElements(prhs[2]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
int num_data_points = *mxGetPr(prhs[0]);
double* x = (double *)mxCalloc(num_data_points, sizeof(double));
@ -30,10 +36,10 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output
// Output
nlhs = num_data_points;
for(int j=0; j < num_data_points; j++){
plhs[j]=mxCreateDoubleScalar(temp[j]);
}
nlhs = 1;
plhs[0] = mxCreateDoubleMatrix(1, num_data_points, mxREAL);
memcpy(mxGetPr(plhs[0]), temp, num_data_points*sizeof(double));

View File

@ -2,4 +2,4 @@
% a611(#num_of_points, vector_a, vector_b)
% number of datapoints in both vectors must be equal,
% or else the result will not be correct
%
% returns vector

View File

@ -7,7 +7,13 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 3)
mexErrMsgTxt ("Check your input parameters");
if( mxGetNumberOfElements(prhs[1]) != mxGetNumberOfElements(prhs[2]))
{
mexErrMsgTxt ("arrays need to be of same size!");
}
int num_data_points = *mxGetPr(prhs[0]);
double* x = (double *)mxCalloc(num_data_points, sizeof(double));
@ -40,11 +46,9 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output
temp[i] = (y[i] - y[i-1]) / (x[i] - x[i-1]);
// Output
nlhs = num_data_points;
for(int j=0; j < num_data_points; j++){
plhs[j]=mxCreateDoubleScalar(temp[j]);
}
nlhs = 1;
plhs[0] = mxCreateDoubleMatrix(1, num_data_points, mxREAL);
memcpy(mxGetPr(plhs[0]), temp, num_data_points*sizeof(double));
return;

View File

@ -1,14 +1,10 @@
% function accepts 1 double and 2 vectors of double
% a611(#num_of_points, vector_a, vector_b)
% a612(#num_of_points, vector_a, vector_b)
% number of datapoints in both vectors must be equal,
% or else the result will not be correct
%
%exp.:
% a=[0, 5, 10, 15, 20 ]
% b=[1, 0.8811, 0.7366, 0.5430, 0.1698]
% [a1,a2,a3,a4,a5]=a611(5,a,b)
% a1 = -0.023780
% a2 = -0.026340
% a3 = -0.033810
% a4 = -0.019360
% a5 = -0.074640
% a612(5,a,b)
% ans= -0.023780 -0.026340 -0.033810 -0.019360 -0.074640

View File

@ -7,6 +7,10 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
char *func_Name;
func_Name = mxArrayToString(prhs[0]);

View File

@ -7,6 +7,9 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
if (nrhs != 4)
mexErrMsgTxt ("Check your input parameters");
char *func_Name;
func_Name = mxArrayToString(prhs[0]);

View File

@ -7,47 +7,53 @@
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
/*
char *func_Name;
func_Name = mxArrayToString(prhs[0]);
double t0 = *mxGetPr(prhs[1]);
double tfinal= *mxGetPr(prhs[2]);
double x0 = *mxGetPr(prhs[3]);
double* tout = mxGetPr(prhs[4]);
double* xout = mxGetPr(prhs[5]);
double* xout = mxGetPr(prhs[5]);*/
double t0 = *mxGetPr(prhs[0]);
double tfinal= *mxGetPr(prhs[1]);
double x0 = *mxGetPr(prhs[2]);
double theta_umg = 20, //temp of room
theta = x0, //start temp
h = 1, //incremtens size
t_start = t0, //start time
t_end = tfinal, //end time
t=t_start,
theta_dot;
double* t_out = (double *)mxCalloc((t_end-t_start)/h, sizeof(double)); //array cont. time stamps
double* theta_out = (double *)mxCalloc((t_end-t_start)/h, sizeof(double)); //array cont. temp
theta_dot = 0;
int len = (t_end-t_start)/h+1;
double* t_out = (double *)mxCalloc(len, sizeof(double)); //array cont. time stamps
double* theta_out = (double *)mxCalloc(len, sizeof(double)); //array cont. temp
t_out[0]=t_start;
theta_out[0]=theta;
for(int i = 1; t<t_end; i++)
{
theta_dot = -1/2*(theta-theta_umg);
theta_dot = -1*(theta-theta_umg)/2; //<- totally different from (-1/2)*(theta-theta_umg), because that doesn't work!
theta += theta_dot*h;
//debug output mexPrintf("%f \t %f \t %f \t %f\n",theta_dot, theta, theta_umg,h);
t += h;
t_out[i] = t;
theta_out = [i]= theta;
theta_out[i]= theta;
}
mxArray *func_Input[2];
mxArray *func_Outputs[1];
func_Input[0] = ;
func_Input[1] = ;
mexCallMATLAB(1, func_Outputs , 1 , func_Input , "plot");
// Output
nlhs = 2;
plhs[0] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[0]), t_out, len*sizeof(double));
plhs[1] = mxCreateDoubleMatrix(1, len, mxREAL);
memcpy(mxGetPr(plhs[1]), theta_out, len*sizeof(double));
return;
}
}

View File

@ -12,6 +12,9 @@ void mexFunction(int nlhs, mxArray *plhs[], // Output
func_Name = mxArrayToString(prhs[0]);
double* tout = mxGetPr(prhs[4]);
double* xout = mxGetPr(prhs[5]);*/
if (nrhs != 3)
mexErrMsgTxt ("Check your input parameters");
double t0 = *mxGetPr(prhs[0]);
double tfinal= *mxGetPr(prhs[1]);

View File

@ -1,8 +1,8 @@
% function accepts 3 doubles
% a700(to, t_end, x0)
% a700(t0, t_end, x0)
%
% a700 simulates the cooling of a fluid
%
%exp.: [a1,a2]=a700(0,10,100)
%exp.: [a1,a2]=a752(0,10,100)
% a1 = 0 1 2 3 4 5 6 7 8 9 10
% a2 = 100.000 60.000 40.000 30.000 25.000 22.500 21.250 20.625 20.312 20.156 20.078