To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
julianne
New to the neighborhood
julianne
1 Posts

MatLab help - Taylor Series

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??
shwaip
elaborate bot
shwaip
5,730 Posts

Re: MatLab help please!

n is an array of (mostly) non-integers. factorial(n) only works on integers, iirc.
Kyle
dormant icrontic veteran
Kyle
98 Posts

Re: MatLab help please!

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):


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.
Similar Threads
Thread Thread Starter Forum Replies Last Post
Solving multiple equations using Matlab avi2112ben Matlab Help 3 22 Feb 2008 8:58pm
MatLab problem - inline function zero finder PGolasze Matlab Help 4 10 May 2007 12:38am
Need matlab help urgently shermaine Matlab Help 3 10 Nov 2006 1:00am
MATLAB code help please.... jennifer22cute Matlab Help 0 18 Sep 2006 8:53pm
MATLAB help= jpeg to avi dalisay Web & Digital Media 0 13 May 2006 5:03pm

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 4:24pm (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.