matlab 运行下面的程序出现一些错误,请帮忙看看错误在哪里,谢谢.

问题描述:

matlab 运行下面的程序出现一些错误,请帮忙看看错误在哪里,谢谢.
程序
syms x n m
n=0;
m=1377*10^6;
x=0.1:0.01:6;
f(x)=(x.*19.1*300*300*pi.*(1-(sin(2*pi.*x))/(2*pi.*x))-n)/((3*x-1.25)*360);
plot(x,f(x))
运行出错
? Subscript indices must either be real positive integers or logicals.
程序
function h = myfun(x)
syms x n m f h g
h=f-g;
n=0;
m=1377*10^6;
f=(x*19.1*300*300*pi*(1-(sin(2*pi*x))/(2*pi*x))-n)/((3*x-1.25)*360);
g=pi*((2/3)*19.1*300*300*300*(sin(pi*x))^3-m)/(360*255*(sin(pi*x)+sin(pi*(1.25-2*x))));
保存为名为myfun的M文件
后来在命令窗口调用
x = fzero(@myfun,1)
出错
? Function 'isfinite' is not defined for values of class 'sym'.
Error in ==> isfinite at 19
[varargout{1:nargout}] = builtin('isfinite', varargin{:});
Error in ==> fzero at 309
elseif ~isfinite(fx) || ~isreal(fx)
1个回答 分类:综合 2014-12-01

问题解答:

我来补答
syms x n m
n=0;
m=1377*10^6;
x=0.1:0.01:6;
f=(x.*19.1*300*300*pi.*(1-(sin(2*pi.*x))/(2*pi.*x))-n)/((3*x-1.25)*360);
plot(x,f)
齐活
再问: 运行正常,麻烦看一下 我修改后调用FZERO函数还是出错看看哪里有问题,非常感谢。
再答: function h = myfun(x) % syms x n m f h g n=0; m=1377*10^6; f=(x*19.1*300*300*pi*(1-(sin(2*pi*x))/(2*pi*x))-n)/((3*x-1.25)*360); g=pi*((2/3)*19.1*300*300*300*(sin(pi*x))^3-m)/(360*255*(sin(pi*x)+sin(pi*(1.25-2*x)))); h=f-g; end 搞不懂你干嘛要弄syms那一行。你的h=f-g要放在最后,不然f,g都还没定义的就让他们相减,不报错才怪了。 MATLAB执行代码是按行的顺序来的,你的思维要适应,书上的公式要做适当调整。
 
 
展开全文阅读
剩余:2000
上一页:函数急