Matlab for Calculus 2

After running the above example, do the following to hand in: (Consult the MATLAB TA's if you have any questions.) Calculate the 2nd and 3rd degree Taylor polynomials for the function ln(1+x) about the point a=0 Plot these polynomials and the function on the interval -1<x<3
This is my code so far. Matlab keeps saying "undefined function 'ln'" or Unbalanced or unexpected parenthesis or bracket.
figure(1);clf; %open figure one and clear any plots
x=[-1: .01:3];
y=ln(1+x.); %the function
P2=-x^2/2; %the second-order Taylor polynomial (calculated by hand)
P3=x^3/3; %the 4th order Taylor polynomial
% annotating the graph is always important
figure(1);clf; %open and clear the 1st figure
plot(x,y,x,P2,'--',x,P4,'-.')
legend('1/(1+x^2)','P_2','P_3')
xlabel('x')
title('The function and the 2^{nd} and 4^{th} order Taylor Polynomials')
figure(2);clf; %open and clear the 2nd figure
plot(x,abs(y-P2),x,abs(y-P4),'--') % abs is the absolute value
legend('|1/(1+x^2)-P_2|','|1/(1+x^2)-P_4|')
xlabel('x')
title('Errors in the two Taylor Polynomials')
% Program output:

回答(1 个)

Joseph Cheng
Joseph Cheng 2014-4-15
编辑:Joseph Cheng 2014-4-15
Natural log is not represented in matlab by ln(). If you want to use ln you can:
ln = @(x)(log(x)); %at the start of your code...
or use log() instead of ln.
Additionally you need to get rid of the period after the x in the ln(1+x.)
You'll additionally need to use the element-by-element operation for you P2 and P3 by using .^ instead of ^ as you're not performing x^2 but the square of each element within x.

2 个评论

Another interesting fact, logarithmic function is not defined in zero, you should avoid the -1 if you want to evaluate log(1+x).
ln(0) is often returned as -infinity by software packages, as it is in MATLAB.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by