📄 example.cs
字号:
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;
namespace MatlabCSharpExample
{
class Example
{
//constructor
public Example()
{
curvefittinglibInitialize();
}
public void CleanUp()
{
curvefittinglibTerminate();
}
/* declare dll functions */
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void curvefittinglibInitialize();
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void curvefittinglibTerminate();
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyinterp1(int nargout, ref IntPtr yi, IntPtr x
, IntPtr y, IntPtr xi);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyinterp2(int nargout, ref IntPtr ZI, IntPtr X
, IntPtr Y, IntPtr Z, IntPtr XI
, IntPtr YI, IntPtr method);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMypolyfit(int nargout, ref IntPtr p, IntPtr x
, IntPtr y, IntPtr n);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMypolyval(int nargout, ref IntPtr y, IntPtr p, IntPtr x);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMymeshgrid(int nargout, ref IntPtr X, ref IntPtr Y
, IntPtr vectorstepx, IntPtr vectorstepy);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMygriddata(int nargout, ref IntPtr ZI, IntPtr x, IntPtr y
, IntPtr z, IntPtr XI, IntPtr YI);
[ DllImport( "curvefittinglib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyfinemeshgrid(int nargout, ref IntPtr row, ref IntPtr col
, IntPtr vectorstepx, IntPtr vectorstepy);
/* end dll functions */
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Curve Fitting:") ;
Example obj = new Example() ;
Console.WriteLine("1. Polynomial") ;
obj.PolynomialFittingCurve() ;
//Environment.NewLine ;
Console.WriteLine("2. One-dimensional interpolation" ) ;
obj.OneDimensionInterpolation() ;
//Environment.NewLine ;
Console.WriteLine("3. Two-dimensional interpolation") ;
obj.TwoDimensionsInterpolation() ;
Console.WriteLine("4. Two-dimensional interpolation with fine grid") ;
obj.TwoDimensionsInterpolationFineSolution() ;
obj.CleanUp() ;
}
/* **************************** */
public void PolynomialFittingCurve()
{
double[] db_X = { 1, 2, 3, 4, 5, 6 } ;
double[] db_Y= { 6.8, 50.2, 140.8, 280.5, 321.4, 428.6 };
double db_three = 3 ;
double db_oneValue = 2.2 ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
IntPtr mx_coefs = (IntPtr) null ;
IntPtr mx_three = (IntPtr) null ;
IntPtr mx_oneValue = (IntPtr) null ;
IntPtr mx_funcValue = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_X = MatlabCSharp.double2mxArray_vectorRowReal (db_X) ;
mx_Y = MatlabCSharp.double2mxArray_vectorRowReal (db_Y) ;
mx_three = MatlabCSharp.double2mxArray_scalarReal (db_three) ;
mx_oneValue = MatlabCSharp.double2mxArray_scalarReal (db_oneValue) ;
/* call an implemental function */
mlfMypolyfit(1, ref mx_coefs, mx_X, mx_Y, mx_three);
/* convert back to Cs double */
double[] db_coefs = MatlabCSharp.mxArray2double_vectorReal(mx_coefs) ;
/* print out */
Console.WriteLine("The polynomial:" ) ;
Console.Write( " {0}x^3 + ", db_coefs[0].ToString() ) ;
Console.Write( " {0}x^2 + ", db_coefs[1].ToString() ) ;
Console.Write( " {0}x + ", db_coefs[2].ToString() ) ;
Console.Write( db_coefs[3].ToString() ) ;
Console.WriteLine() ;
/* calculate the function value at oneValue */
mlfMypolyval(1, ref mx_funcValue, mx_coefs, mx_oneValue);
double db_funcValue = MatlabCSharp.mxArray2double_scalarReal(mx_funcValue) ;
Console.Write("The function value at 2.2 is: {0} ", db_funcValue.ToString() ) ;
}
/* **************************** */
public void OneDimensionInterpolation()
{
double[] db_X = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
double[] db_Y = { 6.8, 24.6, 50.2, 74, 140.8, 280.5, 321.4, 428.6 };
double db_oneValue = 2.1 ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
IntPtr mx_oneValue = (IntPtr) null ;
IntPtr mx_funcValue = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_X = MatlabCSharp.double2mxArray_vectorRowReal(db_X) ;
mx_Y = MatlabCSharp.double2mxArray_vectorRowReal(db_Y) ;
mx_oneValue = MatlabCSharp.double2mxArray_scalarReal(db_oneValue) ;
/* call an implemental function */
mlfMyinterp1(1, ref mx_funcValue, mx_X, mx_Y, mx_oneValue);
/* convert back to Cs double */
double db_funcValue = MatlabCSharp.mxArray2double_scalarReal(mx_funcValue) ;
Console.WriteLine( "The function value at 2.1 is: {0}", db_funcValue.ToString() );
}
/* **************************** */
public void TwoDimensionsInterpolation()
{
/* function values z at 47 points, (x_i, y_j) are : */
/* matrix z values*/
double[,] z = {
{ 0.0001, 0.0034, -0.0299, -0.2450, -0.1100, -0.0043, 0.0000 } ,
{ 0.0007, 0.0468, -0.5921, -4.7596, -2.1024, -0.0616, 0.0004 } ,
{ -0.0088, -0.1301, 1.8559, -0.7239, -0.2729, 0.4996, 0.0130 } ,
{ -0.0365, -1.3327, -1.6523, 0.9810, 2.9369, 1.4122, 0.0331 } ,
{ -0.0137, -0.4808, 0.2289, 3.6886, 2.4338, 0.5805, 0.0125 } ,
{ 0.0000, 0.0797, 2.0967, 5.8591, 2.2099, 0.1328, 0.0013 } ,
{ 0.0000, 0.0053, 0.1099, 0.2999, 0.1107, 0.0057, 0.0000 } };
double[] db_vectorstep = {-3, 1, 3} ;
double db_a = 2.3 ;
double db_b = 0.7 ;
/* declare mxArray variables */
IntPtr mx_a = (IntPtr) null ;
IntPtr mx_b = (IntPtr) null ;
IntPtr mx_interp2z = (IntPtr) null ;
IntPtr mx_x = (IntPtr) null ;
IntPtr mx_y = (IntPtr) null ;
IntPtr mx_z = (IntPtr) null ;
IntPtr mx_vectorstepx = (IntPtr) null ;
IntPtr mx_vectorstepy = (IntPtr) null ;
IntPtr mx_method ;
/* convert Cs double to mxArray */
mx_a = MatlabCSharp.double2mxArray_scalarReal(db_a) ;
mx_b = MatlabCSharp.double2mxArray_scalarReal (db_b) ;
/* same for two step-vectors */
mx_vectorstepx = MatlabCSharp.double2mxArray_vectorRowReal(db_vectorstep) ;
mx_vectorstepy = MatlabCSharp.double2mxArray_vectorRowReal(db_vectorstep) ;
mx_z = MatlabCSharp.double2mxArray_matrixReal(z) ;
/* call implemental functions */
/* create values for the matrix x and matrix y */
mlfMymeshgrid(2, ref mx_x, ref mx_y, mx_vectorstepx, mx_vectorstepy) ;
String interp2method = "cubic";
mx_method = MatlabCSharp.mxCreateString(interp2method);
mlfMyinterp2(1, ref mx_interp2z, mx_x, mx_y, mx_z, mx_a, mx_b, mx_method);
/* convert back to Cs double */
double [,] db_x = MatlabCSharp.mxArray2double_matrixReal(mx_x) ;
double [,] db_y = MatlabCSharp.mxArray2double_matrixReal(mx_y) ;
/* print out */
Console.WriteLine("Matrix x : " ) ;
MatlabCSharp.printMatrix(db_x) ;
Console.WriteLine("Matrix y : " ) ;
MatlabCSharp.printMatrix(db_y) ;
double db_interp2z = MatlabCSharp.mxArray2double_scalarReal(mx_interp2z) ;
Console.WriteLine("Interpolation in two dimensions with cubic method " ) ;
Console.WriteLine(" z = {0}", db_interp2z.ToString() );
}
public void TwoDimensionsInterpolationFineSolution()
{
/* function values z at 47 points, (x_i, y_j) are : */
/* matrix z values*/
double[,] z = {
{ 0.0001, 0.0034, -0.0299, -0.2450, -0.1100, -0.0043, 0.0000 } ,
{ 0.0007, 0.0468, -0.5921, -4.7596, -2.1024, -0.0616, 0.0004 } ,
{ -0.0088, -0.1301, 1.8559, -0.7239, -0.2729, 0.4996, 0.0130 } ,
{ -0.0365, -1.3327, -1.6523, 0.9810, 2.9369, 1.4122, 0.0331 } ,
{ -0.0137, -0.4808, 0.2289, 3.6886, 2.4338, 0.5805, 0.0125 } ,
{ 0.0000, 0.0797, 2.0967, 5.8591, 2.2099, 0.1328, 0.0013 } ,
{ 0.0000, 0.0053, 0.1099, 0.2999, 0.1107, 0.0057, 0.0000 } };
double[] db_vectorstep = {-3, 1 , 3} ;
double[] db_finevectorstep = {-3, 0.2, 3} ;
double db_a = 2.3 ;
double db_b = 0.7 ;
/* declare mxArray variables */
IntPtr mx_a = (IntPtr) null ;
IntPtr mx_b = (IntPtr) null ;
IntPtr mx_interp2z = (IntPtr) null ;
IntPtr mx_x = (IntPtr) null ;
IntPtr mx_y = (IntPtr) null ;
IntPtr mx_z = (IntPtr) null ;
IntPtr mx_vectorstepx = (IntPtr) null ;
IntPtr mx_vectorstepy = (IntPtr) null ;
IntPtr mx_method ;
//
IntPtr mx_finex = (IntPtr) null ;
IntPtr mx_finey = (IntPtr) null ;
IntPtr mx_finez = (IntPtr) null ;
IntPtr mx_finerow = (IntPtr) null ;
IntPtr mx_finecol = (IntPtr) null ;
IntPtr mx_finevectorstepx = (IntPtr) null ;
IntPtr mx_finevectorstepy = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_a = MatlabCSharp.double2mxArray_scalarReal (db_a) ;
mx_b = MatlabCSharp.double2mxArray_scalarReal (db_b) ;
/* same for two step-vectors */
mx_vectorstepx = MatlabCSharp.double2mxArray_vectorRowReal (db_vectorstep) ;
mx_vectorstepy = MatlabCSharp.double2mxArray_vectorRowReal (db_vectorstep) ;
mx_z = MatlabCSharp.double2mxArray_matrixReal (z) ;
//
mx_finevectorstepx = MatlabCSharp.double2mxArray_vectorRowReal (db_finevectorstep) ;
mx_finevectorstepy = MatlabCSharp.double2mxArray_vectorRowReal (db_finevectorstep) ;
/* get size for fine matrixes */
mlfMyfinemeshgrid(2, ref mx_finerow, ref mx_finecol, mx_finevectorstepx, mx_finevectorstepy) ;
int finerow = (int) MatlabCSharp.mxArray2double_scalarReal(mx_finerow) ;
int finecol = (int) MatlabCSharp.mxArray2double_scalarReal(mx_finecol) ;
Console.WriteLine(" fine row = {0}", finerow.ToString() ) ;
Console.WriteLine(" fine col = {0}", finecol.ToString() ) ;
/* call implemental functions */
/* create values for the matrix x and matrix y */
mlfMymeshgrid(2, ref mx_x, ref mx_y, mx_vectorstepx, mx_vectorstepy) ;
/* create values for the fine matrix x and fine matrix y */
mlfMymeshgrid(2, ref mx_finex, ref mx_finey, mx_finevectorstepx, mx_finevectorstepy) ;
/* get a fine matrix mx_finez from mx_z */
mlfMygriddata(1, ref mx_finez, mx_x, mx_y, mx_z, mx_finex, mx_finey);
String interp2method = "cubic" ;
mx_method = MatlabCSharp.mxCreateString(interp2method) ;
mlfMyinterp2(1, ref mx_interp2z, mx_finex, mx_finey, mx_finez, mx_a, mx_b, mx_method);
/* convert back to Cs double */
double db_interp2finez = MatlabCSharp.mxArray2double_scalarReal(mx_interp2z) ;
Console.WriteLine(@"Interpolation in two dimensions with cubic method and a fine grid " ) ;
Console.WriteLine(" z = {0}", db_interp2finez.ToString() ) ;
}
} // end class
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -