Kantipur Forum - a real solution
WELCOME !!

Please Register, ask for assignment solutions & post the solutions if you know any.

LETS START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.



ASK FOR SOLUTION YOU WILL BE SATISFIED
HomeGalleryFAQSearchCalendarRegisterLog in

Numerical Method Problems done in Pulchowk Campus (063) 5 5 1
Share | 
 

 Numerical Method Problems done in Pulchowk Campus (063)

View previous topic View next topic Go down 
Goto page : 1, 2  Next
AuthorMessage
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:11 am



CLICK ABOVE
TO DOWNLOAD ALL PROGRAMS


curve fitting:

Code:


# include
# include
# include
# include
void main()
{
      int n,i,j;
      float x[10],y[10],sumx=0,sumy=0,sumx2=0,sumxy=0,a,b,x0,fx;

      clrscr();
      printf("\nEnter the no of points:");
      scanf("%d",&n);

      printf("\nEnter the points(x,y):\n");
      for(i=0;i
              scanf("%f,%f",&x[i],&y[i]);

      for(i=0;i
      {
              sumx+=x[i];
              sumx2+=x[i]*x[i];
              sumy+=y[i];
              sumxy+=x[i]*y[i];
      }

      printf("\nsumx=%f\tsumx2=%f\tsumy=%f\tsumxy=%f\n",sumx,sumx2,sumy,sumxy);

      a=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx);
      b=(sumy*sumx2-sumx*sumxy)/(n*sumx2-sumx*sumx);

      printf("The coefficients are a=%f\t b=%f\n",a,b);

      printf("\nEnter the point x:");
      scanf("%f",&x0);

      fx=a*x0+b;
      printf("\nf(%f)=%f",x0,fx);

      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.




Last edited by Formatted on 1st September 2009, 5:59 pm; edited 19 times in total
Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:12 am

gauss elimination
Code:

#include<stdio.h>
#include<math.h>

#include<stdlib.h>
#include<conio.h>


void main()
{
      int i,j,k,n=0;
      float a[10][10],l[10][10]={0},u[10][10]={0},p,b[10],z[10],x[10];

      clrscr();
      printf("\nDimension of square matrix:");
      scanf("%d",&n);
      printf("\nEnter the elements of matrix:\n");

      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
              {
                      printf("\na[%d,%d]:",i+1,j+1);
                      scanf("%f",&a[i][j]);
              }
      }
      printf("\nEnter the matrix B:\n");
      for(i=0;i<n;i++)
      {
              printf("\nb[%d]:",i+1);
              scanf("%f",&b[i]);
      }

      for(i=0;i<n;i++)
              for(j=0;j<n;j++)
                      if(i==j)
                              u[i][j]=1;

      for(i=0;i<n;i++)
              l[i][0]=a[i][0];

      for(j=0;j<n;j++)
              u[0][j]=a[0][j]/l[0][0];

      p=0;
      for(j=1;j<n;j++)
      {
              for(i=j;i<n;i++)
              {
                      p=0;
                      for(k=0;k<=j-1;k++)
                              p+=l[i][k]*u[k][j];

                      l[i][j]=a[i][j]-p;
              }
              for(i=j+1;i<n;i++)
              {
                      p=0;
                      for(k=0;k<=j-1;k++)
                              p+=l[j][k]*u[k][i];

                      u[j][i]=(a[j][i]-p)/l[j][j];
              }
      }

      printf("\n\nThe lower triangular matrix:\n");
      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
                      printf("%f\t",l[i][j]);
              printf("\n");
      }

      printf("\n\nThe upper triangular matrix:\n");

      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
                      printf("%f\t",u[i][j]);
              printf("\n");
      }

      z[0]=b[0]/l[0][0];


      for(i=1;i<n;i++)
      {
              p=0;
              for(j=0;j<=i-1;j++)
                      p+=l[i][j]*z[j];
              z[i]=(b[i]-p)/l[i][i];
      }


      x[n-1]=z[n-1];
      for(i=n-2;i>=0;i--)
      {
              p=0;
              for(j=i+1;j<n;j++)
                      p+=u[i][j]*x[j];
              x[i]=z[i]-p;
      }

      printf("\n");
      for(i=0;i<n;i++)
              printf("x[%d]=%f\t",i+1,x[i]);

      printf("\n");
      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:12 am

shoot
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

float fz(float x,float z);
float fy(float x,float y,float z);

void main()
{
      int n,i,j=0;
      float x[10],y[10],z[10],sz1,sz2,sy1,sy2,sz,sy,guess[10],b_gss[10],xn,f_lmt,h;

      clrscr();
      //read x[0],y[0]
      printf("\nx[0]=");
      scanf("%f",&x[0]);
      printf("\ny(x[0])=");
      scanf("%f",&y[0]);
      printf("\nx[n]=");
      scanf("%f",&xn);
      printf("\ny(x[n]))=");
      scanf("%f",&f_lmt);
      printf("\nStep size:");
      scanf("%f",&h);
      n=(int)((xn-x[0])/h);
      j=0;
      do
      {
              //read z[0]
              if(j<2)
              {
                      printf("\nInitial guess,z[0]=");
                      scanf("%f",&z[0]);
              }
              guess[j]=z[0];
              for(i=0;i<n;i++)
              {
                      sz1=fz(x[i],z[i]);
                      sz2=fz(x[i]+h,z[i]+h*sz1);
                      sz=(sz1+sz2)/2;
                      z[i+1]=z[i]+h*sz;
                      //printf("\n*******z=%f\n",z[i+1]);

                      sy1=fy(x[i],y[i],z[i]);
                      sy2=fy(x[i]+h,y[i]+h*sy1,z[i]+h*sz1);
                      sy=(sy1+sy2)/2;
                      y[i+1]=y[i]+h*sy;
                      //printf("\n*******y=%f\n",y[i+1]);
                      x[i+1]=x[i]+h;
                      //printf("\n*******y=%f\n",x[i+1]);
              }
              b_gss[j]=y[n];
              j++;

              if(j>=2)
              {
                      //printf("\n%f**************%f\n",b_gss[j-2],b_gss[j-1]);
                      //printf("\n********************************\n");
                      z[0]=guess[j-2]+(guess[j-1]-guess[j-2])*(f_lmt-b_gss[j-2])/(b_gss[j-1]-b_gss[j-2]);
              }
      }while(fabs(b_gss[j-1]-f_lmt)>0.000001);

      for(i=0;i<=n;i++)
              printf("\nx=%f\ty[x]=%f",x[i],y[i]);

      getch();
}

float fz(float x,float z)
{
      return 6*x;
}
float fy(float x,float y,float z)
{
      return z;
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:13 am

lagrange
Code:
include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

void main()
{
      int i,j,n=0;
      char ans;
      float x[20],f[20],x0;
      float sum,prod;

      clrscr();
      printf("\nEnter the no of sample points:");
      scanf("%d",&n);

      printf("\nEnter the sample points(x,f(x)):\n");
      for(i=0;i<n;i++)
              scanf("%f,%f",&x[i],&f[i]);
      do{
      printf("\nEnter the point for approximation:");
      scanf("%f",&x0);

      sum=0;
      for(i=0;i<n;i++)
      {
              prod=1;
              for(j=0;j<n;j++)
              {
                      if(j!=i)
                              prod*=(x0-x[j])/(x[i]-x[j]);
              }
              sum+=prod*f[i];
      }
      printf("\nThe functional value is f(%f)=%f",x0,sum);
      printf("\nDo you want to continue(y/n)?");
      scanf("%c",&ans);
      }while(ans=='y');


      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:13 am

jordan
Code:
#include<stdio.h>
#include<math.h>

#include<stdlib.h>
#include<conio.h>

void main()
{
      int i,j,k,l,n=0;
      float a[20][20],x[20],p,w,u,b[20][20];

      clrscr();
      printf("\nNo.of equations:");
      scanf("%d",&n);

      n--;

      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\na[%d,%d]:",i+1,j+1);
                      scanf("%f",&a[i][j]);
              }
              printf("\nb[%d]:",i+1);
              scanf("%f",&a[i][n+1]);
              printf("\n");
      }

      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\t%f",a[i][j]);
              }
              printf("\t%f",a[i][n+1]);
              printf("\n");
      }
      printf("\n");




      for(i=0;i<=n;i++)
      {

              w=0;
              for(j=i;j<=n;j++)
              {
                      if(fabs(a[j][i]>w))
                      {
                              u=j;
                              w=fabs(a[j][i]);
                      }
              }
              if(u!=i)
              {
                      for(j=i;j<=n+1;j++)
                      {
                              p=a[u][j];
                              a[u][j]=a[i][j];
                              a[i][j]=p;
                      }
              }

              for(j=0;j<=n;j++)
              {
                      p=a[j][i]/a[i][i];
                      for(k=0;k<=n+1;k++)
                      {
                              if(j!=i)
                                      a[j][k]=a[j][k]-p*a[i][k];
                      }
              }
      }
      /*
      for(i=1;i<=n;i++)
      {
              for(j=i-1;j>=0;j--)
              {
                      p=a[j][i]/a[i][i];
                      for(k=i;k<=n+1;k++)
                      {

                              b[j][k]=a[j][k]-p*a[i][k];
                              a[j][k]=b[j][k];
                      }
              }
      }
      */
      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\t%f",a[i][j]);
              }
              printf("\t%f",a[i][n+1]);
              printf("\n");
      }

      for(i=0;i<=n;i++)
      {
              x[i]=a[i][n+1]/a[i][i];

      }

      for(i=0;i<=n;i++)
      {
              printf("\nx[%d]=%f",i+1,x[i]);
      }

      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:14 am

gaussian
Code:

void main()
{
      int i,j,k,l,n=0;
      float a[20][20],x[20],p,w,u;
      clrscr();
      printf("\nNo.of equations:");
      scanf("%d",&n);

      n--;

      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\na[%d,%d]:",i+1,j+1);
                      scanf("%f",&a[i][j]);
              }
              printf("\nb[%d]:",i+1);
              scanf("%f",&a[i][n+1]);
              printf("\n");
      }

      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\t%f",a[i][j]);
              }
              printf("\t%f",a[i][n+1]);
              printf("\n");
      }
      printf("\n");




      for(i=0;i<=n-1;i++)
      {
              w=0;
              /*if(a[i][i]==0)
              {
                      for(j=i+1;j<=n;j++)
                      {
                              if(fabs(a[j][i]>w))
                              {
                                      u=j;
                                      w=fabs(a[j][i]);
                              }
                      }
                      for(j=0;j<=n+1;j++)
                      {
                              p=a[u][j];
                              a[u][j]=a[i][j];
                              a[i][j]=p;
                      }
              } */





              for(j=i;j<=n;j++)
              {
                      if(fabs(a[j][i]>w))
                      {
                              u=j;
                              w=fabs(a[j][i]);
                      }
              }
              if(u!=i)
              {
                      for(j=i;j<=n+1;j++)
                      {
                              p=a[u][j];
                              a[u][j]=a[i][j];
                              a[i][j]=p;
                      }
              }

              for(j=i+1;j<=n;j++)
              {
                      p=a[j][i]/a[i][i];
                      for(k=i;k<=n+1;k++)
                      {
                              a[j][k]=a[j][k]-p*a[i][k];
                      }
              }
      }

      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\t%f",a[i][j]);
              }
              printf("\t%f",a[i][n+1]);
              printf("\n");
      }

      for(i=n;i>=0;i--)
      {
              p=a[i][n+1];
              if(i!=n)
              {
                      for(j=i+1;j<=n;j++)
                      {
                              p=p-a[i][j]*x[j];
                      }
              }
              x[i]=p/a[i][i];
      }

      for(i=0;i<=n;i++)
      {
              printf("\nx[%d]=%f",i+1,x[i]);
      }

      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:15 am

spline
Code:
void gaussian(float a[10][10],float s[10],int n);

void main()
{
      char ans='n';
      int n,i,j,ind;

      float x[10],y[10],h[10],s[10]={0},at[10][10]={0},f[10],x0;
      float a,b,c,d,gx; //coefficients for interpolating polynomial

      clrscr();

      //read the points(x,y)-----------------------------------
      printf("\nEnter the no of points:");
      scanf("%d",&n);

      printf("\nEnter the points(x,y):\n");
      for(i=0;i<n;i++)
              scanf("%f,%f",&x[i],&y[i]);
      //-------------------------------------------------------

      for(i=0;i<n-1;i++)
      {
              f[i]=(y[i+1]-y[i])/(x[i+1]-x[i]); //First divided difference
              h[i]=x[i+1]-x[i];                //calculation of h[i]
      }
      //--------------------------------------------------------
      //formation of the tridiagonal matrix for s
      for(i=0;i<n-2;i++)
      {
              for(j=0;j<n-2;j++)
              {
                      if(i==j)
                              at[i][j]=2*(h[i]+h[i+1]);
                      if(j==i+1)
                      {
                              at[i][j]=h[j];
                              at[j][i]=at[i][j];
                      }

              }
      at[i][n-2]=6*(f[i+1]-f[i]);
      }
      //----------------------------------------------------
      /*
      printf("\nFDD's\n");
      for(i=0;i<n-1;i++)
      {
              printf("%f\t",f[i]);
      }
      */
      //---------------------------------------------------
      printf("\n\nThe tridiagonal matrix is\n");
      for(i=0;i<n-2;i++)
      {
              for(j=0;j<n-2;j++)
                      printf("%f\t",at[i][j]);
              printf("\t%f",at[i][n-2]);
              printf("\n");
      }
      //--------------------------------------------------
      //solve for s[i] using gaussian elimination
      gaussian(at,s,n-2);

      printf("\nThe values of s[i] are:\n");
      for(i=0;i<n;i++)
      {
              printf("\ns[%d]=%f",i,s[i]);
      }
      //------------------------------------------------
      //start the interpolation process
      do{
              printf("\nEnter the point for interpolation:");
              scanf("%f",&x0);

              for(i=0;i<n-1;i++)
              {
                      if(x0>=x[i] && x0<=x[i+1])
                              ind=i;
              }
              //printf("\n%d",ind);

              //calculation of coefficients
              a=(s[ind+1]-s[ind])/(6*h[ind]);
              b=s[ind]/2;
              c=((y[ind+1]-y[ind])/h[ind])-(2*h[ind]*s[ind]+h[ind]*s[ind+1])/6;
              d=y[ind];

              printf("\nCoefficients:a[%d]=%f,b[%d]=%f,c[%d]=%f,d[%d]=%f",ind,a,ind,b,ind,c,ind,d);

              gx=a*pow(x0-x[ind],3)+b*pow(x0-x[ind],2)+c*(x0-x[ind])+d;
              printf("\nFunctional value: f(%f)=%f",x0,gx);

              printf("\nDo you want to continue(y/n)?");
              fflush(stdin);
              ans=getche();
      }while(ans=='y');

      getch();
}

void gaussian(float a[10][10],float s[10],int n)
{
      int i,j,k,u;
      float w,p,dum[100];

      n--;
      //forward elimination steps
      for(i=0;i<=n-1;i++)
      {
              //code for pivoting
              w=0;
              for(j=i;j<=n;j++)
              {
                      if(fabs(a[j][i]>w))
                      {
                              u=j;
                              w=fabs(a[j][i]);
                      }
              }
              if(u!=i)
              {
                      for(j=i;j<=n+1;j++)
                      {
                              p=a[u][j];
                              a[u][j]=a[i][j];
                              a[i][j]=p;
                      }
              }

              for(j=i+1;j<=n;j++)
              {
                      p=a[j][i]/a[i][i];
                      for(k=i;k<=n+1;k++)
                      {
                              a[j][k]=a[j][k]-p*a[i][k];
                      }
              }
      }

      printf("\nThe final result of gaussian elimination is:\n");
      for(i=0;i<=n;i++)
      {
              for(j=0;j<=n;j++)
              {
                      printf("\t%f",a[i][j]);
              }
              printf("\t%f",a[i][n+1]);
              printf("\n");
      }
      //backward substitution steps
      for(i=n;i>=0;i--)
      {
              p=a[i][n+1];
              if(i!=n)
              {
                      for(j=i+1;j<=n;j++)
                      {
                              p=p-a[i][j]*dum[j];
                      }
              }
              dum[i]=p/a[i][i];
      }
      for(i=1;i<=n+1;i++)
      {
              s[i]=dum[i-1];
      }
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:15 am

num_diff
Code:
include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

float f(float x);

void main()
{
      int i;
      float x,df1,df2,h;
      char ans;

      clrscr();

      printf("\nEnter the value of x:");
      scanf("%f",&x);
      do{
      printf("\nEnter the value of h:");
      scanf("%f",&h);


      df1=(f(x+h)-f(x))/h;
      df2=(f(x+h)-f(x-h))/(2*h);
      printf("\nUsing two point formula:");
      printf("\nf'(%f)=%f",x,df1);

      printf("\nUsing three point central difference formula:");
      printf("\nf'(%f)=%f",x,df2);

      printf("\nTry for another value of h?(y/n)");
      ans=getche();

      }while(ans=='y');

      getch();
}

float f(float x)
{
      return exp(x)*sin(x);
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:15 am

Code:
num_int

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

float f(float x);

void main()
{
      int i=0;
      float h,integral1=0,integral2=0,integral3=0,integral4=0,a,b,p,q,temp;
      char ans;

      clrscr();
      printf("\nEnter the limit of integration(a,b):");
      scanf("%f,%f",&a,&b);
      do{
              printf("\nEnter the value of h:");
              scanf("%f",&h);
              if(a>b)
              {
                      temp=b;
                      b=a;
                      a=temp;
              }
              printf("\nUsing trapezoid rule:");
              i=0;
              integral1=0;
              while((a+i*h)<b-h)
                      integral1+=(h/2)*(f(a+i*h)+f(a+(++i)*h));
              printf("\nThe integral is:%f\n",integral1);
              printf("\n\n");

              printf("\nUsing Simpson's 1/3 rule:");
              i=0;
              integral2=0;
              while((a+i*h)<b-h)
              {
                      integral2+=(h/3)*(f(a+i*h)+4*f(a+(++i)*h)+f(a+(++i)*h));

              }
              printf("\nThe integral is:%f\n",integral2);

              printf("\nUsing Gaussian two point formula:");

              p=(b-a)/2;
              q=(b+a)/2;
              integral3=p*(f(-.577350*p+q)+f(.577350*p+q));
              printf("\nThe integral is:%f\n",integral3);

              printf("\nUsing Gaussian three point formula:");
              integral4=p*((5.0/9)*f(-.774596*p+q)+(8.0/9)*f(q)+(5.0/9)*f(.774596*p+q));
              printf("\nThe integral is:%f",integral4);


              printf("\nTry for another value of h?(y/n)");
              ans=getche();

      }while(ans=='y');
      getch();
}

float f(float x)
{
      return x*exp(x);
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
Formatted
Administrator


Number of posts: 386
Age: 24
Location: Kathmandu
faculty: Unknown
Points: 1061
Reputation: 5
Registration date: 2009-02-27

PostSubject: Re: Numerical Method Problems done in Pulchowk Campus (063)   1st September 2009, 12:17 am

Code:
lu decomposition
#include<stdio.h>
#include<math.h>

#include<stdlib.h>
#include<conio.h>


void main()
{
      int i,j,k,n=0;
      float a[10][10],l[10][10]={0},u[10][10]={0},p,b[10],z[10],x[10];

      clrscr();
      printf("\nDimension of square matrix:");
      scanf("%d",&n);
      printf("\nEnter the elements of matrix:\n");

      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
              {
                      printf("\na[%d,%d]:",i+1,j+1);
                      scanf("%f",&a[i][j]);
              }
      }
      printf("\nEnter the matrix B:\n");
      for(i=0;i<n;i++)
      {
              printf("\nb[%d]:",i+1);
              scanf("%f",&b[i]);
      }

      for(i=0;i<n;i++)
              for(j=0;j<n;j++)
                      if(i==j)
                              u[i][j]=1;

      for(i=0;i<n;i++)
              l[i][0]=a[i][0];

      for(j=0;j<n;j++)
              u[0][j]=a[0][j]/l[0][0];

      p=0;
      for(j=1;j<n;j++)
      {
              for(i=j;i<n;i++)
              {
                      p=0;
                      for(k=0;k<=j-1;k++)
                              p+=l[i][k]*u[k][j];

                      l[i][j]=a[i][j]-p;
              }
              for(i=j+1;i<n;i++)
              {
                      p=0;
                      for(k=0;k<=j-1;k++)
                              p+=l[j][k]*u[k][i];

                      u[j][i]=(a[j][i]-p)/l[j][j];
              }
      }

      printf("\n\nThe lower triangular matrix:\n");
      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
                      printf("%f\t",l[i][j]);
              printf("\n");
      }

      printf("\n\nThe upper triangular matrix:\n");

      for(i=0;i<n;i++)
      {
              for(j=0;j<n;j++)
                      printf("%f\t",u[i][j]);
              printf("\n");
      }

      z[0]=b[0]/l[0][0];


      for(i=1;i<n;i++)
      {
              p=0;
              for(j=0;j<=i-1;j++)
                      p+=l[i][j]*z[j];
              z[i]=(b[i]-p)/l[i][i];
      }


      x[n-1]=z[n-1];
      for(i=n-2;i>=0;i--)
      {
              p=0;
              for(j=i+1;j<n;j++)
                      p+=u[i][j]*x[j];
              x[i]=z[i]-p;
      }

      printf("\n");
      for(i=0;i<n;i++)
              printf("x[%d]=%f\t",i+1,x[i]);

      printf("\n");
      getch();
}

_________________
START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.


Back to top Go down
View user profile http://tv.antnepal.com
 

Numerical Method Problems done in Pulchowk Campus (063)

View previous topic View next topic Back to top 
Page 1 of 2Goto page : 1, 2  Next

Permissions in this forum:You cannot reply to topics in this forum
Kantipur Forum - a real solution :: Computer and Technology :: Programming :: Application Development :: C Programming-
Jump to:  
Free forum | © phpBB | Free forum support | Contact | Report an abuse | Free forums