User-defined functions/MAC series

edited November 2010 in Science & Tech
Ok, I need to: Write a function (f_macsine) that approximates sin(x), then write a program to call that function (fcall_macsine). For any value of x, I am to run the series sum until the ABSOLUTE VALUE of the true percent relativ error is < specified error.

The goal is to approximate sin(x) for x=[0:.2:2*pi] an I am supposed to this by using a loop in the calling program that calls the fn for each value of X.

The main problem I am having is I don't understand how to approximate sine before calling it. I am using a while loop but then realized "while" what? while true percent relative error is < specified error... but if I am supposed to get that in the calling program, how do I code the actual function?? PLease help, approximations are kicking my ass.

Here is what I have for my function:
function f_macsine(x)
global SE SINE_MAC
% F_MACSINE Approximate sine for any value of x
% Function F_MACSINE accepts user input for the value of x and uses the
% Maclaurin series to approximate the value of the sine function at this value.

%Define variables:
% x: value at which to approximate sine
% n: number of terms in the Mac series
% t_n: the number of terms in the Mac series
% sine_approx: the solution to the nth term Mac series
% se: the specified error 
x=input('Enter x:');
se=input('Enter specified error');
n=1;
t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1)
sine_approx=t_n
while t_n>se
n=n+1;
t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1);
sine_approx=sine_approx+t_n;
end



Thanks a lot for any help, I will appreciate it so much. I'm trying really hard to figure approximations out.

Comments

  • edited October 2010
    plllllleaaaaaseeee help.
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited November 2010
    You're correct that you should use a while loop for this. You've come upon one of the classic problems in introductory programming: how to enter a pre-test loop (while in MATLAB) in a sensible fashion. There are three common approaches to this problem:

    1) Run a copy of the code block inside the loop before you run the loop. In your case, the code block you posted followed by a definition of the error, with the while loop condition of error < error margin; then another copy of that code block inside the while loop.

    2) Put the chunk of code you posted inside a while loop and make the loop test the error as normal, but initialize the error prior to the loop with a large value to ensure the loop runs at least once.

    3) Create a new variable named something along the lines of finished, done, or converged and set it to 0. Make the loop test condition while(~done). Include in your loop an if statement: if error < error margin, done = 1.

    Each of these three approaches has its own set of advantages and disadvantages, but for your purposes any will do. As far as determining the actual error, as far as I know you the only way to do it is to compare against MATLAB's sin() function. Speaking of functions, the code block you posted is in script M format. Check the MATLAB help file for how to write function M files.

    I read your problem statement to say that your calling script needs to just call your function for all the values of X (for loop). You can pass the specified error as an additional argument to your function, but unless it's different for each X you'd be better served by hard-coding it as a constant in your function.
  • edited November 2010
    Here is an example. The code below computes value of sin(x) using the Taylor expansion of sin(x) and a tolerance value. Then, plots both internal sin(x) and approximate sin(x) (via function sine). As you can see in the lower graph, function starts to deviate after x = 5 when tolerance is 1.0.

    PS: You can use the code as you wish. I just wrote it as an example.
    function retval = sine (theta,tol) 
      retval = theta; 
      l = 1;
      lm = 1;
      lfact = 1;
      do
        oldval = retval;
        lm = -lm;
        l = l + 2;
        lfact = lfact*(l-1)*l;
        retval = retval + lm*((theta).^l)/lfact;
      until (abs (retval - oldval) < tol) 
    endfunction 
    
    t = linspace(0,2*pi,100);
    y1 = sin(t);
    y2 = sine(t,1);
    plot(t, y2, t, y1);
    

    picture1tc.png

    Uploaded with ImageShack.us
  • edited November 2010
    fuck. heres my function now.
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]function[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] f_macsine(x)
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]global[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] se sine_mac
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#228b22][FONT=Courier New][SIZE=2][COLOR=#228b22][FONT=Courier New][SIZE=2][COLOR=#228b22]% F_MACSINE Approximate sine for any value of x
    % Function F_MACSINE accepts user input for the value of x and uses the
    % Maclaurin series to approximate the value of the sine function at this
    % value.
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]n=1;
    t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1)
    sine_mac=t_n
    c=abs((sin(x)-t_n)/sin(x))
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] c>se
    n=n+1;
    t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1);
    sine_mac=sine_mac+t_n;
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]end
    

    heres my calling program:
    [FONT=Courier New][SIZE=2][COLOR=#228b22][FONT=Courier New][SIZE=2][COLOR=#228b22][FONT=Courier New][SIZE=2][COLOR=#228b22]% Define variables:
    % x: value at which to approximate sine
    % n: number of terms in the Mac series
    % t_n: the nth term in the Mac series
    % sine_mac: the solution to the nth term Mac series
    % se: the specified error
    % c: true percent relative error
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]global[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] se sine_mac[/COLOR]
    se=.01
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000] x=[0:.2:2*pi][/COLOR]
    sine_mac=f_macsine(x)
    [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]end
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]
    


    [/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
  • edited November 2010
    and heres the error message:
    >> fcall_macsine
    se =
    0.0100
    ??? Error using ==> f_macsine
    Too many output arguments.
    Error in ==> fcall_macsine at 11
    sine_mac=f_macsine(x)
    
    due tomorrow at 1. thanks a LOT for everyone who already helped and anyone who is going to.
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited November 2010
    Lots of stuff still wrong:
    1) Function header is incorrect. The format is function outputs = f_name(inputs). Currently your function doesn't return anything so sine_mac = f_macsine(x) isn't valid.
    2) Your outputs shouldn't be globalized.
    3) Your while loop doesn't recalculate the error after adding each term.
  • edited November 2010
    Looking at your code :hair: after I gave you the function. I have wasted time apparently :(
  • edited November 2010
    Man I know thats what you keep telling me but my assignment says I am required to globalize. Let me show you the exact assignment (please don't jump on me for posting HW assignment, just want these guys to see):


    Write a function, f_macsine, that will use the Maclaurin series to approximate sin(x). Write a main or calling program, fcall_macsine, that will call f_macsine. Document f_macsine and fcall_macsine fully. Include in fcall_macsine a preamble section “Definition of Functions” where you will give a very brief description of the 2 functions called in fcall_macsine. Pass the value of x from the main program to the function through the function input. Pass the acceptable specified error, se, and the series approximation, sine_mac, between the main program and the function through the global memory. You can hard-code se and x in fcall_macsine (no keyboard input required). So, the first two lines of your function will look like this:

    function f_macsine(x)
    global se sine_mac

    For any value of x, run the series sum until the absolute value of the true percent relative error (Lecture 8, slide 18) is less than the specified error, se. (If you forget abs, it won’t work right.) The goal for this lab is to approximate the sin(x) for x running from 0 to 2*pi in 0.2 increments with se = 0.01 . To accomplish this, include a loop in fcall_macsine that will call f_macsine once for each value of x. In fcall_macsine, write an annonymous plotting function, fplot, that will plot one curve with line type, marker type, and color specified along with line width and marker size. Use fplot to help generate a graph as shown below that will show the “right answer” sin(x) (black dashed line, width = 1) along with the series approximation at each x (red circle marker, size = 7). Include the grid, axis labels, legend, and title as shown.


    Sorry to have waste your time mirage, (and drasnor), I thought I took useful stuff from both but apparently not. I had a hard time understanding your variables and what they were mirage (thats on me).

    Anyway, I'm gonna work on it all morning till 1PM tomorrow and try to come out on top.

    Still, thanks for everything.
  • edited November 2010
    hamlet wrote:
    For any value of x, run the series sum until the absolute value of the true percent relative error (Lecture 8, slide 18) is less than the specified error, se. (If you forget abs, it won’t work right.)

    The slide its talking about says that
    "true percent relative error" = (true value -approximation)/true value
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited November 2010
    I don't know what to say; you can't call a function M-file with no output by assigning its nonexistent result to a variable.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited November 2010
    The best person to ask problem-specific questions on a class's lab is your lab instructor/ta. When I TA'd, I was happy to help students. Remember that you are paying their salary.

    That being said, you probably won't get an answer if you ask now, you should have gone to them for help first.
  • edited November 2010
    ive been asking my TA. thanks anyway.
  • edited November 2010
    is this closer for the function?
    function f_macsine(x)
    global se sine_mac
    n=1;
    t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1)
    sine_mac=t_n
    err=abs((sin(x)-t_n)/sin(x))
    while err>se
    n=n+1;
    t_n=(x^((2*(n-1))+1).*((-1)^(n-1)))/factorial((2*(n-1))+1);
    sine_mac=t_n
    sine_mac=sine_mac+t_n;
    err=abs((sin(x)-t_n)/sin(x))
    end
    
Sign In or Register to comment.