MatLab help - Taylor Series

edited October 2010 in Science & Tech
I have been trying to write a matlab function that will evaluate the taylor series of the ln(x) with the base point at x=1. then I need to use that taylor series to to evaluate the approximate value of f (x) for 0 ≤ x ≤ 2 when the Taylor series is truncated at two,three, and four terms. Perform these evaluations with a function m-file named taylor.m that is vectorized such that its input is an array of x values, and its output is the series value at each x for a specified series length. In other words if x = [0 : 0.02 : 2], then the
function taylor (x, N ) should give a vector whose elements are the value of the Taylor series truncated at N terms when it is evaluated at each element of x.

I have gotten started but keep getting error messages...and I don't know what is going wrong!

so far I have

function f=taylor(x,N)
%evaluate the taylor series for any function (specifically ln(x) for the
%homework problem)
n=[0:0.02:2];
%relative to x0=1
terms=(-1).^(j/2).*(x-1).^n./factorial(n);
f=sum(terms)

but am getting errors with the factorial..can anyone offer any help??

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    n is an array of (mostly) non-integers. factorial(n) only works on integers, iirc.
  • KyleKyle Lafayette, LA New
    edited January 2007
    Let's clarify a few things. First "x" is the array of inputs that you will plug into your Taylor series approximation. "N" is the number of Taylor series terms to use in your approximation (two, three, or four terms).

    Now look at the general form of the Taylor series (thanks Wikipedia):
    e0eae846ed9020ced087b7d18302a10a.png

    Instead of summing to infinity, you'll be summing up to "N", and "a" is the base point which equals 1. The last piece left in obtaining your approximation is f(n) which represents the n'th derivative of f(x) evaluated at "a". If your instructor didn't specify that your program should calculate the derivatives of ln(x), then don't do any extra work in doing so. Just work out the consecutive derivatives of ln(x) and plug in "a" for "x". I would create an array in the program to hold these values so you can piece together your approximation in a loop that repeats "N" times.

    shwaip is right in that your factorial function should not be using your array "n". The factorial will be evaluated with the values 0 to N where N is the number of terms in your Taylor series.

    If it were my program, I'd have two loops with one nested inside the other. The inner loop should go from 0 to N and sums up the terms of the Taylor approximation evaluated at "x". The outer loop should go from x = 0 to 2 with a step of 0.02.

    I hope that helps. :wink:
  • paniz68paniz68 Birmingham UK
    edited October 2010
    julianne wrote:
    I have been trying to write a matlab function that will evaluate the taylor series of the ln(x) with the base point at x=1. then I need to use that taylor series to to evaluate the approximate value of f (x) for 0 ≤ x ≤ 2 when the Taylor series is truncated at two,three, and four terms. Perform these evaluations with a function m-file named taylor.m that is vectorized such that its input is an array of x values, and its output is the series value at each x for a specified series length. In other words if x = [0 : 0.02 : 2], then the
    function taylor (x, N ) should give a vector whose elements are the value of the Taylor series truncated at N terms when it is evaluated at each element of x.

    I have gotten started but keep getting error messages...and I don't know what is going wrong!



    hi would u plz help me with one program of matlab it's finding pi with taylor series in Matlab:
    pi=3.1415...
    so far I have

    function f=taylor(x,N)
    %evaluate the taylor series for any function (specifically ln(x) for the
    %homework problem)
    n=[0:0.02:2];
    %relative to x0=1
    terms=(-1).^(j/2).*(x-1).^n./factorial(n);
    f=sum(terms)

    but am getting errors with the factorial..can anyone offer any help??
  • KyleKyle Lafayette, LA New
    edited October 2010
    paniz68, why did you just quote the original question?

    Man... I never post on Icrontic anymore. Thanks, email notifications, for bringing me back. But seeing this from nearly 4 years ago makes me feel like my intelligence has greatly declined. Kids, don't drink too much in college.
Sign In or Register to comment.