Matlab for Calculus 2
1 view (last 30 days)
Show older comments
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:
0 Comments
Answers (1)
Joseph Cheng
on 15 Apr 2014
Edited: Joseph Cheng
on 15 Apr 2014
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 Comments
Alberto
on 15 Apr 2014
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
on 15 Apr 2014
ln(0) is often returned as -infinity by software packages, as it is in MATLAB.
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!