Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab taylor series script

  1. Feb 5, 2015 #1


    User Avatar

    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.

    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]);


    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'])
  2. jcsd
  3. Feb 7, 2015 #2


    User Avatar
    Homework Helper

    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.
  4. Feb 7, 2015 #3


    User Avatar
    Gold Member

    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://www.millersville.edu/~bikenaga/calculus/remainder-term/remainder-term.html [Broken]
    Last edited by a moderator: May 7, 2017
  5. Feb 7, 2015 #4


    User Avatar
    Homework Helper

    If you wanted to do this without the built in Taylor functions, you could define the derivatives of g:
    g(x)= 5sin(3x)
    and the taylor series is
    Often, the series is evaluated at x_0 = 0...which produces a pretty simple result for the sine function.
  6. Feb 7, 2015 #5


    User Avatar

    Thanks RUber, you are compltely correct, I read the problem statement a little more closely and releaized i wasnt do it correct.

    So ive tried working with this:


    n =[2 5 50]
    for i =1:720

    for k=1:1:50

    havent had any luck with it so far though
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook