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

📄 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()
		{
			rootslibInitialize();	
		}

		public void CleanUp()
		{
			rootslibTerminate();	
		}

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

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

		[ DllImport( "rootslib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyfzero(int nargout, ref IntPtr x
			, IntPtr strfunc, IntPtr x0);

		[ DllImport( "rootslib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyroots(int nargout, ref IntPtr r, IntPtr c);


/* end dll functions */

		[STAThread]
		static void Main(string[] args)
		{
			Console.WriteLine("Roots of functions.") ;

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

			Console.WriteLine("\n") ;
			obj.FindingZeroFunction() ;

			obj.CleanUp() ;
		}

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

		public void FindingRootsPolynormial()  
		{
			/* 
			Find the solutions of polynomial function:
				f(x) = -x^3 + 7.2x^2 -21x -5  
			*/
			int order = 3 ;
			double[] db_coefs = { -1, 7.2, -21, -5 } ;

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

			/* convert Cs double to mxArray */
			mx_coefs = MatlabCSharp.double2mxArray_vectorColumnReal (db_coefs) ;

			/* step 4 : call an implemental function */
			mlfMyroots(1, ref mx_x, mx_coefs);

			/* step 5 : convert back to C/C++ double  */
			double []db_xReal = new double [order] ;
			double []db_xImag = new double [order] ;

			MatlabCSharp.mxArray2double_vectorComplex(mx_x, ref db_xReal, ref db_xImag) ;

			/* print out */
			Console.WriteLine("Solutions of the polynomial function : ") ;
			int i ;
			for (i=0; i<order; i++)  
			{
				Console.Write( db_xReal[i].ToString() +  " + " ) ;
				Console.Write( db_xImag[i].ToString() + "i \n" ) ;
			}

		}


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

		public void FindingZeroFunction ( ) 
		{
			/* Find the solution of the function f(x) = sin(2*x) + cos(x) + 1  
				fzero(..) returns ONLY ONE SOLUTION near a initial guess value	
				If your problem is complicated, please look at functions 
				in Optimization Tool Box
			*/
			double db_initialGuess = 0.9 ;
			String strfunc = "sin(2*x) + cos(x) + 1" ;

			/* declare mxArray variables */
			IntPtr mx_initialGuess = (IntPtr) null  ;
			IntPtr mx_x			   = (IntPtr) null  ;
			IntPtr mx_strfunc ;

			/* convert Cs double to mxArray */
			mx_initialGuess = MatlabCSharp.double2mxArray_scalarReal (db_initialGuess) ;
			mx_strfunc = MatlabCSharp.mxCreateString(strfunc)	; 

			/* call an implemental function */
			mlfMyfzero(1, ref mx_x, mx_strfunc, mx_initialGuess);

			/* convert back to Cs double  */
			double db_xReal   ;
			db_xReal = MatlabCSharp.mxArray2double_scalarReal(mx_x) ;

			/* print out */
			Console.WriteLine("Solutions of the given function : " ) ;
			Console.WriteLine( db_xReal.ToString() ) ;
			
		}

	} // end class 
}

⌨️ 快捷键说明

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