1. Aug 13, 2011

### shayaan_musta

Here is my program for finding sin(x) at given x value.

x=input('x=\n')
tol=input('Tolerence=\n')
n=1
t(n)=1
s(n)=1
while(1)
n=n+1
t(n)=- ((x.^2)*(t(n-1))) / ((2*(n)-1)*(2*(n)-2))
s(n)=s(n-1)+t(n)
if(abs(t(n))>tol)
break;
end
end
fprintf('sinx for x=%d is %f',x,s(n))
%END

And here is a output

x=
5
Tolerence=
0.01

n =

1

t =

1.0000 -4.1667

s =

1.0000 -3.1667

n =

2

t =

1.0000 -4.1667

s =

1.0000 -3.1667

sinx for x=5 is -3.166667

But according to calculator sin(5)=0.08715

And I am confident that my recursion formula is correct.

Thanks

2. Aug 13, 2011

### Pythagorean

Sin function should alway returns number on (-1 1); your function returned a number outside that range.

Your calculator is taking degrees for the argument, btw.

3. Aug 13, 2011

### shayaan_musta

Wait wait bro. First of all thanks for your contribution.

I think you r saying that I must take readings from my calculator in radians?

4. Aug 14, 2011

### MATLABdude

Well, sin(5 radians) is -0.958924275, while sin(5 degrees) is 0.0871557427. I don't recognize your algorithm, but if you're trying to do a Laurent or Maclaurin series, you should probably take the input angle in radians.

Pythagorean's response is that the output of the sine function should be between -1 and 1. Your output for sin(5) is -3.166667: something is clearly broken.

EDIT: I still don't recognize your algorithm, but if your method guarantees convergence, shouldn't you want your t(n) (which I'm assuming is your tolerance or difference computation) to be LESS than your desired tolerance?

5. Aug 14, 2011

### shayaan_musta

I have understood you.
As you said that something is really broken, then what is that?
I have review it many times but I am unable to detect error(probably this is the reason that I am new to MATLAB)

6. Aug 14, 2011

### Pythagorean

could you direct us to the name of the algorithm you're using, shayaan?

7. Aug 14, 2011

### shayaan_musta

As Pythagorean you and MATLABdude said that they didn't understand my algorithm. As far as I understand algorithm mean is that your are trying to say that what program I am trying to create in MATLAB or either you are asking for version of MATLAB or windows which I am being used.

Well I answer both these questions.
1) I have sin taylor series, through which I derived a recursion formula tn as I shown. But at sin(5) it is given wrong value. It must be between (-1 1) as you said. Right?

2) I am using MATLAB 5(the oldest version) and OS is Windows XP SP2.

I will be very great full to you for helping me in this program.

8. Aug 14, 2011

### uart

You need to exit your loop when the error is less than some error tolerance.
Code (Text):

s(n)=s(n-1)+t(n)
if(abs(t(n))>tol)
break;

Should be changed to :
Code (Text):

s(n)=s(n-1)+t(n)
if(abs(t(n))<tol)
break;

BTW. I think there's still an error in the actual recursion formula.

9. Aug 14, 2011

### Pythagorean

I don't quite recognize the sin taylor series in there. Here's how I would have started:

Code (Text):

x = 5;
n = 0;
S = 0;

for i = 1:10

t = ((-1)^n)*(x^(2*n+1))/factorial(2*n+1);
S = S + t;

n = n+1;

end

disp('mine')
disp(S)
disp('matlabs')
disp(sin(x))

but here I hard code the number of iterations (and thus the order) with the line:

Code (Text):
for i = 1:10
which you will want to replace with your

Code (Text):
while(1)
and implement a tolerance routine. You can also have the while loop argument check the tolerance rather than using the if, break. This will reduce computation time (not by much in this case, but it's good practice).

I must admit I don't know exactly how to implement a tolerance routine, so if you use my code, please post the update so I can see! Thanks.

10. Aug 14, 2011

### Pythagorean

So I tried to implement a tolerance here below

It works until about x = 40 at which point mine starts diverging.

Code (Text):

x = 50;
n = 0;
S = 0;
tol = .01;
t = inf;

while(abs(t)>=tol)

t = ((-1)^n)*(x^(2*n+1))/factorial(2*n+1);
S = S + t;

disp(t)

n = n+1;

end

disp('mine')
disp(S)
disp('matlabs')
disp(sin(x))

11. Aug 14, 2011

### uart

I just checked your algorithm and located it. You should have initialized both s and t to the same value as x.

12. Aug 15, 2011

### uart

BTW the use of arrays for s and t is pointless (unless you wanted to plot them for example to display the convergence).

It seems to me that the point of the recursive algorithm is that each term in the series can be calculated from just the previous term. There is no need to store all the terms.

The following code
Code (Text):

while(abs(t(n))>tol)
n = n+1;
t = - x.^2 * t / (2*n-1) / (2*n-2);
s = s+t;
end

would work just as well.

Which brings me to two other points that could improve your code.

1. Avoid the use of "break" where more transparent end loop conditions are more appropriate.

2. Overuse of unnecessary parentheses can make your code harder to read than it need be. This is one reason why a basic working knowledge of algebra (including order of operations) is a good prerequisite for programming.

Last edited: Aug 15, 2011
13. Aug 15, 2011

### shayaan_musta

Thanks for experts to reply and I am sorry for late reply(i.e. due to low voltage in our area.)

Here is my derivation for recursion formula of sin(x)

#### Attached Files:

• ###### sinx.JPG
File size:
14.6 KB
Views:
156
14. Aug 15, 2011

### shayaan_musta

Thanks Pythagorean

I will try your code and I'll post it.

15. Aug 15, 2011

### shayaan_musta

How can I initialize s and t to the same value?
where as,
t is the first term in the series and s is the sum of the first term i.e. s1=t1=1

16. Aug 16, 2011

### uart

I said same value as x.

Code (Text):

x=input('x=\n')
t=x;
s=x;

17. Aug 16, 2011

### shayaan_musta

Hello experts here is my corrected code and it is given same values as calculator does.

clc
close all
clear all
x=input('Give x=\n')
tol=input('Give Tolerance=\n')
u=x*(pi/180)
n=1;
t(n)=u;
s(n)=t(n);
while(1)
n=n+1;
t(n)= -((u.^2)*t(n-1)) / ((2*(n)-1)*(2*(n)-2))
s(n)=s(n-1)+t(n);
if(abs(t(n))<tol)
break;
end
end
fprintf('sin(x) at x=%d is %f',x,s(n))
%END

Here I am taking input as degree and then converting into radians then this program is running better. And here I have initialized t=x as a first term of sin(x) Taylor series, that's the point that uart was trying to say, right uart?

Hello Pythagorean here you can see my updated code.

And MATLABdude helped me in understanding me for the concept of radians and degree.

But experts I have a question related to Taylor series of trigonometry. That is Taylor series for trigonometry always work on radian as an angle input? Means if we give input in degree, will it operate a wrong answer?

18. Aug 16, 2011

### jhae2.718

The Maclaurin/Taylor series for trigonometric functions are defined with respect to radian angle measures.

Trigonometric functions are almost always considered in radians in calculus.

19. Aug 16, 2011

### shayaan_musta

Hi jhae2.718!

Means Taylor/Maclaurin for trigonometry functions series only works on radian input?

20. Aug 16, 2011

### uart

Yes that's correct. Most trig related formulas in calculus and related topics will only work if you use radians. In a similar way to how $\log_e$ is the most natural representation of logarithm so to are the trig functions most naturally defined for radians.