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

📄 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
	{
		public Example()
		{
			linearsystemlibInitialize();	
		
		}

		public void CleanUp()
		{
			linearsystemlibTerminate();	
		}

		/* declare dll functions */

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

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

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMydiag(int nargout, ref IntPtr X, IntPtr v, IntPtr k);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyextractmatrix(int nargout, ref IntPtr B, IntPtr A
			, IntPtr rowa, IntPtr rowb
			, IntPtr cola, IntPtr colb);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyfull(int nargout, ref IntPtr A, IntPtr S);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMylu(int nargout, ref IntPtr L, ref IntPtr U
			, ref IntPtr P, IntPtr A);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMymldivide(int nargout, ref IntPtr x, IntPtr A, IntPtr b);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMymrdivide(int nargout, ref IntPtr x, IntPtr A, IntPtr b);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMysparse(int nargout, ref IntPtr S, IntPtr A);

		[ DllImport( "linearsystemlib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyspdiags(int nargout, ref IntPtr A, IntPtr B
			, IntPtr d, IntPtr m, IntPtr n);


	/* end dll functions */
	
		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine("Linear System Equations") ;

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

			Console.WriteLine() ;
			obj.LU_decompression() ;

			Console.WriteLine() ;
			obj.sparseSystem() ;

			Console.WriteLine() ;
			obj.tridiagonalSystem() ;

			Console.WriteLine() ;
			obj.bandMatrixSystem() ;

			obj.CleanUp() ;
		}

		/* **************************** */
		public void LinearSystemEquations()  
		{
			/* Solve general linear system equations Ax = b */
			double[,] db_A	 =  {	{1.1,  5.6, 3.3}  ,
									{4.4, 12.3, 6.6}  ,
									{7.7,  8.8, 9.9}  };

			double[] db_vectorb =  { 12.5, 32.2 , 45.6 } ;

			/* declare mxArray variables */
			IntPtr mx_A = (IntPtr)null ;
			IntPtr mx_b = (IntPtr)null ;
			IntPtr mx_x = (IntPtr)null ;

			/* convert Cs matrix to mxArray */
			mx_A = MatlabCSharp.double2mxArray_matrixReal(db_A) ;
			mx_b = MatlabCSharp.double2mxArray_vectorColumnReal(db_vectorb) ;

			/* call an implemental function */
			mlfMymldivide(1, ref mx_x, mx_A, mx_b);

			/* convert back to Cs double  */
			double[] db_vectorx = MatlabCSharp.mxArray2double_vectorReal(mx_x) ; 

			// print out
			MatlabCSharp.printVector(db_vectorx) ;
		}

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

		public void LU_decompression ()  
		{
			/* find lower and upper matrixes */
			double[,] db_A =  {	{1.1,  5.6, 3.3}  ,
								{4.4, 12.3, 6.6}  ,
								{7.7,  8.8, 9.9}  };

			/* declare mxArray variables */
			IntPtr mx_A = (IntPtr)null ;
			IntPtr mx_L = (IntPtr)null ;
			IntPtr mx_U = (IntPtr)null ;
			IntPtr mx_P = (IntPtr)null ;


			/* convert Cs matrix to mxArray */
			mx_A = MatlabCSharp.double2mxArray_matrixReal(db_A) ;

			/* call an implemental function */
			mlfMylu(3, ref mx_L, ref mx_U, ref mx_P, mx_A);

			/* convert back to Cs double  */

			double[,] db_L = MatlabCSharp.mxArray2double_matrixReal(mx_L) ;
			double[,] db_U = MatlabCSharp.mxArray2double_matrixReal(mx_U) ;

			/* print out */
			Console.WriteLine("The lower matrix") ;
			MatlabCSharp.printMatrix(db_L) ;

			Console.WriteLine("The upper matrix") ;
			MatlabCSharp.printMatrix(db_U) ;

		}


		/* **************************** */
		public void sparseSystem()  
		{
			/*
			A = 0		0		0		0		1.1
				0		2.2		0		0		0	
				3.3		0		0		0		0
				0		0		0		6.6		0
				0		0		5.5		0		0

			b =	  11.1	0		22.2	0		33.3
			*/


			/* Solve general linear system equations Ax = b */
			double[,] db_A = {  {0  ,    0  ,    0  ,   0  ,    1.1 } ,
								{0  ,    2.2,    0  ,   0  ,    0   } ,
								{3.3,    0  ,    0  ,   0  ,    0   } ,
								{0  ,    0  ,    0  ,   6.6,    0   } ,
								{0  ,    0  ,    5.5,   0  ,    0   } } ;


			double[] db_vectorb =  { 11.1,	0,	22.2,	0,	33.3 } ;

			/* declare mxArray variables */
			IntPtr  mx_A		= (IntPtr)null ; 
			IntPtr  mx_b		= (IntPtr)null ; 
			IntPtr  mx_x		= (IntPtr)null ; 

			/* note: we create mx_b and mx_x as column vectors */ 

			/* convert Cs matrix to mxArray */
			mx_A = MatlabCSharp.double2mxArray_matrixReal(db_A) ;
			mx_b = MatlabCSharp.double2mxArray_vectorColumnReal(db_vectorb) ;


			/* call an implemental function */
			mlfMysparse(1, ref mx_A, mx_A);
			mlfMysparse(1, ref mx_b, mx_b);

			mlfMymldivide(1, ref mx_x, mx_A, mx_b);
			mlfMyfull    (1, ref mx_x, mx_x);

			/* convert back to Cs double  */
			double[] db_vectorx = MatlabCSharp.mxArray2double_vectorReal(mx_x) ;

			/* print out */
			Console.WriteLine("Solution x :") ;
			MatlabCSharp.printVector(db_vectorx) ;

		}


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

		public void tridiagonalSystem()  
		{
			double [,]B = new double [6,6] ;
			B[0,0] =	1.1		;
			B[1,0] =	1.2		;
			B[2,0] =	1.3		;
			B[3,0] =	1.4		;
			B[4,0] =	1.5		;
			B[5,0] =	1.6		;

			/* columns 2 */ 
			B[0,1] =	4.1		;
			B[1,1] =	4.2		;
			B[2,1] =	4.3		;
			B[3,1] =	4.4		;
			B[4,1] =	4.5		;
			B[5,1] =	4.6		;

			/* columns 3 */
			B[0,2] =	2.1		;
			B[1,2] =	2.2		;
			B[2,2] =	2.3		;
			B[3,2] =	2.4		;
			B[4,2] =	2.5		;
			B[5,2] =	2.6		;

			double[] db_vectord = new double [6] ;
			db_vectord[0] = 1.2  ;
			db_vectord[1] = 4.5  ;
			db_vectord[2] = 5.6  ;
			db_vectord[3] = 12.4 ;
			db_vectord[4] = 7.8  ;
			db_vectord[5] = 6.8  ;

			int one		= 1 ;
			int two		= 2 ;
			int seven	= 7 ;

			int row = 6 ;
			int col = 6 ;

			int band = 3 ;  /* tridiagnal */
			int m = row ;
			int n = col + (band-1) ;

			double []d = {0, 1, 2} ; /* start from 0 */

			int rowB = row  ;
			int colB = band ;

			/* declare mxArray variables */
			IntPtr  mx_B		= (IntPtr) null ;
			IntPtr  mx_bufferA	= (IntPtr) null ;
			IntPtr  mx_A		= (IntPtr) null ;

			IntPtr  mx_d		= (IntPtr) null ;
			IntPtr  mx_m		= (IntPtr) null ;
			IntPtr  mx_n		= (IntPtr) null ;

			IntPtr  mx_one		= (IntPtr) null ;
			IntPtr  mx_two		= (IntPtr) null ;
			IntPtr  mx_seven	= (IntPtr) null ;
			IntPtr  mx_row		= (IntPtr) null ;

			IntPtr  mx_vectord	= (IntPtr) null ;
			IntPtr  mx_x		= (IntPtr) null ;

			/* note: we create mx_vectord and mx_x are column vectors
			   when using in the following functions */ 

			/* convert Cs matrix to mxArray */
			mx_one	= MatlabCSharp.double2mxArray_scalarReal(one)	;
			mx_two	= MatlabCSharp.double2mxArray_scalarReal(two)	;
			mx_seven= MatlabCSharp.double2mxArray_scalarReal(seven) ;
			mx_row	= MatlabCSharp.double2mxArray_scalarReal(row)	;

			mx_m = MatlabCSharp.double2mxArray_scalarReal(m) ;
			mx_n = MatlabCSharp.double2mxArray_scalarReal(n) ;

			mx_d = MatlabCSharp.double2mxArray_vectorColumnReal(d) ;
			mx_B = MatlabCSharp.double2mxArray_matrixReal(B) ;

			mx_vectord = MatlabCSharp.double2mxArray_vectorColumnReal(db_vectord) ;

			/* call an implemental function */

			/* create a sparse matrix mx_bufferA from column-matrix B  */
			mlfMyspdiags (1, ref mx_bufferA, mx_B, mx_d , mx_m, mx_n);
			mlfMyfull(1, ref mx_bufferA, mx_bufferA) ;

			/* plot to see */
			double[,] db_bufferA = MatlabCSharp.mxArray2double_matrixReal(mx_bufferA) ;

			Console.WriteLine("The buffer matrix A:") ;
			MatlabCSharp.printMatrix(db_bufferA) ;


			/* extract the need-matrix from the buffter matrix,
				from row 1 to row 6 and from column 2 to column 7 */
			mlfMyextractmatrix(1, ref mx_A, mx_bufferA, mx_one, mx_row, mx_two, mx_seven);

			/* plot to see */
			double[,] db_A = MatlabCSharp.mxArray2double_matrixReal(mx_A) ;

			Console.WriteLine("The need-matrix A:") ;
			MatlabCSharp.printMatrix(db_A) ;

			/* solve the tridiagnal system equations */
			mlfMymldivide(1, ref mx_x, mx_A, mx_vectord);

			/* convert back to Cs double  */
			double[] db_vectorx = MatlabCSharp.mxArray2double_vectorReal(mx_x) ;

			/*print out */
			Console.WriteLine("Tridiagnal system solution:") ;
			MatlabCSharp.printVector(db_vectorx) ;

		}


		/* **************************** */
		public void bandMatrixSystem()  
		{
			double[,] B = new double[6,4] ;

			/* columns 1 */
			B[0,0] =	1.1		;
			B[1,0] =	1.2		;
			B[2,0] =	1.3		;
			B[3,0] =	1.4		;
			B[4,0] =	1.5		;
			B[5,0] =	1.6		;

			/* columns 2 */
			B[0,1] =	4.1		;
			B[1,1] =	4.2		;
			B[2,1] =	4.3		;
			B[3,1] =	4.4		;
			B[4,1] =	4.5		;
			B[5,1] =	4.6		;

			/* columns 3 */
			B[0,2] =	2.1		;
			B[1,2] =	2.2		;
			B[2,2] =	2.3		;
			B[3,2] =	2.4		;
			B[4,2] =	2.5		;
			B[5,2] =	2.6		;

			/* columns 4 */
			B[0,3] =	7.1		;
			B[1,3] =	7.2		;
			B[2,3] =	7.3		;
			B[3,3] =	7.4		;
			B[4,3] =	7.5		;
			B[5,3] =	7.6		;

			double [] db_vectord = new double [6] ;
			db_vectord[0] = 1.2  ;
			db_vectord[1] = 4.5  ;
			db_vectord[2] = 5.6  ;
			db_vectord[3] = 12.4 ;
			db_vectord[4] = 7.8  ;
			db_vectord[5] = 6.8  ;

			int i ;
			int one		= 1 ;
			int two		= 2 ;
			int seven	= 7 ;

			int row = 6 ;
			int col = 6 ;

			int band = 4 ;  /* band width */
			int m = row ;
			int n = col + (band-1) ;

			double [] d = {0, 1, 2, 3} ; /* start from 0 */

			int rowB = row  ;
			int colB = band ;

			/* declare mxArray variables */
			IntPtr mx_B			= (IntPtr) null ; 
			IntPtr mx_bufferA	= (IntPtr) null ; 
			IntPtr mx_A			= (IntPtr) null ; 

			IntPtr mx_d		= (IntPtr) null ; 
			IntPtr mx_m		= (IntPtr) null ; 
			IntPtr mx_n		= (IntPtr) null ; 

			IntPtr mx_one	= (IntPtr) null ; 
			IntPtr mx_two	= (IntPtr) null ; 
			IntPtr mx_seven	= (IntPtr) null ; 
			IntPtr mx_row	= (IntPtr) null ; 

			IntPtr mx_vectord	= (IntPtr) null ; 
			IntPtr mx_x			= (IntPtr) null ; 

			/* note: we create mx_vectord and mx_x are column vectors 
			   when using in the following functions */ 
			/* convert Cs double to mxArray */
			mx_one = MatlabCSharp.double2mxArray_scalarReal(one) ;
			mx_two = MatlabCSharp.double2mxArray_scalarReal(two) ;
			mx_seven = MatlabCSharp.double2mxArray_scalarReal(seven) ;
			mx_row	 = MatlabCSharp.double2mxArray_scalarReal(row) ;

			mx_m = MatlabCSharp.double2mxArray_scalarReal(m) ;
			mx_n = MatlabCSharp.double2mxArray_scalarReal(n) ;

			mx_d = MatlabCSharp.double2mxArray_vectorColumnReal(d) ;
			mx_B = MatlabCSharp.double2mxArray_matrixReal(B) ;

			mx_vectord = MatlabCSharp.double2mxArray_vectorColumnReal(db_vectord) ;

			/* step 4 : call an implemental function */

			/* create a sparse matrix, size mxn, from column-matrix B  */
			mlfMyspdiags (1, ref mx_bufferA, mx_B, mx_d , mx_m, mx_n);
			mlfMyfull(1, ref mx_bufferA, mx_bufferA) ;

			/* plot to see */
			double[,] db_bufferA = MatlabCSharp.mxArray2double_matrixReal(mx_bufferA) ;

			Console.WriteLine("The buffer band-matrix A:") ;
			MatlabCSharp.printMatrix(db_bufferA) ;

			/* extract the need-matrix A from the buffter matrix,
				from row 1 to row 6 and from column 2 to column 7 */
			mlfMyextractmatrix(1, ref mx_A, mx_bufferA, mx_one, mx_row, mx_two, mx_seven);

			/* plot to see */
			double[,] db_A = MatlabCSharp.mxArray2double_matrixReal(mx_A) ;

			Console.WriteLine("The band-need-matrix A:" ) ;
			MatlabCSharp.printMatrix(db_A) ;

			/* solve the system equations */
			mlfMymldivide(1, ref mx_x, mx_A, mx_vectord);

			/* convert back to Cs double  */
			double[] db_vectorx = MatlabCSharp.mxArray2double_vectorReal(mx_x) ;

			/* convert back to Cs double */
			Console.WriteLine("Band matrix system solution:") ;
			MatlabCSharp.printVector(db_vectorx) ;

		}

	}
}

⌨️ 快捷键说明

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