yuanping09 发表于 2009-10-24 11:24

用fsolve解方程出现的问题?

想用fsolve函数解方程,以前没有用过这个函数,所以就把help里的一个例子在matlab页面运行了,可是出现了错误。
例子:function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
      -x(1) + 2*x(2) - exp(-x(2))];
      x0 = [-5; -5];                                  % Make a starting guess at the solution
   options=optimset('Display','iter');   % Option to display output
= fsolve(@myfun,x0,options)

错误:??? function F = myfun(x)
    |
Error: Function definitions are not permitted at the prompt or in scripts。
这是怎么回事啊,是版本的问题吗?我用的是7.0.
请大家知道的给指点一下吧,谢了!

ChaChing 发表于 2009-10-24 11:35

Ref : 7F, 常见的程序出错问题整理 (eight)
http://forum.vibunion.com/forum/thread-46001-1-1.html

VibrationMaster 发表于 2009-10-24 19:50

"例子:function F = myfun(x)" 第一行的“例子:”是不是也被你考进去了?

ChaChing 发表于 2009-10-24 20:23

回复 板凳 VibrationMaster 的帖子

应该不是的, 应该是楼主直接复制至Command window!
本想让楼主自己看资料找答案的!:loveliness:

yuanping09 发表于 2009-10-25 17:47

回复 板凳 VibrationMaster 的帖子

“例子”没有拷进去,呵呵

VibrationMaster 发表于 2009-10-25 20:28

在>>状态下只能执行命令,比如myfun(3)
你现在把一个函数定义贴倒了>>,它应该在一个独立的M文件中
然后在>>只执行myfun(3)
页: [1]
查看完整版本: 用fsolve解方程出现的问题?