Optimizing Taylor Series Approximations in Matlab for Trigonometric Functions

In summary, the script calculates f(x) = 5sin(3x) using the Taylor series with the number of terms n=2, 5, 50, without using the built-in sum function. The approximations along with the exact function for x=[-2π 2π] are plotted.
  • #1
NYK
27
0
I have been working on writing g a script file that will:

  1. Calculate f(x)=5sin(3x) using the Taylor series with the number of terms n=2, 5, 50, without using the built-in sum function. 
  2. Plot the three approximations along with the exact function for x=[-2π 2π]. 
  3. Plot the relative true error for each of the approximations 
  4. Calculate the value of sin(x) and the error for x=π and x=3π/2 for each of the approximations 
  5. How many terms are necessary for an error E<.000001?
I have been able to get as far as the third part of the question, any advice, tips or pointers are greatly appreciated!

I pasted the script I have so far bellow:

clear, clc, close all
%Define the limits, the original function and the Taylor series.
syms x

a = -2*pi:2*pi;

g = (5*sin(3*x));

T_2 = taylor(g, 'Order', 2);

T_5 = taylor(g, 'Order', 5);

T_50 = taylor(g, 'Order', 50);

z = (5*sin(3*a));%plot the original function and the three Taylor series.

fg=figure;
ax=axes;
ez1=plot(a,z, 'r--');
hold on
ez2=ezplot(char(T_2),[-2*pi, 2*pi]);
ez3=ezplot(char(T_5),[-2*pi, 2*pi]);
ez4=ezplot(char(T_50),[-2*pi, 2*pi]);

legend('5sin(3x)','T2','T5','T50')

set(ez2, 'color', [0 1 0])
set(ez3, 'color', [0 0 1])
set(ez4, 'color', [1 0 1])title(ax,['Graph of 5sin(3x) and taylor expansions T2, T5 and T50'])
 
Physics news on Phys.org
  • #2
for part 4, you should just evaluate the taylor series at the points mentioned and subtract that from the true value.
Once you have done that, you can set up a script to try different values for 'order' until you reach the accuracy needed.
 
  • Like
Likes NYK
  • #3
3. "Relative true error" to me just sounds like you're plotting the absolute difference between the approximations and the true values.

4. Pretty self explanatory, I'm with RUber on this one ^^.

5. Use the remainder term to figure this out. See the following links:

http://en.wikipedia.org/wiki/Taylor's_theorem#Motivation

http://www.millersville.edu/~bikenaga/calculus/remainder-term/remainder-term.html
 
Last edited by a moderator:
  • Like
Likes NYK
  • #4
If you wanted to do this without the built in Taylor functions, you could define the derivatives of g:
g(x)= 5sin(3x)
d(n,g(x))=5*3^n*sin(3x+n*pi/2)
and the taylor series is
(x-x_0)^n/n!*d(n,g(x_0)).
Often, the series is evaluated at x_0 = 0...which produces a pretty simple result for the sine function.
 
  • Like
Likes NYK
  • #5
Thanks RUber, you are compltely correct, I read the problem statement a little more closely and releaized i wasnt do it correct.

So I've tried working with this:

clear;clc

n =[2 5 50]
do=linspace(-2*pi,2*pi,720);
for i =1:720

for k=1:1:50
ns=2*k+1
T(i)=T(i)+5*(-1)^k*(3*do(i))^(ns)/factorial(ns);
end
endhavent had any luck with it so far though
 

1. What is a Taylor series?

A Taylor series is a mathematical representation of a function as an infinite sum of terms. It is used to approximate a function at a specific point by taking into account the function's derivatives at that point.

2. How can I use Matlab to compute Taylor series?

Matlab has a built-in function called "taylor" that can be used to compute a Taylor series. The syntax is taylor(f, x, n), where f is the function, x is the point of approximation, and n is the number of terms in the series.

3. Can I customize the number of terms in the Taylor series?

Yes, the number of terms can be specified as the third input argument in the "taylor" function. By default, Matlab calculates 10 terms in the series, but this can be changed to any desired number.

4. How can I plot the Taylor series using Matlab?

To plot the Taylor series, you can use the "ezplot" function in Matlab. The syntax is ezplot(taylor(f, x, n)), where f is the function, x is the point of approximation, and n is the number of terms in the series.

5. Can I use the Taylor series to approximate any function?

Yes, the Taylor series can be used to approximate any function as long as it is differentiable at the point of approximation. However, the accuracy of the approximation may vary depending on the properties of the function and the number of terms used in the series.

Similar threads

  • Introductory Physics Homework Help
Replies
5
Views
328
Replies
3
Views
709
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
27
Views
2K
Replies
3
Views
1K
  • Linear and Abstract Algebra
Replies
5
Views
2K
  • Calculus and Beyond Homework Help
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
7
Views
720
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
287
Back
Top