我想编写一个m-fi文件function demoTaylorlog(x0,dx)
来完成等效但是f(x) = ln(x)
的泰勒系列表示。
这就是我所拥有的,但它不起作用。我不确定它是否是正确的代码。我该如何解决?
function demoTaylorlog(x0,dx)
% demoTaylor Taylor Series approximations for f(x) = 1/(1-x)
%Synopsis:
% demoTaylorlog(x0,dx)
% Input: x0 = (optional) point about which the Taylor Series expansion is
% made. Default: x0 = 1.6;
% dx = (optional) size of neighborhood over which the expansion
% is evaluated. Default: dx = 0.8
% Output: a plot of f(x) and its Taylor Series approximations
if nargin<2, x0 = 1.6; dx = 0.8; end
x = linspace(x0-dx/2,x0+dx/2,20);
% x-values at which f(x) is evaluated
f(x)= log(x);
% Exact f(x); notice the array operator
h = x - x0;
% Avoid recomputing intermediate values,
t = 1/(1-x0);
% h and t p1x = t*ones(size(x)) + h*t^2;
% First order Taylor polynomial p2x = p1x+ (h.^2)*t^3;
% Second order " " " p3x = p2x + (h.^3)*t^4;
% Third
plot(x,fx,'-',x,p1x,'o-',x,p2x,'^-',x,p3x,'s-');
legend('exact','P_1(x)','P_2(x)','P_3(x)',4);
xlabel('x');
ylabel('Approximations to f(x) = 1/(1-x)');
end
答案 0 :(得分:1)
在声明中f(x)= log(x);
f
是一个向量,log
是一个函数。假设您的x
向量为[0.12 0.24 0.36]
,则带有错误的语句相当于:
f(0.12) = log(0.12);
f(0.24) = log(0.24);
f(0.36) = log(0.36);
但如果f
是向量,则f(0.12)
的赋值没有意义,因为.12不是正整数或逻辑值(如错误所示)。
你应该写f = log(x);