Help in MATLAB: Finding sin(x) at given x value

  • Context: MATLAB 
  • Thread starter Thread starter shayaan_musta
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB program designed to compute the sine of a given angle using a Taylor series expansion. Participants explore the implementation details, address errors in the code, and clarify the importance of using radians versus degrees in trigonometric calculations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a MATLAB program for calculating sin(x) using a recursive formula derived from the Taylor series.
  • Another participant points out that the output of the sine function should be within the range of -1 to 1, highlighting that the program's output is incorrect.
  • There is a discussion about whether the input to the sine function should be in radians or degrees, with some participants suggesting that the calculator may be interpreting the input in degrees.
  • Several participants propose modifications to the code, including changing the loop condition to exit when the error is less than the specified tolerance.
  • One participant suggests that the algorithm should be initialized differently, specifically that both the sum and the term should start with the input value.
  • There are suggestions to avoid using arrays for storing terms in the series, as each term can be computed from the previous one without needing to store all terms.
  • Clarifications are made regarding the Taylor series for trigonometric functions, emphasizing that they are defined for radian measures.

Areas of Agreement / Disagreement

Participants generally agree that the sine function should return values within the range of -1 to 1 and that the input should be in radians. However, there is no consensus on the best implementation of the algorithm, as multiple suggestions and corrections are offered.

Contextual Notes

Participants express uncertainty about the correctness of the recursion formula and the overall structure of the program. There are also discussions about the limitations of using an older version of MATLAB and the implications for the code's functionality.

Who May Find This Useful

This discussion may be useful for individuals learning MATLAB programming, particularly in the context of numerical methods and trigonometric functions, as well as those interested in the Taylor series expansion.

shayaan_musta
Messages
208
Reaction score
2
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.

Please help me.
Thanks
 
Physics news on Phys.org
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.
 
Wait wait bro. First of all thanks for your contribution.

Kindly explain your answer.
I think you r saying that I must take readings from my calculator in radians?
 
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?
 
Thanks for replying and made me to understand you.

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)

I need your help. Thanks in advance.
 
could you direct us to the name of the algorithm you're using, shayaan?
 
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.

Kindly don't tell me to upgrade anything. Please help me on my installed versions.

If you want more information then kindly post reply.
I will be very great full to you for helping me in this program.
Thanks in advance.
 
You need to exit your loop when the error is less than some error tolerance.
Code:
s(n)=s(n-1)+t(n)
if(abs(t(n))>tol)
break;
Should be changed to :
Code:
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.
 
I don't quite recognize the sin taylor series in there. Here's how I would have started:

Code:
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:
 for i = 1:10

which you will want to replace with your

Code:
 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
So I tried to implement a tolerance here below

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

Code:
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
uart said:
BTW. I think there's still an error in the actual recursion formula.

I just checked your algorithm and located it. You should have initialized both s and t to the same value as x.
 
  • #12
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:
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:
  • #13
Thanks for experts to reply and I am sorry for late reply(i.e. due to low voltage in our area.)

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

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

Attachments

  • sinx.JPG
    sinx.JPG
    14.3 KB · Views: 504
  • #14
Thanks Pythagorean

Pythagorean said:
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.

I will try your code and I'll post it.
 
  • #15
uart said:
I just checked your algorithm and located it. You should have initialized both s and t to the same value as x.

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
shayaan_musta said:
How can I initialize s and t to the same value ?
I said same value as x.

Code:
x=input('x=\n')
t=x;
s=x;
 
  • #17
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))
%ENDHere 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
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
Hi jhae2.718!

Means Taylor/Maclaurin for trigonometry functions series only works on radian input?
 
  • #20
shayaan_musta said:
Hi jhae2.718!

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

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 [itex]\log_e[/itex] is the most natural representation of logarithm so to are the trig functions most naturally defined for radians.
 
  • #21
Shayann, have you thought about my suggestion of not using arrays (indexed notation) for both t and s. Have you figured out why it will still work correctly with both t and s as simple scalar quantities.
 
  • #22
uart said:
Shayann, have you thought about my suggestion of not using arrays (indexed notation) for both t and s. Have you figured out why it will still work correctly with both t and s as simple scalar quantities.

Hello uart!
Hope you'll be fine.
Yes I did what you said. But in my updated code, I have not shown that. And yes it is working. Even by acting upon your suggestion its easy.

But using indexed notation and not using indexed notation work just fine, I don't know. May be you help me about this as I am newest to MATLAB.
 
  • #23
shayaan_musta said:
Hello uart!But using indexed notation and not using indexed notation work just fine, I don't know.
Yes they both work. It's a matter of simplicity, if a scalar works just as good then why use a vector?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
3K