Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 工程数学软件 > MATLAB论坛
MATLAB论坛 一切MATLAB相关问题在此讨论。
回复
 
主题工具 显示模式
旧 2015-01-12, 13:17   #1
亡羊补牢
初级会员
 
注册日期: 2008-01-15
年龄: 45
帖子: 6
声望力: 0
亡羊补牢 正向着好的方向发展
默认 这样调用函数为什么运行不了,请大家帮我看看吧

本人菜鸟刚在学习《MATLAB R2014a 完全自学一本通》,书中的一个例子搞不明白,请大家帮我看看,指导我一下。
例 4_16,函数文件的简单运行实例。
创建M文件并命名为equation_solve.m。
利用M文件编辑器,在equation_solve.m文件中写入:
function [x1,x2] = equation_solve(a,b,c)
delt=b*b-4*a*c;
if delt<0
'There is no answer!!!'
elseif delt==0
'There is only one answer!!!'
x1=(-a+sqrt(delt))/2;
ans=x1
else
'There is two answers!!!'
x1=(-a+sqrt(delt))/2;
x2=(-a-sqrt(delt))/2;
ans=[x1 x2]
end

创建M文件并命名为ex4_16.m,调用equation_solve函数。
利用M文件编辑器,在equation_solve.m文件中写入:
[x1 x2]=equation_solve(1,2,4);
[x1 x2]=equation_solve(1,2,1);
[x1 x2]=equation_solve(1,2,-1);

运行总是出现错误如下:
>> ex4_16

ans =

There is no answer!!!

Error in equation_solve (line 2)
delt=b*b-4*a*c;

Output argument "x1" (and maybe others) not assigned during call to
"C:\Users\Administrator\Desktop\matlab\equation_solve.m>equation_solve".

Error in ex4_16 (line 1)
[x1 x2]=equation_solve(1,2,4);


我自己把ex4_16.m文件中的[x1 x2]全部去掉,
equation_solve(1,2,4);
equation_solve(1,2,1);
equation_solve(1,2,-1);
便能运行成功,得到和书上一样的结果如下:>> ex4_16

ans =

There is no answer!!!


ans =

There is only one answer!!!


ans =

-0.5000


ans =

There is two answers!!!


ans =

0.9142 -1.9142



大家见笑之余,能给我讲解下原因吗!谢谢了

此帖于 2015-01-12 13:39 被 亡羊补牢 编辑。
亡羊补牢 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码



所有时间均为北京时间。现在的时间是 12:41


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.