-corrected a32 and added a33(newton)

This commit is contained in:
Patrice Matz 2018-01-05 22:42:24 +01:00
parent 1bd9aa037e
commit 61dfd39548
4 changed files with 131 additions and 0 deletions

56
Mex/a32.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
char *func_Name;
func_Name = mxArrayToString(prhs[0]);
double a = *mxGetPr(prhs[1]);
double b = *mxGetPr(prhs[2]);
int limit= *mxGetPr(prhs[3]);
double c;
//allocate memory
mxArray *func_Input[1];
mxArray *func_Outputs[1];
func_Input[0] = mxCreateDoubleScalar(a);
//bisection
for (int i = limit; i--;)
{
double tempA, tempB, tempC;
c = (a + b) / 2;
func_Input[0] = mxCreateDoubleScalar(a);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name);
tempA = *mxGetPr(func_Outputs[0]);
func_Input[0] = mxCreateDoubleScalar(b);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name);
tempB = *mxGetPr(func_Outputs[0]);
func_Input[0] = mxCreateDoubleScalar(c);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name);
tempC = *mxGetPr(func_Outputs[0]);
if(tempA*tempC > 0){ a = c; }
if(tempC*tempB > 0){ b = c; }
}
// Output
plhs[0] = mxCreateDoubleScalar(c);
//free allocated memory
mxDestroyArray(func_Input[0]);
mxDestroyArray(func_Outputs[0]);
return;
}

11
Mex/a32.m Normal file
View File

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

49
Mex/a33.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "mex.h"
#include "matrix.h"
#include "stdlib.h"
void mexFunction(int nlhs, mxArray *plhs[], // Output
int nrhs, const mxArray *prhs[]) // Input
{
char *func_Name_1;
func_Name_1 = mxArrayToString(prhs[0]);
char *func_Name_2;
func_Name_2 = mxArrayToString(prhs[1]);
double x = *mxGetPr(prhs[2]);
int limit= *mxGetPr(prhs[3]);
//allocate memory
mxArray *func_Input[1];
mxArray *func_Outputs[1];
//bisection
for (limit; limit--;)
{
double temp_func_1, temp_func_2;
func_Input[0] = mxCreateDoubleScalar(x);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name_1); //f(x)
temp_func_1 = *mxGetPr(func_Outputs[0]);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name_2); //f'(x)
temp_func_2 = *mxGetPr(func_Outputs[0]);
x = x - temp_func_1/temp_func_2;
}
// Output
plhs[0] = mxCreateDoubleScalar(x);
//free allocated memory
mxDestroyArray(func_Input[0]);
mxDestroyArray(func_Outputs[0]);
return;
}

15
Mex/a33.m Normal file
View File

@ -0,0 +1,15 @@
% function accepts 2 strings, 1 double and the number of iterations
% a31("func_name","func_derv_name", start, limit)
% functions must be in appropriate form for newton
%
%exp.:
% function y=func(x)
% y=(x^4-x-10);
% end
%
% function y=func_der(x)
% y=4*x^3-1;
% end
%
% a32("func","func_der",2,100)
% ans=1.8556