User-defined functions/MAC series
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:
Thanks a lot for any help, I will appreciate it so much. I'm trying really hard to figure approximations out.
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.
0
Comments
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.
PS: You can use the code as you wish. I just wrote it as an example.
Uploaded with ImageShack.us
heres my calling program:
[/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
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.
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.
The slide its talking about says that
"true percent relative error" = (true value -approximation)/true value
That being said, you probably won't get an answer if you ask now, you should have gone to them for help first.