From: Sudiyono on
Hello everyone, I have a little bit question here
I have made a sine function using the Taylor series (using n terms). This of course will have some error when compared to the Matlab's sine built-in function.
I find the error by using err = abs(mysinefunc - sin(x))
Now, i am a bit confused on how to find the value of n if let's say, i want the err to be < 1. How do I find the value of 'n' when the err is < 1. I have tried using loops, but it doesn't appear to work well. Here's the code:

min_err = input('Enter value: ');
x = pi;
n = 0;
mysinefunc = Taylor_sine(x,0);
err = abs(mysinefunc - sin(x));

while err < min_err
for n = 0:1+n
sine_approx = sine_approx + SineTaylor(x,n);
trunc_err = abs(sine_approx - sin(x))
end
end

I know the for loop won't work, that's also my question, can I do a for loop for an unspecified last number ( the term 1+n)?

Oh, and I have defined the Taylor_sine function to work perfectly well.
Any suggestion/help will be very appreciated.
Thanks and have a nice day :)
From: Steven Lord on

"Sudiyono " <sudiyono_manik(a)hotmail.com> wrote in message
news:hq4art$dbc$1(a)fred.mathworks.com...
> Hello everyone, I have a little bit question here
> I have made a sine function using the Taylor series (using n terms). This
> of course will have some error when compared to the Matlab's sine built-in
> function.
> I find the error by using err = abs(mysinefunc - sin(x))
> Now, i am a bit confused on how to find the value of n if let's say, i
> want the err to be < 1. How do I find the value of 'n' when the err is <
> 1. I have tried using loops, but it doesn't appear to work well. Here's
> the code:
>
> min_err = input('Enter value: ');
> x = pi;
> n = 0;
> mysinefunc = Taylor_sine(x,0);

What is this function and what is its relation to the SineTaylor function
you use below?

> err = abs(mysinefunc - sin(x));
>
> while err < min_err
> for n = 0:1+n

You don't need a FOR loop nested in here.

> sine_approx = sine_approx + SineTaylor(x,n);

You haven't defined sine_approx in this code, so this line will error (due
to an undefined variable) or use whatever was last in sine_approx (which is
likely to be the results of a previous run through this script, which is not
what you want.)

> trunc_err = abs(sine_approx - sin(x))
> end

Note that you never modified either err or min_err inside the WHILE loop, so
if the WHILE condition was satisfied when it was first reached it will
ALWAYS be satisfied and so you have an infinite WHILE loop.

> end
>
> I know the for loop won't work, that's also my question, can I do a for
> loop for an unspecified last number ( the term 1+n)?

No, but you can use a WHILE loop that will execute until the WHILE condition
is not satisfied.

Here's some pseudocode that you can fill in:

% select tolerance
% generate initial approximation
% while (approximation is not "close enough")
% update approximation with next term
% end while

You have most of the pieces in your code above ... the one piece you're
missing is keeping track of which term is the "next term". For that, I
suggest using "n = n + 1;".

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


 | 
Pages: 1
Prev: Open webpage through Matlab
Next: random values