Computer Based Numerical & Statistical Techniques Lab
• Bi-Section Method Program
Program 1.
#include<stdio.h>
#include<math.h>
float f(float x)
{
return (x*x*x -4*x-9);
}
void bisect (float *x,float a,float b,int *itr)
{
*x= (a+b)/2;
++(*itr);
printf(" interation no. %3d x=%7.5f\n",*itr,*x);
}
main()
{
int itr=0, maxitr;
float x,a,b,aerr,x1;
printf("Enter the value of a,b""allolwed error, maimum iteratio n\n");
scanf("%f%f%f%d",&a,&b,&aerr,&maxitr);
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if (fabs(x1-x)<aerr)
{
printf("after %d iteration ,root <169>""=%6.4f\n,itr,x1");
return 0;
}
x=x1;
}
while (itr< maxitr);
printf("solution does not coverage,""iteration not sufficent ");
return 1;
}
• Regula Falsi Method
Program 2.
#include <stdio.h>
#include <math.h>
float f(float x) {
return cos(x) - x * exp(x);
}
void regula(float *x, float x0, float x1, float fx0, float fx1, int *itr) {
*x = x0 - ((x1 - x0) / (fx1 - fx0)) * fx0;
++(*itr);
printf("iteration no. %3d x=%7.5f\n", *itr, *x);
}
int main() {
int itr = 0, maxitr;
float x0, x1, x2, x3, aerr;
printf("Enter the values for x0, x1, absolute error, and maximum iterations: ");
scanf("%f %f %f %d", &x0, &x1, &aerr, &maxitr);
regula(&x2, x0, x1, f(x0), f(x1), &itr);
do {
if (f(x0) * f(x2) < 0)
x1 = x2;
else
x0 = x2;
regula(&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3 - x2) < aerr) {
printf("after %d iterations, root=%6.64f\n", itr, x3);
return 0;
}
x2 = x3;
} while (itr < maxitr);
printf("solution does not converge, iterations not sufficient\n");
return 1;
}
Program 3.
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;
}
main()
{
int itr,maxitr;
float h,x0,x1,aerr;
printf("Enter x0,allowed error,""maximum itrations\n");
scanf("%f %f %d",&x0,&aerr,&maxitr);
for (itr=1;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf("itration no. %3d,""x=%9.6f\n",itr,x1);
if (fabs (h)<aerr)
{
printf("After %3d iterations,""root = %9.6f\n",itr,x1);
return 0;
}
x0=x1;
}
printf("iterations not sufficient,""solution does not converge\n");
return 1;
}
4. Program
Matrix Addition
#include<stdio.h>
int main(){
int m,n,i,j;
printf("Enter the number of rows and column of the matrix: ");
scanf("%d%d", &m,&n);
int a[m][n],b[m][n],c[m][n];
printf("enter the element of the matrix A: \n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of matrix B: \n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
//add the matrix
for(i=0;i<m;i++){
for(j=0;j<n;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
//print the result
printf("the sum of two matrix is : \n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d",c[i][j]);
}
printf("\n");
}
return 0;
}
5. Program
Matrix multiplication :
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Comments
Post a Comment