-added 6.2.2 (trapz integral)

This commit is contained in:
Patrice Matz 2018-01-06 19:35:19 +01:00
parent 16c9390f36
commit 020311b429
3 changed files with 64 additions and 1 deletions

56
Mex/a622.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 x1= *mxGetPr(prhs[2]);
double precision = *mxGetPr(prhs[3]);
double A=0;
int fak = 1;
double ya = 0;
//trapz
mxArray *func_Input[1];
mxArray *func_Outputs[1];
for(int i = 0; a < x1; a += precision)
{
func_Input[0] = mxCreateDoubleScalar(a);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name);
ya = *mxGetPr(func_Outputs[0]);
A += fak*ya;
if(i%2)
fak=2;
else
fak=4;
}
func_Input[0] = mxCreateDoubleScalar(a);
mexCallMATLAB(1, func_Outputs , 1 , func_Input , func_Name);
ya = *mxGetPr(func_Outputs[0]);
A += ya;
A = A*precision/3;
// Output
plhs[0] = mxCreateDoubleScalar(A);
//free allocated memory
mxDestroyArray(func_Input[0]);
mxDestroyArray(func_Outputs[0]);
return;
}

7
Mex/a622.m Normal file
View File

@ -0,0 +1,7 @@
% function accepts 1 string and 3 doubles
% a622("function", interval_start, interval_end, precision)
%
% a622 implements trapz (integral)
%
%exp.: a622("sin",0,pi/2,0.000001)
% ans = 1.0000

View File

@ -29,7 +29,7 @@ long double INTEGRAL::trapz(long double x0, long double x1, long double precisio
long double INTEGRAL::quad(/*long double (* funk)(long double), */long double x0, long double x1, long double precision)
{
long double A = 0, a = x0, b = a + precision;
long double A = 0, a = x0;
int fak = 1;
for (int i = 0; a < x1; a += precision, i++)