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

  • MATLAB
  • Thread starter shayaan_musta
  • Start date
  • Tags
    Matlab
In summary: *(t(n-1))./((2*n)-1)*(2*n)-2; disp(t)should be changed towhile(abs(t(n))<tol) n = n+1; t = x.^2...*(t(n-1))./((2*n)-1)*(2*n)-2; disp(t)as the first statement will continue to calculate the sine even if it exceeds the tolerance.
  • #1
shayaan_musta
209
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
  • #2
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
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?
 
  • #4
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
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.
 
  • #6
could you direct us to the name of the algorithm you're using, shayaan?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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: 450
  • #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?
 

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

What is MATLAB?

MATLAB is a high-level programming language and interactive environment used for scientific computing, data analysis, and visualization.

How do I find the sine of a given value in MATLAB?

You can use the built-in function "sin(x)" in MATLAB to find the sine of a given value, where x is the angle in radians.

Can I find the sine of multiple values at once in MATLAB?

Yes, you can provide an array of values to the "sin(x)" function in MATLAB and it will calculate the sine for each value.

What is the output format of the "sin(x)" function in MATLAB?

The "sin(x)" function in MATLAB returns the value of sine in the same format as the input, i.e. if the input is a scalar, the output will be a scalar, and if the input is an array, the output will be an array.

Is there a way to change the output format of the "sin(x)" function in MATLAB?

Yes, you can use the "format" command in MATLAB to change the output format to your desired format. For example, you can use "format long" to display the output in a long format with more decimal places.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
969
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
965
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top