matlab
 
Matlab is a tool for doing numerical computations with matrices and vectors. It can also display information graphically. The best way to learn what Matlab can do is to work through some examples at the computer. After reading the " getting started " section, you can use the tutorial below for this. 
Getting started 
Tutorial 
Matrices 
Vectors 
Systems of equations 
Loops: a bit of programming 
Graphing 
Functions of one variable 
Functions of two variables 
Getting started 
Here is a sample session with Matlab. Text in bold is what you type, ordinary text is what the computer "types." You should read this example, then imitate it at the computer. 
% matlab>> a = [ 1 2; 2 1 ]a =
     1     2
     2     1>> a*aans =     5     4
     4     5>> quit 16 flops.%In this example you started Matlab by (you guessed it) typing matlab. Then you defined matrix a and computed its square ("a times a"). Finally (having done enough work for one day) you quit Matlab. The tutorial below gives more examples of how to use Matlab. For best results, work them out using a computer: learn by doing! Matlab Tutorial 
Matrices 
To enter the matrix 
             1 2
             3 4and store it in a variable a, do this: 
       >> a = [ 1 2; 3 4 ]

解决方案 »

  1.   

    C++Builder调用Matlab
       
    Borland C++Builder是一种新颖的可视化编程语言。在工程应用中,我们一般用C++Builder语言编写应用程序,实现交互界面、数据采集和端口操作等,但C++Builder在数值处理分析和算法工具等方面,其效率远远低于Matlab语言。在准确方便地绘制数据图形方面,Matlab语言更具有无可比拟的优势。此外,Matlab还提供功能强大的工具箱。但Matlab的缺点是不能实现端口操作和实时控制。因此,若能将两者结合运用,实现优势互补,将获得极大的效益。
    本文结合实际介绍了应用Borland C++Builder3.0开发的Windos应用程序中,对Matlab的调用方法。
    一、C++Builder调用Matlab的实现方案
    1. 实现思路
    在高版本的Matlab中(如Matlab V4.2)提供了DDE接口,用户可以通过Windows的DDE通信基制实现外部调用。这种实现方式比较简单,但将增大主程序代码,影响运行速度。
    在Windows系统中,DLL是一种很特别的可执行文件,可以被多个Windows应用程序同时访问,具有固定的共享数据段。该数据段的数据在DLL被Windows下载前会一直保留在内存中,因此可以通过DLL实现用户程序与Matlab之间的数据传输和函数调用。
    具体地说,就是利用Matlab的32位动态连接库(DLL),生成相应的可以被C++Builder调用的DLL,用来提供二者之间的基本支撑环境。只需在用户程序中加载该DLL,即可实现其数据段的共享。然后在用户程序中操作DLL数据段的数据,并通过某种方式在用户程序中使Matlab执行该DLL,就可实现用户程序对Matlab的调用。其形式可以是混合编程或函数调用,非常方便而高效。
    2. 实现方式
    Matlab提供了可外部连接的DLL文件,通过将其转换为相应的Lib文件,并加以必要的设置,就可以在C++Builder中直接进行Matlab函数调用,实现C++ Builder语言与Matlab语言的混合编程。
    (1) 运行环境要求
    由于Matlab提供的是32位的DLL。其运行环境要求是Matlab V4.2或更高版本。C++Builder可以进行32位编程,这里我们采用的是V3.0版本。
    (2) C++Builder下LIB文件的生成
    Matlab提供的Def文件允许用户通过Implib命令生成相应的Lib文件。其命令格式为
    Implib ???.lib ???.def
    在<matlab>\extern\include目录下,提供了如下三个.Def文件:
    _libeng.def,_libmat.def,_libmx.def
    通过上述命令可以生成相应的三个Lib文件。这些Lib文碱中包含了可外部调用的Matlab函数的必要信息。
    二、实现计算和绘图
    为清楚起见,通过一个简单的CBuilder例程进行说明。该实例通过调用Matlab实现矩阵运算并绘制图形,来演示C++Builder对Matlab的调用。
    在C++Builder编辑环境中,建立一个新的窗体MyForm,并放置一个按钮Demo。将工程文件命名为Try.prj,其主函数为try.cpp。在主函数中,我们将使用一个实现Matlab调用的子函数DemoMatlab,作为按钮Demo的响应事件。其源代码如下:
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TMyForm *MyForm;
    __fastcall TMyForm::TMyForm(TComponent* Owner)
    : TForm(Owner)
    {
    }
    void __fastcall TMyForm::DemoClick(TObject *Sender)
    {
    DemoMatlab(); //演示Matlab语言调用
    }
    为了调用Matlab中的函数,必须进行必要的设置,将包含这些函数的文件加入工程文件Try.prj。以下是操作过程:
    1. 在头文件中加入Engine.h。其包含了启动Matlab调用和关闭的函数声明。
    2. 打开Project|Option...对话框,点击Directories/Conditionals。
    ● 在Include Path中,加入目录路径<matlab>\extern\include,该路径包含了engine.h和matlab.h等有用的头文件。
    ● 在Library Path中,加入<matlab>\bin和<matlab>\extern\include。这两个目录路径包含了可外部调用的DLL和LIB文件。
    3. 点选Project|Add to Project...对话框,加入如下库文件:
    _libeng.lib,_libmat.lib和_libmx.lib。
    在进行了这些必要的设置之后,我们就可以选用适当的函数来实现目标。 以下是子函数DemoMatlab的程序代码。
    void DemoMatlab
    {
    Engine *eng;
    //定义Matlab引擎
    char buffer[200]; //定义数据缓冲区
    int array[6]={1,2,3,4,5,6};
    mxArray *S = NULL, *T = NULL;
    engOpen(NULL); //打开MATLAB 引擎 ---1
    S= mxCreateDoubleMatrix(1,6, mxREAL);
    // 产生矩阵变量
    mxSetName(S, "S");
    memcpy((char*) mxGetPr(S),(char *) array, 6*sizeof(int));
    engPutArray(eng, S); //将变量X置入Matlab的工作空间
    engEvalString(eng, "T = S/S.^2;"); //计算
    engEvalString(eng, "plot(S, T);"); //绘制图形
    …… ……
    engOutputBuffer(eng, buffer, 200); //获取Matlab输出
    T = engGetArray(eng, "T"); //获得计算结果----2
    engClose(eng); //关闭Matlab引擎,结束调用
    mxDestroyArray(S); //释放变量
    mxDestroyArray(T);
    }
    若还需要执行其他功能和任务,那么按照上面介绍的方法,进行变量声明后,在1、2处加写需要的语句即可。
    当然,使用这种方法调用Matlab不能脱离Matlab环境的支撑。