Taylor Series Figures of e^x

4 Ansichten (letzte 30 Tage)
Mohammed Alahmad
Mohammed Alahmad am 12 Feb. 2023
Bearbeitet: John D'Errico am 12 Feb. 2023
Consider the following expression:
f(x)= x^2*sin(x)+e^-x*cos^2(x):
Part 1
Plot f(x) varying 'x' from '-π' to '+π' for 100 points.
Using Taylor's series expansion for of degree 4, plot the graph with the above graph using hold on feature for same range of x.
Add Taylor series of degrees 7 and 10 to the same plot.
Compare the results. In the graph, add legend, title, axis titles. Add different line style, markers, colors. Choose appropriate font size for the graph.
Part 2
Integrate the function f(x) and plot the graph varying 'x' from '-π' to '+π' for 100 points.
Using Taylor's series expansion for integration of of degree 4. Plot the graph with the above graph using hold on feature for same range of x.
Add Taylor series of degrees 7 and 10 to the same plot.
Compare the results. In the graph, add legend, title, axis titles. Add different line style, markers, colors. Choose appropriate font size for the graph.
Here are my codes for both of part one&two and I just want to know whether it's correct or there are mistakes.
part one:
xx= linspace(-pi,pi, 100);
syms x
fx=(x*x^2)*sin(x*x)+exp(-x*x)*(cos(x*x)^2);
figure(1)
fplot(fx, [-pi pi], 'linewidth' ,2)
title('Original Signal')
xlabel('x')
ylabel('f(x)')
f= (x^2)*sin(x)+exp(-x)*(cos(x)^2);
T4 = taylor(f,x, 'Order',4);
figure (2)
fplot(fx, [-pi pi], 'linewidth' ,2)
hold on
fplot(T4,[-pi pi],'--')
T7 = taylor(f,x,'Order', 7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order', 10);
fplot(T10,[-pi pi],'-')
legend({'Degree = 4','Degree = 7', 'Degree = 10'})
xlabel('x')
ylabel('f(x)')
title('Taylor Series Approximation')
part two:
syms x
fx= (x^2)*sin(x)+exp(-x)*(cos(x)^2);
Ifx=int(fx);
figure(1)
fplot(fx, [-pi pi], 'linewidth' ,2)
title('Original Signal')
xlabel('x')
ylabel('f(x)')
T4 = taylor(f,x, 'Order',4);
figure (2)
fplot(T4,[-pi pi],'--')
hold on
T7 = taylor(f,x,'Order', 7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order', 10);
fplot(T10,[-pi pi],'-')
legend({'Degree = 4','Degree = 7', 'Degree = 10'})
xlabel('x')
ylabel('f(x)')
title('Taylor Series Approximation')
  2 Kommentare
John D'Errico
John D'Errico am 12 Feb. 2023
What do you think? Is this the function you were told to use?
fx=(x*x^2)*sin(x*x)+exp(-x*x)*(cos(x*x)^2);
Mohammed Alahmad
Mohammed Alahmad am 12 Feb. 2023
fx=(x^2)*sin(x)+exp(-x)*(cos(x)^2);
I've now replaced that line.
these are the two figures I've got :

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 12 Feb. 2023
Bearbeitet: John D'Errico am 12 Feb. 2023
Ok. Now that you have changed the function to one that actually looks like the desired expression... :)
My test would be one to see if the result makes sense. Always apply common sense to what you have done. (I could tell the story about how, as a student myself many millions of years ago, I computed the temperature in a heated bar for a problem in a linear algrebra class, and said the temperature would be something like -5000 degrees. I don't recall if that was degrees F or degrees C. Does it matter? My teacher was not impressed.) So apply common sense to what you have done. ALWAYS.
What does that mean here? If I look at the true curve in blue, that is what the Taylor series approximation should look like, if we used infinitely many terms, and could compute with infinite precision. But you need to understand that a Taylor series will often diverge for large deviations of x away from the origin. It may not even converge at all for some x. But the more terms you take, the better it will do. (There are some functions where that is not the case, where more terms will help.)
So what do I see in the plots you generated? The blue curve looks very much like the approximations for x between -1 and +1, but then things go completely to hell for larger absolute values of x. And that is completely expected, enough so that I'd say this conforms to my expectations for a Taylor series.
As long that is, as you fix fx, so that it is the true function. You have essentially this code:
syms x
fx= (x^2)*sin(x)+exp(-x)*(cos(x)^2); % I used f and fx as the same thing here
f= (x^2)*sin(x)+exp(-x)*(cos(x)^2);
T4 = taylor(f,x, 'Order',4);
fplot(fx, [-pi pi], 'linewidth' ,2)
hold on
fplot(T4,[-pi pi],'--')
T7 = taylor(f,x,'Order', 7);
fplot(T7,[-pi pi],'-o')
T10 = taylor(f,x, 'Order', 10);
fplot(T10,[-pi pi],'-')
legend('f','T4','T7','T10')
hold off
And they would all be correct. Again, as long as fx is fixed to be the same as f.
You would expect the higher order approximations to be a little better, and do a little better as you go a little further out. But not too far, as that is a moderately messy function.
So now lets apply common sense to that plot. Does it make sense?
Which curve peels off from the true function first? T4.
Which curve peels away next? A higher order series will do well a little further out. Logically, that should be T7. Is that not the case?
Finally, which is the curve that does best, for the longest distance away from 0? That is T10. Again, this matches with common sense. It fits my expectations perfectly about what should happen.
As well, you can even look at the way the curves deviate from the true function. A 4th degree polynomial approximation will have errors that will be dominated by an odd function. (THINK ABOUT IT! What term are you throwing away?) So all even degree truncated Taylor series will probably show deviations that will look like odd functions. And odd degree truncated Taylor series will typically deviate in an "even" way. What does that mean? (A caveat to the above rule is to look at the Taylor series for sin (x). It has ONLY odd terms in it. Conversely, the series for cos(x) only has even terms in it.)
What is the first term you dropped from a 4th degree truncated Taylor series? The x^5 term! How about the 10'th degree series? The dominant term in the error there is the x^11 term. But in the degree 7 truncated Taylor series, the first truncated term is the x^8 term. Now, go back and look at the plot we drew. Apply common sense in terms of our expectations of what should happen. Do our expectations hold up?
What does the polynomial term x^5 look like? x^8? x^11?
fplot(x^5,[-2,2])
x^5 and x^11 are odd powers of x. So as x--> inf, it goes to +inf. But as x--> -inf, x^5 approaches -inf. This is a characteristic of an odd function.
But even functions appoach +inf in both directions.
fplot(x^8,[-2,2])
So again, if we now look at the plot for T7, it deviates from the true function in a way that is exactly as I expect. The same applies to T10. It deviates as a polynomial that is dominated by an odd degree monomial would behave.
My point is, always apply common sense to what you have done. Does what you have done make sense? Had I done so in that long since completed linear algebra class, I would have had less egg on my face back then. ;-) (I did get an A anyway. And I learned a valuable lesson along the way.)

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by