⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.cs

📁 MATLAB C# Book. This book is a great tutorial for C# programmers who use MATLAB to develop applicati
💻 CS
字号:
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;

namespace MatlabCSharpExample
{
	class Example
	{
		//constructor
		public Example()
		{
			integrationlibInitialize();	
		}

		public void CleanUp()
		{
			integrationlibTerminate();	
		}

/* declare dll functions */
		[ DllImport( "integrationlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void integrationlibInitialize(); 

		[ DllImport( "integrationlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void integrationlibTerminate(); 

		[ DllImport( "integrationlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyquad(int nargout, ref IntPtr y, IntPtr strfunc
			, IntPtr a, IntPtr b);

		[ DllImport( "integrationlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMydblquad(int nargout, ref IntPtr dbint
			, IntPtr strfunc, IntPtr x1
			, IntPtr x2, IntPtr y1, IntPtr y2);


/* end dll functions */

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine("Single integration:") ;

			Example obj = new Example() ; 
			obj.singleIntegration() ;

			Console.WriteLine("Double-integration:") ;
			obj.dbIntegration() ;
			obj.CleanUp() ;
		}


		public void singleIntegration()  
		{
			String strfunc = "sin(x) + x.^2" ;

			double db_beginInterval = 0 ;
			double db_endInterval	= 3*Math.PI ; // using the value pi in C#

			/* declare mxArray variables */
			IntPtr mx_strfunc ;
			IntPtr mx_beginInterval = (IntPtr) null  ;
			IntPtr mx_endInterval   = (IntPtr) null  ;
			IntPtr mx_y				= (IntPtr) null  ;

			/* convert Cs double to mxArray */
			mx_strfunc		 = MatlabCSharp.mxCreateString(strfunc)	; 
			mx_beginInterval = MatlabCSharp.double2mxArray_scalarReal (db_beginInterval) ;
			mx_endInterval   = MatlabCSharp.double2mxArray_scalarReal (db_endInterval) ;


			/* call an implemental function */
			mlfMyquad(1, ref mx_y, mx_strfunc, mx_beginInterval, mx_endInterval);

			/* convert back to Cs double  */
			double db_y = MatlabCSharp.mxArray2double_scalarReal(mx_y) ;
			Console.WriteLine(" I = {0}", db_y.ToString() ) ;

		}

/* ******************************************* */

		public void dbIntegration()  
		{
			String strfunc = "sin(x) + x.^2 + y.^3" ;

			double db_x1 = 0 ;
			double db_x2 = 3*Math.PI ; // using the value pi in C#

			double db_y1 = 0 ;
			double db_y2 = Math.PI ; 

			/* declare mxArray variables */
			IntPtr mx_strfunc ;
			IntPtr mx_x1 = (IntPtr) null  ;
			IntPtr mx_x2 = (IntPtr) null  ;

			IntPtr mx_y1 = (IntPtr) null  ;
			IntPtr mx_y2 = (IntPtr) null  ;

			IntPtr mx_II = (IntPtr) null  ;

			/* convert Cs double to mxArray */
			mx_strfunc = MatlabCSharp.mxCreateString(strfunc)	; 
			mx_x1 = MatlabCSharp.double2mxArray_scalarReal (db_x1) ;
			mx_x2 = MatlabCSharp.double2mxArray_scalarReal (db_x2) ;

			mx_y1 = MatlabCSharp.double2mxArray_scalarReal (db_y1) ;
			mx_y2 = MatlabCSharp.double2mxArray_scalarReal (db_y2) ;

			/* call an implemental function */
			mlfMydblquad(1, ref mx_II, mx_strfunc, mx_x1, mx_x2, mx_y1, mx_y2) ;

			/* convert back to Cs double  */
			double db_II = MatlabCSharp.mxArray2double_scalarReal(mx_II) ;
			Console.WriteLine(" II = {0}", db_II.ToString() ) ;

		}

	} // end class 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -