求助:matlab diff()的一个问题
楼主 · 1 楼
我这里也运行不了啊, 
2 楼
可能是在你的电脑上有些软件包已经被删除了,所以你定义了函数还不能运行,应该是这样吧
117751.下面是老师写的一个文件,老师的电脑上市可以用的
它是用来得到taylor多项式的
function out=mytaylor(fkn,x0,n)
%
%
% out=mytaylor(fkn,x0,n)
%
%returns the n:th Taylor polynomial for fkn about x0.
%
%EX:
% >> mytaylor('sin(x)',0,6)
%
%returns
% out = x - 0.16667*x.^3 + 0.0083333*x.^5
%
% which is a string. To find the value of out at x=b use
%
% >> x=b;
% >> eval(out)
%
% Written by Sasan Gooran
% Friday, 28th January, 00:15
%
%*************************************************
x=x0;
testak=0;
if eval(fkn)~=0
out=[num2str(eval(fkn))];
else
out=[];
testak=1;
end;
if x0>0
s=['(x-',num2str(x0),')'];
elseif x0<0
s=['(x+',num2str(x0),')'];
else
s='x';
end;
fkd=fkn;
d=eval(fkn);
for i=1:n
fkd=diff(fkd);
oo(i)=d;
d=eval(fkd)/fac(i);
if d~=0
if i~=1
ii=['.^',num2str(i)];
else
ii=[];
end;
if d>0
dd=num2str(d);
sig=' + ';
z='*';
else
dd=num2str(-d);
sig=' - ';
z='*';
end;
if abs(d)==1
dd=[];
z=[];
end;
if (((i==1) & (testak==1)) | sum(abs(oo))==0)
sig=[];
end;
out=[out,sig,dd,z,s,ii];
end;
end;
if (sum(abs(oo))==0 & d==0)
out=0;
end;
%disp(out)
2.但是在我的电脑上运行会出错:
>> mytaylor('sin(x)',0,6)
??? Function 'eval' is not defined for values of class 'double'.
Error in ==> eval at 44
[varargout{1:nargout}] = builtin('eval', varargin{:});
Error in ==> mytaylor at 49
d=eval(fkd)/fac(i);
3.我调试的时候发现应该是在diff()上有问题
diff()得不到正确的导数值
比如:
>> x = 0:0.1:1;
y = diff(sin(x));
是可以得到一个粗略的解
y = Columns 1 through 8
0.0998 0.0988 0.0969 0.0939 0.0900 0.0852 0.0796 0.0731
Columns 9 through
10
0.0660 0.0581
但是老师的文件是通过字符串来计算的,例如:
f = 'sin(x)'
x = 0:0.1:1;
y = diff(f)
f =sin(x)
y = -
10 5 -70 80 -79
所以导致,不正确
但是我不知道到底为什么在我的电脑上不能运行成功,老师的却可以,是不是我的操作有问题?
谢谢大家帮我解释解释!!
117751.下面是老师写的一个文件,老师的电脑上市可以用的
它是用来得到taylor多项式的
function out=mytaylor(fkn,x0,n)
%
%
% out=mytaylor(fkn,x0,n)
%
%returns the n:th Taylor polynomial for fkn about x0.
%
%EX:
% >> mytaylor('sin(x)',0,6)
%
%returns
% out = x - 0.16667*x.^3 + 0.0083333*x.^5
%
% which is a string. To find the value of out at x=b use
%
% >> x=b;
% >> eval(out)
%
% Written by Sasan Gooran
% Friday, 28th January, 00:15
%
%*************************************************
x=x0;
testak=0;
if eval(fkn)~=0
out=[num2str(eval(fkn))];
else
out=[];
testak=1;
end;
if x0>0
s=['(x-',num2str(x0),')'];
elseif x0<0
s=['(x+',num2str(x0),')'];
else
s='x';
end;
fkd=fkn;
d=eval(fkn);
for i=1:n
fkd=diff(fkd);
oo(i)=d;
d=eval(fkd)/fac(i);
if d~=0
if i~=1
ii=['.^',num2str(i)];
else
ii=[];
end;
if d>0
dd=num2str(d);
sig=' + ';
z='*';
else
dd=num2str(-d);
sig=' - ';
z='*';
end;
if abs(d)==1
dd=[];
z=[];
end;
if (((i==1) & (testak==1)) | sum(abs(oo))==0)
sig=[];
end;
out=[out,sig,dd,z,s,ii];
end;
end;
if (sum(abs(oo))==0 & d==0)
out=0;
end;
%disp(out)
2.但是在我的电脑上运行会出错:
>> mytaylor('sin(x)',0,6)
??? Function 'eval' is not defined for values of class 'double'.
Error in ==> eval at 44
[varargout{1:nargout}] = builtin('eval', varargin{:});
Error in ==> mytaylor at 49
d=eval(fkd)/fac(i);
3.我调试的时候发现应该是在diff()上有问题
diff()得不到正确的导数值
比如:
>> x = 0:0.1:1;
y = diff(sin(x));
是可以得到一个粗略的解
y = Columns 1 through 8
0.0998 0.0988 0.0969 0.0939 0.0900 0.0852 0.0796 0.0731
Columns 9 through
10
0.0660 0.0581
但是老师的文件是通过字符串来计算的,例如:
f = 'sin(x)'
x = 0:0.1:1;
y = diff(f)
f =sin(x)
y = -
10 5 -70 80 -79
所以导致,不正确
但是我不知道到底为什么在我的电脑上不能运行成功,老师的却可以,是不是我的操作有问题?
谢谢大家帮我解释解释!!