Thread: Maclaurin series Sine(x) function Expantion

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    Maclaurin series Sine(x) function Expantion

    Hi guys hope you're doing well
    so this is my assigment
    Maths3assignment2-no3 (2).pdf

    this is as far as I got in the code, problem is I'm not getting the same accuray for the output for the program, in comparison with a calculator.

    also I'm trying to display each iteration of the accuray for the output but I think that's wrong as well for e.g.

    -----------------------------------------
    ITERATIONS -------- value
    -----------------------------------------
    ITERATION 1 -------: 2.500000
    ITERATION 2 -------: 2.750000
    ITERATION 3 -------: 2.625000
    ITERATION 4 -------: 2.687500
    ITERATION 5 -------: 2.718750
    ....
    ....
    ....
    ITERATION 20-------: 2.740645

    and so on..

    I had a read of
    Code:
    http://blogs.ubc.ca/infiniteseriesmodule/units/unit-3-power-series/taylor-series/maclaurin-expansion-of-sinx/
    but no luck .. you're gudince is my only help

    thank you


    Code:
    /* PROGRAM FOR SUM OF SIN (X) SERIES *//*x -x^3/3! + x^5/5! - .........*/
    
    
    
    
    #include<stdio.h>
    
    
    
    
    int main()
     {
        int x,n,t=1,i,j;
        double num,sum=0.0,r;
        long int deno;
    
    
        printf("\n\t\tINPUT:");
        printf("\n\t\t------");
        printf("\n\t\tEnter the value for x:  ");
        scanf("%d",&x);
        printf("\n\t\tEnter the value for n:  ");
        scanf("%d",&n);
        printf("\n\t\tOUTPUT:");
        printf("\n\t\t-------");
        r=x*(3.14/180.0);
        printf("\n\t\tThe radius value for the given x value is:  %lf",r);
        for(i=1;i<=n;i+=2)
         {
           num=1.0;
           deno=1;
           for(j=1;j<=i;j++)
             {
               num=num*r;
               deno=deno*j;
             }
           sum=sum+num/deno*t;
           t=t*-1;
           printf("\n\t\tThe value for sin %d is:  %.11f",x,sum);
         }
    
    
        printf("\n\n\n");
        return 0;
      }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Can you use a MacLaurin series to estimate sin(x) correctly with paper and pencil? If you can't solve a problem, you can't program a computer to do it. Just a few small examples calculated to 4 or 5 terms should be sufficient. Make sure you write out each term, listing the numerator, denominator and "x" term (x**3, x**5, etc). Compare your by-hand calculation to what your program produces at each step (add printfs to print out all necessary value on each loop iteration). Where do you see deviations?

    You should also use a more accurate value for pi when converting degrees to radians. Better more accurate than less. It might be best if you simply let the user input the angle in radians to begin with though, and eliminate any source of error from deg->rad conversion.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int x,n,t=1,i,j;
    > double num,sum=0.0,r;
    > long int deno;
    Having variables like
    double xPowerN;
    double nFactorial;

    would go a long way to clarifying your code purpose, as opposed to a handful of single letter and generic names.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 10-30-2013, 11:34 PM
  2. Troubleshooting Sine Function
    By sam.briggs in forum C Programming
    Replies: 1
    Last Post: 10-28-2011, 12:40 PM
  3. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  4. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 12:20 PM
  5. sine C program with Power Series
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-08-2001, 10:46 AM