Matlab RTW&Engine 建模仿真实验报告(3)

2019-05-26 23:11

北京航空航天大学研究生实验报告

图 11

图 12

结果分析:VC程序和Simulink得到的仿真波形一致,说明通过VC程序可以操纵MATLAB Engine引擎得到准确的仿真结果。仿真时可配置仿真步长和仿真时间,不过仿真速度较慢。

6 实验总结

实验过程中,不但对MATLAB RTW和MATLAB Engine的调用方法有了深入的学习,同时,对MATLAB中的一些操作和执行语句有了一定的了解,更是锻炼了自己的编程能力。 在解决问题的过程中对基于vc++ .net的编程进行了学习,为今后的工作打下了基础。通过MATLAB RTW及Engine工具以两种不同的方式在Visual C++环境中对Simulink模型进行集成和调用,准确、通用、快速地建立复杂系统仿真模型,这在以后的科研及工程实践中会有很大的帮助。

7附:程序主要代码

(1) 基于MATLAB RTW的建模与仿真

“Simulate”按钮的响应函数:

System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) {

int i; double t=0; cw();//模型注册 MdlInitializeSizes(); MdlInitializeSampleTimes(); MdlStart();//启动模型 //获得输入参数

cw_U.In_fAAx=System::Decimal::ToDouble(numericUpDown1->Value); cw_U.In_fAAy=System::Decimal::ToDouble(numericUpDown2->Value); cw_U.In_fAAz=System::Decimal::ToDouble(numericUpDown3->Value); cw_U.In_Omega=System::Decimal::ToDouble(numericUpDown4->Value); cw_U.In_Cita=System::Decimal::ToDouble(numericUpDown5->Value); cw_U.In_Fi=System::Decimal::ToDouble(numericUpDown6->Value); progressBar1->Value=0; PointF tempPt1=Point(23,90);

- 10 -

北京航空航天大学研究生实验报告

PointF tempPt2=Point(23,90); PointF tempPt3=Point(23,90); PointF tempPt4=Point(23,280); PointF tempPt5=Point(23,280);

PointF tempPt6=Point(23,280); //设置画笔 Pen^ myPen1=gcnew Pen(Color::Blue,1.0f); Pen^ myPen2=gcnew Pen(Color::Red,1.0f); Pen^ myPen3=gcnew Pen(Color::LightGreen,1.0f); Graphics^ gs=this->pictureBox1->CreateGraphics(); pictureBox1->Refresh(); for (i = 0; i < 3000; i++) { MdlOutputs(0);//产生输出 MdlUpdate(0);//更新状态

cw_derivatives();//连续模型导数 t = t + 0.01;//时间推进

progressBar1->PerformStep();//更新进度条

PointF endPt1=Point(t*10+23,90-(cw_Y.Out_L1*210)); PointF endPt2=Point(t*10+23,90-(cw_Y.Out_L2*210)); PointF endPt3=Point(t*10+23,90-(cw_Y.Out_L3*210)); PointF endPt4=Point(t*10+23,280-(cw_Y.Out_L4*210)); PointF endPt5=Point(t*10+23,280-(cw_Y.Out_L5*210));

PointF endPt6=Point(t*10+23,280-(cw_Y.Out_L6*210)); //绘制图形

gs->DrawLine(myPen1,tempPt1,endPt1); gs->DrawLine(myPen2,tempPt2,endPt2); gs->DrawLine(myPen3,tempPt3,endPt3); gs->DrawLine(myPen1,tempPt4,endPt4); gs->DrawLine(myPen2,tempPt5,endPt5); gs->DrawLine(myPen3,tempPt6,endPt6); tempPt1=endPt1; tempPt2=endPt2; tempPt3=endPt3; tempPt4=endPt4; tempPt5=endPt5; tempPt6=endPt6;

}

MdlTerminate();//模型终止 }

(2) 基于MATLAB Engine的建模与仿真

“执行”按钮的响应函数:

System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

- 11 -

北京航空航天大学研究生实验报告

int i;

String^ module_path; String^ module_name; String^ temp; Decimal step,time;

module_path=textBox1->Text;//模型路径 module_name=textBox2->Text;//模型名称 step=numericUpDown7->Value;//仿真步长 time=numericUpDown8->Value;//仿真时间 temp=\+textBox1->Text+\;

engEvalString(ep,(char*)(void*)Marshal::StringToHGlobalAnsi(temp));//进入模型所在目录 engEvalString(ep,\);//对模型运行所需变量初始化

temp=\+int(numericUpDown8->Value/numericUpDown7->Value+1)+\;

engEvalString(ep,(char*)(void*)Marshal::StringToHGlobalAnsi(temp));//生成一个矩阵用于存

储模型的输出结果

engEvalString(ep,\);//设置仿真的开始时刻 engEvalString(ep,\);

engEvalString(ep,\);//载入模型参数 progressBar2->Value=0;

progressBar2->Maximum=int(numericUpDown8->Value/numericUpDown7->Value); for(i=0;iValue/numericUpDown7->Value+1);i++) {

temp=\+textBox2->Text+\+step.ToString()+\+step.ToString()+\;

engEvalString(ep,(char*)(void*)Marshal::StringToHGlobalAnsi(temp));//调用本地节点

的SIMULINK,将模型运行的逻辑时间推进.1s }

}

/////////画出模型输出///////////

temp=\+Decimal(numericUpDown7->Value)+\+int(numericUpDown8->Value)+\; engEvalString(ep,(char*)(void*)Marshal::StringToHGlobalAnsi(temp)); engEvalString(ep,\); engEvalString(ep,\); engEvalString(ep,\);

engEvalString(ep,\); engEvalString(ep,\); engEvalString(ep,\); engEvalString(ep,\);

engEvalString(ep,\);

engEvalString(ep,\);//记录模型输出 engEvalString(ep,\);

engEvalString(ep,\);//状态变量迭代,将本次时间步结束时系统 //的中断状态记录下来,作为下一个时间步运行的初始状态 progressBar2->PerformStep();//更新进度条

- 12 -


Matlab RTW&Engine 建模仿真实验报告(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:物业管理三介作业指导书 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: