找回密码
 注册
Simdroid-非首页
查看: 665|回复: 25

【求助】vb中调用MATLAB疑问!

[复制链接]
lilian198011 该用户已被删除
发表于 2005-12-6 11:48:36 | 显示全部楼层 |阅读模式 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-6 15:48:59 | 显示全部楼层 来自 上海黄浦区

Re:vb中调用MATLAB疑问!

Simdroid开发平台
据我想,可能是VPA这个命令,是maple中的,而目前MATLAB的编译不支持MAPLE的符号命令,能否去掉VPA这个命令,用DLL方式做个尝试?
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-6 17:38:04 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-7 08:56:00 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-7 09:19:08 | 显示全部楼层 来自 美国

Re:vb中调用MATLAB疑问!

是solve函数的问题,solve函数调用了maple,在matlab6.x里面maple是不能编译的,是无法做成脱离matlab的COM组件的。如果你允许不脱离matlab,可以在VB中用ActiveX自动化或者DDE的方式去调用。
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-7 09:34:23 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-7 14:18:44 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

It looks like you used symbolic toolbox in your m functions. In MATLAB 7, the compiler does not support symbolic toolbox.

Are you sure your running of mbuild did not give any error?

The following link gives all the toolboxes which can be compiled:
http://www.mathworks.com/products/compiler/compiler_support.html
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-7 14:52:13 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-7 20:42:02 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

说实话,matlab的符号运算功能是块鸡肋,勉强可以在matlab工作环境中应急用用,根本不值得当作解决实际问题的首选工具。

你是想在程序运行时间才告诉程序要解的非线性方程组吗?貌似Function handle就能实现,也能支持编译。建议楼主先看看有关Function handle的在线帮助。有问题的话大家来讨论。:I
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-8 09:12:17 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-8 09:49:37 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-8 09:54:53 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-8 09:56:46 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

Just thought of another approach. Since you used MATLAB Builder for COM, probably you can make use of events from MATLAB COM components.

MATLAB COM component supports IConnectionPointContainer interface. This means that this kind of components can interact with the client. If you can write Sink code in VB to respond to component's calling, then your problem would be resolved elegantly.

There is an example on how to sink component's event in C++ in my book. This is the most complex example in the book, which is also one of the special features of the book. (It took me a very long time to prepare that example, as far as I know, it is not talked else where in public domain. ) :I
发表于 2005-12-8 09:59:17 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

I am at work now, and have no access to MATLAB at this moment. Will look at it later if I get some time.
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-8 10:27:28 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-9 20:23:34 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-13 20:12:24 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

书接上回,偶说 matlab 的符运算是鸡肋,那么用什么来求解非线性方程组呢?答案是可以考虑优化工具箱的fsolve。

那么如何解决楼主的问题呢?也就是如何在运行时才告诉fsolve我们需要求解的方程组呢?答案是可以考虑用函数句柄(Function Handle)或者匿名函数(Anonymous Function)。从一个字符串变量构造所要求解的方程组,然后求解。在matlab 7.x中,这个技术支持编译。

下面是一个根据matlab中fsolve的在线帮助中提到的一个例子为例,示范如何从一个字符串变量构造一个匿名函数,然后调用fsolve求解该方程组。这个测试函数在matlab 7.1中编译成exe文件后执行也没有问题。剩下来的问题则是如何利用这种技术,再利用matlab buildre for com产生com组件,然后在vb中使用,如果有人练习成功后,能把例子程序贴出来共享的话,则感激不尽。偶本人等有空的时候会再尝试给出在c++中使用这种技术的例子。

不用管里面的myfunc,那是我测试函数句柄的。
  1. function testfsolve
  2. % TESTFSOLVE   
  3. % A test function to demonstrate how to use anonymous
  4. % function and fsolve. The equation to be solved can be constructed using a
  5. % string at runtime.
  6. %
  7. % Dong Weiguo
  8. % 13 Dec. 2005
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. str = '[a*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))]';
  11. str = vectorize(str);
  12. eval('a=2;');
  13. af = eval(sprintf('@(x)%s', str));
  14. % af([1 2])
  15. % myfun([1 2])
  16. x0 = [-5; -5];           % Make a starting guess at the solution
  17. options=optimset('Display','iter');   % Option to display output
  18. % [x,fval] = fsolve(@myfun,x0,options)  % Call optimizer
  19. [x,fval] = fsolve(af,x0,options)  % Call optimizer
  20. function F = myfun(x)
  21. F = [2*x(1) - x(2) - exp(-x(1));
  22.       -x(1) + 2*x(2) - exp(-x(2))];
复制代码

昨天有空试验了这个,不过SCIE给我提过的问题还是没时间弄,可能还得等两天。抱歉:I
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-14 20:47:24 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2005-12-14 21:04:00 | 显示全部楼层 来自 新加坡

Re:vb中调用MATLAB疑问!

抱歉,不懂VB。

上面的例子只是示范了如何从一个字符串构造匿名函数,以实现你想要得功能。

虽然不懂vb,但是觉得
  1. temp1 = "'[2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))]'"
复制代码
应该为
  1. temp1 = "[2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))]"
复制代码

关于初值数组x1的赋值好像是对的。
lilian198011 该用户已被删除
 楼主| 发表于 2005-12-14 21:34:53 | 显示全部楼层 来自 四川绵阳
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-4-28 21:55 , Processed in 0.056748 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表