From 61dfd3954855e87e3aa36230052ac5259ed247e0 Mon Sep 17 00:00:00 2001 From: Patrice Matz Date: Fri, 5 Jan 2018 22:42:24 +0100 Subject: [PATCH] -corrected a32 and added a33(newton) --- Mex/a32.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Mex/a32.m | 11 +++++++++++ Mex/a33.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++ Mex/a33.m | 15 ++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 Mex/a32.cpp create mode 100644 Mex/a32.m create mode 100644 Mex/a33.cpp create mode 100644 Mex/a33.m diff --git a/Mex/a32.cpp b/Mex/a32.cpp new file mode 100644 index 0000000..893e752 --- /dev/null +++ b/Mex/a32.cpp @@ -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; +} \ No newline at end of file diff --git a/Mex/a32.m b/Mex/a32.m new file mode 100644 index 0000000..2f7ed34 --- /dev/null +++ b/Mex/a32.m @@ -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 \ No newline at end of file diff --git a/Mex/a33.cpp b/Mex/a33.cpp new file mode 100644 index 0000000..d091fc6 --- /dev/null +++ b/Mex/a33.cpp @@ -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; +} \ No newline at end of file diff --git a/Mex/a33.m b/Mex/a33.m new file mode 100644 index 0000000..a27eeb5 --- /dev/null +++ b/Mex/a33.m @@ -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 \ No newline at end of file