Thread: Lagrange Polynomial Program

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    18

    Lagrange Polynomial Program

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    #define pi 4. * atan(1.)
    int LaGrange(int x0[],int x[],int n,int m);
    main(){
        double x[10]={0,0.1,0.3,0.45,0.66,0.8,1.1,1.4,1.5,1.9};
        double approx[6]={-0.5,0.2,0.9,1.6,2.0,3.0};
        double y[10],yapprox[6],lagrange[6],temp,total;
        int i,n,m;
        
        for(i=0;i<10;i++)
        y[i]=x[i]*sin(x[i]);
        
        for(i=0;i<7;i++)
        yapprox[i]=approx[i]*sin(approx[i]);
        
        for (m=0;m<=6;m++){
            for (n=0;n<=9;n++){
                if (n<=7){
                    temp=(((approx[n]-x[n+1])*(approx[n]-x[n+2]))/((x[n]-x[n+1])*(x[n]-x[n+2])))*y[n];
                    total += temp;
                }
                else{
                    temp=(((approx[n]-x[n-1])*(approx[n]-x[n]))/((x[n-2]-x[n-1])*(x[n-2]-x[n])))*y[n];
                    total += temp;
                }
            }
            lagrange[m]=total;
        }
        printf("\nThe LaGrange Values for each value:\n");
        printf("x=-.5:\t%e\nx=0.2:\t%e\nx=0.9:\t%e\nx=1.6:\t%e\nx=2.0:\t%e\nx=3.0:\t%e\n",lagrange[0],lagrange[1],lagrange[2],lagrange[3],lagrange[4],lagrange[5]);
        printf("\nThe actual value for each value is:\n");
        printf("x=-.5:\t%e\nx=0.2:\t%e\nx=0.9:\t%e\nx=1.6:\t%e\nx=2.0:\t%e\nx=3.0:\t%e\n",yapprox[0],yapprox[1],yapprox[2],yapprox[3],yapprox[4],yapprox[5]);
        getchar();
    }
    So I am suppose estimate the values for xsinx by using Lagrange polynomial and noticed while doing this that my code is almost correct but my equation is not 9th order. How can i make my code shorter instead of writing out a 9th order polynomial. The equation i made was a 2nd order because i was not really understanding the LaGrange Polynomial.


    I basically have learning arrays, pointers, functions and thats it for right now. I can't use structures for this (I don't know what they are either.

    This is what a LaGrange Polynomial is.
    lagrange polynomial - Wolfram|Alpha

    Thank you for all the help

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Code:
    intLaGrange(intx0[],intx[],intn,intm);
    I just noticed that i am not even using this

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I've not much time but have you already looked at the examples on Wikipedia?

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    18
    Okay so looking at the examples on google I came up with this code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define pi 4*atan(1.)
    main(){
        double x[10]={0,0.1,0.3,0.45,0.66,0.8,1.1,1.4,1.5,1.9};
        double approx[6]={-0.5,0.2,0.9,1.6,2.0,3.0};
        double y[10],yapprox[6],lagrange[6],temp,total=0,part=1,grandtotal=0;
        int i,n,m,k;
         
        for(i=0;i<10;i++)
        y[i]=x[i]*sin(x[i]);
         
        for(i=0;i<7;i++)
        yapprox[i]=approx[i]*sin(approx[i]);
         
        for (k=0;k<=5;k++){
    		for (m=0;m<=9;m++){
    			for (n=0;n<=9;n++){
    				if (m!=n && k!=n){
    					temp = (approx[k]-x[n])/(x[m]-x[n]);
    					part = part * temp;
    				}
    			total = part * y[m];
    			}
    		grandtotal= total + grandtotal;
    		}
    	lagrange[k]=grandtotal;
    	}
        printf("\nThe LaGrange Values for each value:\n");
        printf("x=-.5:\t%e\nx=0.2:\t%e\nx=0.9:\t%e\nx=1.6:\t%e\nx=2.0:\t%e\nx=3.0:\t%e\n",lagrange[0],lagrange[1],lagrange[2],lagrange[3],lagrange[4],lagrange[5]);
        printf("\nThe actual value for each value is:\n");
        printf("x=-.5:\t%e\nx=0.2:\t%e\nx=0.9:\t%e\nx=1.6:\t%e\nx=2.0:\t%e\nx=3.0:\t%e\n",yapprox[0],yapprox[1],yapprox[2],yapprox[3],yapprox[4],yapprox[5]);
        getchar();
    }
    But this is still not giving me what values I want and i can't figure out what my mistake is. I have been looking at it for a while and can't figure it out.

    Lagrange polynomial - Wikipedia, the free encyclopedia

    I followed example one on the foundation of my code. Basically taking the ratio of each part and multiplying it all together through my first loop, then multiplying that by y[m] when all my values for ratios are multiplied together. After that they are all added up to give me some value and that is assigned to each lagrange variable. But i am not getting the results i need.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What are "approx" and "yapprox" supposed to be?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomial Multiplication program
    By daggerhunt in forum C Programming
    Replies: 2
    Last Post: 11-02-2012, 07:57 AM
  2. Polynomial program, Unknown Error.
    By protofarmer720 in forum C++ Programming
    Replies: 5
    Last Post: 06-14-2011, 01:24 AM
  3. Issue with Horner's polynomial c program
    By swebdev in forum C Programming
    Replies: 6
    Last Post: 05-24-2011, 12:13 PM
  4. Is it just me, or is the polynomial program popular?
    By CaptainMorgan in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-05-2006, 08:10 PM
  5. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 05:52 AM