Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help in MATLAB please

  1. Aug 13, 2011 #1
    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
     
  2. jcsd
  3. Aug 13, 2011 #2

    Pythagorean

    User Avatar
    Gold Member

    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.
     
  4. Aug 13, 2011 #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?
     
  5. Aug 14, 2011 #4

    MATLABdude

    User Avatar
    Science Advisor

    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?
     
  6. Aug 14, 2011 #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.
     
  7. Aug 14, 2011 #6

    Pythagorean

    User Avatar
    Gold Member

    could you direct us to the name of the algorithm you're using, shayaan?
     
  8. Aug 14, 2011 #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.
     
  9. Aug 14, 2011 #8

    uart

    User Avatar
    Science Advisor

    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.
     
  10. Aug 14, 2011 #9

    Pythagorean

    User Avatar
    Gold Member

    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.
     
  11. Aug 14, 2011 #10

    Pythagorean

    User Avatar
    Gold Member

    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))

     
     
  12. Aug 14, 2011 #11

    uart

    User Avatar
    Science Advisor

    I just checked your algorithm and located it. You should have initialized both s and t to the same value as x.
     
  13. Aug 15, 2011 #12

    uart

    User Avatar
    Science Advisor

    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
  14. Aug 15, 2011 #13
    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:

  15. Aug 15, 2011 #14
    Thanks Pythagorean

    I will try your code and I'll post it.
     
  16. Aug 15, 2011 #15
    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
     
  17. Aug 16, 2011 #16

    uart

    User Avatar
    Science Advisor

    I said same value as x.

    Code (Text):

    x=input('x=\n')
    t=x;
    s=x;
     
     
  18. Aug 16, 2011 #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))
    %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?
     
  19. Aug 16, 2011 #18

    jhae2.718

    User Avatar
    Gold Member

    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.
     
  20. Aug 16, 2011 #19
    Hi jhae2.718!

    Means Taylor/Maclaurin for trigonometry functions series only works on radian input?
     
  21. Aug 16, 2011 #20

    uart

    User Avatar
    Science Advisor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help in MATLAB please
  1. Matlab help please (Replies: 2)

  2. Matlab help please! (Replies: 3)

  3. Help in MATLAB please (Replies: 1)

  4. Matlab Help please (Replies: 4)

Loading...