MATLAB - Taylor Series

edited March 2012 in Science & Tech
I have a question that asks me to compute the taylor series of cos(x) in Matlab with a function, like this:
function [cosx] = my_cos1(x,N)

Where x is the number we want to compute and N is the number of terms.

I have completed this question and now need to incorporate an optional 3rd parameter, 'eps' which is the accuracy. Where basically if the absolute value of one of the terms of the taylor series is less than the given accuracy, you don't add it on.
So I have everything to do with how to add an optional parameter at the top, but don't know how to write the code for the eps part. So this is what I have so far:

function tayexp=taylorcos(varargin)
K=length(varargin);
if K>3
disp('Too many parameters!')
else
if K<2
disp('Not enough of parameters')
else
x=varargin{1};
N=varargin{2};
if K==2
tayexp=my_cos1(x,N);
else
eps=varargin{3};
format long
r = 0;
for i = 0:N-1
t=((((-1)^i) * (x^(2*i)))/factorial(2*i));
if abs(t)>eps
r=r+t;
end
end
tayexp=r;
end
end
end
end


Any help greatly appreciated!

Comments

  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited March 2012
    I'm thinking that between this MATLAB link and this TAMU course instruction PDF, you should be able to figure it out. If not, one of the MATLAB experts may come along and offer better guidance.

    if abs(t)>eps
    r=r+t;

    This seems like you already accomplish what you are asking about. You don't do anything when abs(t) < eps. What am I missing? I don't know MATLAB, but I do know algorithm and coding design, so maybe I can help tease out the answer.
  • I'm not sure what I'm missing either to be honest... But when I type in numbers, the answer doesn't look right! I get things like -35 when I ask it to compute taylorcos(9, 3, 0.01), which doesn't seem right.
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited March 2012
    Your formula

    tayexp=mycos1(x,N);

    doesn't take in the .01 value. Is that correct?

    Can you provide the matlab code in a tabbed format (so I can follow it better)? For example, wrap your tabbed code with < code> and </ code> (remove the space before code in both)

    function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2<br /> disp('Not enough of parameters') else x=varargin{1}; N=varargin{2};



    The ending also confused me, because you create a function (tayexp) then set it to something else that is not defined (my_cos1()) and then set it again to r.
  • Yeah the original my_cos1 code doesn't take in anything to do with accuracy as that was only part 1 of the whole question.

    function tayexp=taylorcos(varargin)
    K=length(varargin);
    if K>3
    disp('Too many parameters!')
    else
    if K<2</code>
    disp('Not enough of parameters')
    else
    x=varargin{1};
    N=varargin{2};
    if K==2
    tayexp=my_cos1(x,N);
    else
    eps=varargin{3};
    format long
    r = 0;
    for i = 0:N-1
    t=((((-1)^i) * (x^(2*i)))/factorial(2*i));
    if abs(t)>eps
    r=r+t;
    end
    end
    tayexp=r;
    end
    end
    end
    end
  • PacifistoPacifisto Turnip Extraordinaire Michigan Icrontian
    Look what I made! It's got tabs now!

    function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2 disp('Not enough of parameters') else x=varargin{1}; N=varargin{2}; if K==2 tayexp=my_cos1(x,N); else eps=varargin{3}; format long r = 0; for i = 0:N-1 t=((((-1)^i) * (x^(2*i)))/factorial(2*i)); if abs(t)>eps r=r+t; end end tayexp=r; end end end end


    Can you not just add the third parameter to my_cos1 and replace whatever instance of 0.1 you have in there with eps? So you would call either my_cos1(x,N,0.1) or my_cos1(x,N,eps) depending on how many arguments you have.
  • Yeah but eps has to be an optional parameter so it works whether they type a number in for eps or not...
  • PacifistoPacifisto Turnip Extraordinaire Michigan Icrontian
    Does the user ever manually call my_cos(x,N)? Because if not, and they're just calling taylorcos, you can "hide" the extra parameter. Like this:

    function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K
  • Yes I understand this, however it is the other bit I am stuck with, not the optional parameters... If the absolute value of one of the terms of the taylor series is less than the given accuracy, you don't add it on.
  • PacifistoPacifisto Turnip Extraordinaire Michigan Icrontian
    edited March 2012
    Oh, I see. Somehow I thought you were already doing something of the type and just had to make the value variable.

    Looks like that second post of yours happened while I was formatting your code and thinking about the answer. Whoops!
  • Yeah I was just using 0.1 as an example, in theory it could be any real number. Thanks anyway.
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    Okay, I follow what you are doing for the first part (and your program works when you input only two, correct?). For the second part (with eps), the way I usually debugged the sort of behavior you are talking about was to insert checks into my code, so that it would print out the variables as it went through iterations of a loop.

    Is the "tayexp=r;" actually in the right spot? It looks like it will evaluate that statement for even when k=2, but it could be a matlab quirk.

    It looks like right now, if you have 3 variables, it goes into the loop, will run N-1 times, adding to r whenever abs(t)>eps and doing nothing for the rest of the loop if abs(t)<eps (by omission, maybe you should break it if that case occurs), then you set tayexp=r (though that seems strange to me ... setting the value of a function to a variable ... I guess that way it always "evaluates" to one value, right?).
  • Yeah, in the notes we were given it basically says whatever you call your function on the left hand side of the equals sign, you have to call it back later on and make it equal something. It doesn't make much sense to me either to be honest! So you think I should experiment with moving the "tayexp=r" to other places in the script?
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    No, actually, I think it was just one tab short from Pacifisto, so the tayexp=r is in the correct place assuming you are printing tayexp somewhere later on. Still thinking
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited March 2012
    function tayexp=taylorcos(varargin) K=length(varargin); if K>3 disp('Too many parameters!') else if K<2 disp('Not enough of parameters') else x=varargin{1}; N=varargin{2}; if K==2 tayexp=my_cos1(x,N); else eps=varargin{3}; format long r = 0; for i = 0:N-1 t=((((-1)^i) * (x^(2*i)))/factorial(2*i)); if abs(t)>eps r=r+t; end end tayexp=r; end end end

    Okay, the algorithm looks good (i.e. if abs(t)>eps, we add t to our running total, if abs(t)<=eps, we do nothing and continue evaluating the for loop), though you might want to just break the loop from further processing if you ever qualify the second scenario.

    The only thing I could see that looked bad to me was that it appears you have an extra end following tayexp=r (see my formatted code above). I use notepad++ as my editor to make stuff like this easy to see, but the matlab editor may make it look pretty too.
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited March 2012
    Oh and to add, one way to figure it out would be to add debugging checks into your code like outputting values to a txt file or live debug window where you can control the flow of the program (i.e. if you can force it to step through and show you the value of each variable while it is going through)

    Also, does "end" behave like a close parenthesis to the statement above it? for example
    for i<4 ( if abs(t)>eps i++; )
  • TushonTushon I'm scared, Coach Alexandria, VA Icrontian
    edited March 2012
    You could also lose one level of code by doing this (if possible)

    if 1
Sign In or Register to comment.