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일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2014년 4월 15일

댓글:

2014년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by