0
我想寫一個等效的模擬文件function demoTaylorlog(x0,dx)
,但是對於泰勒級數表示f(x) = ln(x)
。 這是我的,但它不起作用。我不確定它是否是正確的代碼。我應該如何解決它?泰勒級數近似當系列在第一,第二和第三項之後被截斷時,在MATLAB中
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
什麼「不起作用」是什麼意思? – ead
我會得到這個錯誤:下標索引必須是真正的正整數或邏輯。 demoTaylorlog中的錯誤(第18行) f(x)= log(x); – riva
通過提供樣本輸入和您的預期輸出來證明什麼不起作用是有幫助的。請參閱http://www.stackoverflow.com/help/mcve –