亡羊补牢
2015-01-12, 13:17
本人菜鸟刚在学习《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
大家见笑之余,能给我讲解下原因吗!谢谢了
例 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
大家见笑之余,能给我讲解下原因吗!谢谢了