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

📄 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
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Example
	{
		//constructor
		public Example()
		{
			odelibInitialize();	
		}

		public void CleanUp()
		{
			odelibTerminate();	
		}

/* declare dll functions */

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

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

		[ DllImport( "odelib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyode45firstorder(int nargout, ref IntPtr t
			, ref IntPtr y, IntPtr strfunc
			, IntPtr tspan, IntPtr y0);

		[ DllImport( "odelib.dll ", CallingConvention = CallingConvention.Cdecl)] 
		public static extern void mlfMyode45secondorder(int nargout, ref IntPtr t
			, ref IntPtr y, IntPtr strfunc
			, IntPtr tspan, IntPtr y0);
		
/* end dll functions */

		[STAThread]
		static void Main(string[] args)
		{
			Example obj = new Example() ; 
			Console.WriteLine("ODE problems.") ;

			Console.WriteLine("First order ODE.") ;
			obj.FirstOrder() ;
			obj.FirstOrderGetParticularValues() ;

			Console.WriteLine("Second order ODE.") ;
			obj.SecondOrder()  ;

			obj.CleanUp() ;
		}

		/* **************************** */
		public void FirstOrder()  
		{
			/* Calculating first order ODE */ 
			//String strfunc = "2+y" ;

			String strfunc = "cos(t)" ;

			double db_y0 = 2.2	; /* initial condition at t0		*/

			double []db_tspan = new double [2]	;
			db_tspan[0] = 0.2	; /* begin interval t0 = 0.2		*/
			db_tspan[1] = 6.5	; /* end interval, we choose this	*/

			/* declare mxArray variables */
			IntPtr mx_strfunc ;
			IntPtr mx_y0	= (IntPtr) null  ;
			IntPtr mx_tspan	= (IntPtr) null  ;

			IntPtr mx_length= (IntPtr) null  ;
			IntPtr mx_t		= (IntPtr) null  ;
			IntPtr mx_y		= (IntPtr) null  ;

			IntPtr mx_dum01	= (IntPtr) null  ;
			IntPtr mx_dum02	= (IntPtr) null  ;

			/* mx_tspan is a column vector when using in the following function */
			/* convert Cs double to mxArray */
			mx_strfunc = MatlabCSharp.mxCreateString(strfunc)	; 

			mx_y0 = MatlabCSharp.double2mxArray_scalarReal(db_y0) ;
			mx_tspan = MatlabCSharp.double2mxArray_vectorRowReal(db_tspan) ;

			/* call an implemental function */
			mlfMyode45firstorder(2, ref mx_t, ref mx_y, mx_strfunc, mx_tspan, mx_y0);

			/* convert back to Cs double  */
			
			double[] db_t = MatlabCSharp.mxArray2double_vectorReal(mx_t) ;
			double[] db_y = MatlabCSharp.mxArray2double_vectorReal(mx_y) ;

			Console.WriteLine("The column of time") ;
			MatlabCSharp.printVector(db_t) ;

			Console.WriteLine("The column of the function values y") ;
			MatlabCSharp.printVector(db_y) ;

		}


		/* **************************** */
		public void FirstOrderGetParticularValues()  
		{
			/* Calculating first order ODE */ 
			String strfunc = "6.4*t.^2 - 3.8*t*y" ;
			double db_y0= 1.24  ; /* initial condition at t0            */

			double[] db_tspan = new double [4]  ;
			db_tspan[0] = 0.15  ; /* begin interval t0 = 0.15           */
			db_tspan[1] = 0.2   ; /* choose a particular time t = 0.2   */
			db_tspan[2] = 2.6   ; /* choose a particular time t = 2.6   */
			db_tspan[3] = 5.0   ; /* choose a particular time t = 5.0   */

			/*declare mxArray variables */
			IntPtr mx_strfunc ;
			IntPtr mx_y0	= (IntPtr) null  ;
			IntPtr mx_tspan	= (IntPtr) null  ;

			IntPtr mx_t		= (IntPtr) null  ;
			IntPtr mx_y		= (IntPtr) null  ;

			/* mx_tspan is a column vector when using in the following function */
			/* convert Cs double to mxArray */
			mx_strfunc = MatlabCSharp.mxCreateString(strfunc)	; 

			mx_y0 = MatlabCSharp.double2mxArray_scalarReal(db_y0) ;
			mx_tspan = MatlabCSharp.double2mxArray_vectorColumnReal(db_tspan) ;

			/* call an implemental function */
			mlfMyode45firstorder(2, ref mx_t, ref mx_y, mx_strfunc, mx_tspan, mx_y0);

			/* convert back to Cs double  */
			double[] db_t = MatlabCSharp.mxArray2double_vectorReal(mx_t) ;
			double[] db_y = MatlabCSharp.mxArray2double_vectorReal(mx_y) ;

			Console.WriteLine("The column of time") ;
			MatlabCSharp.printVector(db_t) ;

			Console.WriteLine("The column of the function values y" ) ;
			MatlabCSharp.printVector(db_y) ;

		}

	/* **************************** */
		public void SecondOrder()  
		{
			/* Calculating second order ODE */
			String strfunc = "cos(3*t) + 2*yprime + 6*y" ;

			double[] db_ybc = new double [2]	; /* y boundary conditions			*/
			db_ybc[0]  = 0.2	; /* initial condition of y  at t0	*/
			db_ybc[1]  = 1.1	; /* initial condition of y' at t0	*/

			double[] db_tspan = new double [2]	;
			db_tspan[0] = 1.2	; /* begin interval t0 = 1.2		*/
			db_tspan[1] = 2.5	; /* end interval, we choose this	*/

			/* declare mxArray variables */
			IntPtr mx_strfunc ;
			IntPtr mx_ybc	= (IntPtr) null  ;
			IntPtr mx_tspan	= (IntPtr) null  ;

			IntPtr mx_t		= (IntPtr) null  ;
			IntPtr mx_y		= (IntPtr) null  ;

			/* convert Cs double to mxArray */
			mx_strfunc	= MatlabCSharp.mxCreateString(strfunc)				; 
			mx_ybc		= MatlabCSharp.double2mxArray_vectorRowReal(db_ybc)	;
			mx_tspan	= MatlabCSharp.double2mxArray_vectorRowReal(db_tspan)	;

			/* call an implemental function */
			mlfMyode45secondorder(2, ref mx_t, ref mx_y, mx_strfunc, mx_tspan, mx_ybc);

			/* convert back to Cs double  */
			double[]  db_t = MatlabCSharp.mxArray2double_vectorReal(mx_t) ;
			double[,] db_y = MatlabCSharp.mxArray2double_matrixReal(mx_y) ;

			Console.WriteLine("The column of time") ;
			MatlabCSharp.printVector(db_t) ;

			Console.WriteLine("The column of the function values y :") ;
			/* first column of the matrix y */
			int i ;
			int aLength = db_t.Length ;
			for (i=0; i<aLength; i++) 
			{
				Console.Write("{0} ", db_y.GetValue(i,0).ToString() ) ;
				Console.WriteLine() ;
			}

			Console.WriteLine("The column of the first derivative y' :") ;
			/* second column of the matrix y */
			for (i=0; i<aLength; i++)
			{
				Console.Write("{0} ", db_y.GetValue(i,1).ToString() ) ;
				Console.WriteLine() ;
			}

		}

	} // end class
}

⌨️ 快捷键说明

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