0% found this document useful (0 votes)
141 views

Numerical Lab

The document contains a table listing 10 numerical analysis topics covered by a student. For each topic, the table includes the date the topic was covered and space for the student's signature and remarks. The document then provides program code examples and output for 8 of the 10 topics covered, including Simpson's 1/3 rule, Trapezoidal method, Euler's method, Gauss elimination, Gauss-Seidel method, Runga-Kutta method, Newton-Raphson method, and Modified Euler's method.

Uploaded by

Neha Miracle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Numerical Lab

The document contains a table listing 10 numerical analysis topics covered by a student. For each topic, the table includes the date the topic was covered and space for the student's signature and remarks. The document then provides program code examples and output for 8 of the 10 topics covered, including Simpson's 1/3 rule, Trapezoidal method, Euler's method, Gauss elimination, Gauss-Seidel method, Runga-Kutta method, Newton-Raphson method, and Modified Euler's method.

Uploaded by

Neha Miracle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

NAME:

ROLL NO.:
SUBJECT:

Sl TOPIC DATE SIGNATURE REMARKS


No.
1 Simpsons 1/3rd Rule
2 Trapezoidal Rule
3 Eulers Method
4 Gauss Elimination
Method
5 Gauss Seidel Method
6 Runga Kutta Method (4th
order)
7 Newton Raphson
Method
8 Modified Eulers Method
9 Curve Fitting
10 Sum of Fibonacci Series
SIMPSON 1/3 RULE
#include<stdio.h>
#include<conio.h>
float f(float x)
{
return(1/(1+x));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h: ");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\n Refined value of n and h are:%d %f\n",n,h);
printf("\n Y values: \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n %f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}

}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\n Final integration is %f",ans);

getch();
}
OUTPUT:
TRAPEZOIDAL METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
int i,n;
float x0,xn,h,y[20],so,se,ans,x[20];
printf("\n Enter values of x0,xn,h:\n");
scanf("%f%f%f",&x0,&xn,&h);
n=(xn-x0)/h;
if(n%2==1)
{
n=n+1;
}
h=(xn-x0)/n;
printf("\nrefined value of n and h are:%d %f\n",n,h);
printf("\n Y values \n");
for(i=0; i<=n; i++)
{
x[i]=x0+i*h;
y[i]=f(x[i]);
printf("\n%f\n",y[i]);
}
so=0;
se=0;
for(i=1; i<n; i++)
{
if(i%2==1)
{
so=so+y[i];
}
else
{
se=se+y[i];
}
}
ans=h/3*(y[0]+y[n]+4*so+2*se);
printf("\nfinal integration is %f",ans);
getch();
}
OUTPUT:
EULER METHOD

#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t%0.3f\n",x,y);
}
}

OUTPUT:
GAUSS ELIMINATION
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular
matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required
solutions*/
}
return(0);
}
OUTPUT:
GAUSS-SEIDEL METHOD

#include<stdio.h>
#include<math.h>
#define X 2
Void main()
{
float x[X][X+1],a[X], ae, max,t,s,e;
int i,j,r,mxit;
for(i=0;i<X;i++) a[i]=0;
puts(" Eneter the elemrnts of augmented matrix rowwise\n");
for(i=0;i<X;i++)
{
for(j=0;j<X+1;j++)
{
scanf("%f",&x[i][j]);
}
}
printf(" Eneter the allowed error and maximum number of iteration: ");
scanf("%f%d",&ae,&mxit);
printf("Iteration\tx[1]\tx[2]\n");
for(r=1;r<=mxit;r++)
{
max=0;
for(i=0;i<X;i++)
{
s=0;
for(j=0;j<X;j++)
if(j!=i) s+=x[i][j]*a[j];
t=(x[i][X]-s)/x[i][i];
e=fabs(a[i]-t);
a[i]=t;
}
printf(" %5d\t",r);
for(i=0;i<X;i++)
printf(" %9.4f\t",a[i]);
printf("\n");
if(max<ae)
{
printf(" Converses in %3d iteration\n", r);
for(i=0;i<X;i++)
printf("a[%3d]=%7.4f\n", i+1,a[i]);
return 0;
}

}
}
OUTPUT:
RUNGI-KUTTA METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x,float y);
int main()
{
float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;
printf("Enter x0,y0,xn,h:");
scanf("%f %f %f %f",&x0,&y0,&xn,&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<xn)
{
m1=f(x0,y0);
m2=f((x0+h/2.0),(y0+m1*h/2.0));
m3=f((x0+h/2.0),(y0+m2*h/2.0));
m4=f((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y=y+m*h;
x=x+h;
printf("%f\t%f\n",x,y);
getch();
return 0;
}
}
float f(float x,float y)
{
float m;
m=(x-y)/(x+y);
return m;
}
NEWTON-RAPHSON METHOD
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*log10(x) - 1.2;
}
float df (float x)
{
return log10(x) + 0.43429;
}
void main()
{
int itr, maxmitr;
float h, x0, x1, allerr;
printf("\nEnter x0, allowed error and maximum iterations\n");
scanf("%f %f %d", &x0, &allerr, &maxmitr);
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
if (fabs(h) < allerr)
{
printf("After %3d iterations, root = %8.6f\n", itr, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are
insufficient\n");
return 1;
}
MODIFIED EULER METHOD
#include<stdio.h>
#include<math.h>
#include<string.h>
float fun(float,float);
main()
{
int i,j,c;
float x[100],y[100],h,m[100],m1,m2,a,s[100],w;
printf("\n C program for Modified Euler Method \n\n");
printf(" Enter the initial value of x:");
scanf("%f",&x[0]);
printf("\n Enter the value of increment h:");
scanf("%f",&h);
printf("\n Enter the final value of x:");
scanf("%f",&a);
printf("\n Enter the initial value of the variable y :");
scanf("%f",&y[0]);
s[0]=y[0];
for(i=1;x[i-1]<a;i++)
{
w=100.0;
x[i]= x[i-1]+h;
m[i]=fun(x[i-1],y[i-1]);
c=0;
while(w>0.0001)
{
m1=fun(x[i],s[c]);
m2=(m[i]+m1)/2;
s[c+1]=y[i-1]+m2*h;
w=s[c]-s[c+1];
w=fabs(w);
c=c+1;
}
y[i]=s[c];
}
printf("\n\n The respective values of x and y are\n x \t
y\n\n");
for(j=0;j<i;j++)
{
printf(" %f\t%f",x[j],y[j]);
printf("\n");
}
}
float fun(float a,float b)
{
float c;
c=a*a+b;
return(c);
}
CURVE FITTING

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{

int n,i,x[20],y[20],sumx=0,sumy=0,sumxy=0,sumx2=0;
float a,b;
printf("\n C program for Linear Curve Fitting \n ");
printf("\n Enter the value of number of terms n:");
scanf("%d",&n);
printf("\n Enter the values of x:\n");
for(i=0;i<=n-1;i++)
{
scanf(" %d",&x[i]);

}
printf("\n Enter the values of y:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<=n-1;i++)
{
sumx=sumx +x[i];
sumx2=sumx2 +x[i]*x[i];
sumy=sumy +y[i];
sumxy=sumxy +x[i]*y[i];

}
a=((sumx2*sumy -sumx*sumxy)*1.0/(n*sumx2-sumx*sumx)*1.0);
b=((n*sumxy-sumx*sumy)*1.0/(n*sumx2-sumx*sumx)*1.0);
printf("\n\nThe line is Y=%3.3f +%3.3f X",a,b);
return(0);
}
Output:
SUM OF FIBONACCI SERIES

Program:

# include <stdio.h>
# include <conio.h>
void main()
{
int a = -1, b = 1, c = 0, i, n, sum = 0 ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nThe fibonacci series is : \n\n") ;
for(i = 1 ; i <= n ; i++)
{
c=a+b;
printf("%d \t", c) ;
sum = sum + c ;
a=b;
b=c;
}
printf("\n\nThe sum of the fibonacci series is : %d", sum) ;
getch() ;
}

Ouput:

You might also like