0% found this document useful (0 votes)
30 views30 pages

bcsl-58 Lab

Uploaded by

ryuomran
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)
30 views30 pages

bcsl-58 Lab

Uploaded by

ryuomran
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/ 30

Q1: Bisection Method.

ANS:

#include<stdio.h>

#include<conio.h

>

#include<math.h

>

/*

Defining equation to be solved.

Change this equation to solve another problem.

*/

#define f(x) pow(x,5)-2*x*x-4*x-16

int main()

float x0, x1, x2, f0, f1, f2,

e; int step = 1;

/* Inputs

*/ up:

printf("\nEnter two initial guesses:\n");

scanf("%f%f", &x0, &x1);


printf("Enter tolerable error:\

n"); scanf("%f", &e);


/* Calculating Functional Value

*/ f0 = f(x0);

f1 = f(x1);

/* Checking whether given guesses brackets the root or not. */

if( f0 * f1 > 0.0)

printf("Incorrect Initial Guesses.\

n"); goto up;

/* Implementing Bisection Method */ printf("\

nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do

x2 = (x0 +

x1)/2; f2 =

f(x2);

printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);

if( f0 * f2 < 0)

x1 = x2;
f1 = f2;

else

x0 =

x2; f0 =

f2;

step = step + 1;

}while(fabs(f2)>e);

printf("\nRoot is: %f",

x2); return 0;

}
Q2: Regula-Falsi Method.
ANS:

#include<stdio.h>

#include<conio.h>

#include<math.h>

/* Defining equation to be solved.

Change this equation to solve another problem.

*/ #define f(x) x*x*x*x*x-2*x*x-4*x-16

int main()

float x0, x1, x2, f0, f1, f2, e;

int step = 1;

/* Inputs */

up:

printf("\nEnter two initial guesses:\n");

scanf("%f%f", &x0, &x1);

printf("Enter tolerable error:\n");

scanf("%f", &e);

/* Calculating Functional Values */


f0 = f(x0);

f1 = f(x1);

/* Checking whether given guesses brackets the root or not. */

if( f0*f1 > 0.0)

printf("Incorrect Initial Guesses.\n");

goto up;

/* Implementing Regula Falsi or False Position Method */

printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");

do

x2 = x0 - (x0-x1) * f0/(f0-f1);

f2 = f(x2);

printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);

if(f0*f2 < 0)

x1 = x2;

f1 = f2;
}

else

x0 = x2;

f0 = f2;

step = step + 1;

}while(fabs(f2)>e);

printf("\nRoot is: %f", x2);

getch();

return 0;

Secant Method
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

/* Defining equation to be solved.

Change this equation to solve another problem.

*/ #define f(x) pow(x,5)-2*x*x-4*x-16

int main()

float x0, x1, x2, f0, f1, f2, e;

int step = 1, N;

//clrscr();

/* Inputs */

printf("\nEnter initial guesses:\n");

scanf("%f%f", &x0, &x1);

printf("Enter tolerable error:\n");

scanf("%f", &e);

printf("Enter maximum iteration:\n");

scanf("%d", &N);
/* Implementing Secant Method */ printf("\

nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do

f0 = f(x0);

f1 = f(x1);

if(f0 == f1)

printf("Mathematical Error.");

exit(0);

x2 = x1 - (x1 - x0) * f1/(f1-f0);

f2 = f(x2);

printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);

x0 = x1;

f0 = f1;

x1 = x2;
f1 = f2;

step = step + 1;

if(step > N)

printf("Not Convergent.");

exit(0);

}while(fabs(f2)>e);

printf("\nRoot is: %f", x2);

getch();

Q3:

2x+3y+4z=9

X+2y+3z=6

4x+2y+5z=11

Solve it through Gauss’s Elimination Method.

ANS:

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

#include<math.h>

void main()

float mat[4][4],temp,temp1,x,y,z;

int i,n,j;

clrscr();

printf("\nEnter size of matrix: ");

scanf("%d",&n);

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

printf("\n\nenter the value of %d eqvation",i+1);

for(j=0; j<n; j++)

printf("\nenter the value of coeffcient %d: ",j+1);

scanf("%f",&mat[i][j]);

printf("\nenter the value of constent: ");

scanf("%f",&mat[i][j]);

printf("\n Your Matrix \n\n");


{

for(j=0;j<n+1;j++)

printf(" %g ",mat[i][j]);

printf("\n\n");

temp=mat[1][0]/mat[0][0];

temp1=mat[2][0]/mat[0][0];

for(i=0,j=0;j<n+1;j++)

mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);

mat[i+2][j]=mat[i+2][j]-(mat[i][j]*temp1);

temp=mat[2][1]/mat[1][1];

for(i=1,j=0;j<n+1;j++)

mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);

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

for(j=0;j<n+1;j++)

printf(" %.3f ",mat[i][j]);

printf("\n\n");

z = mat[2][3]/mat[2][2];

y = (mat[1][3] - mat[1][2]*z)/mat[1][1];

x = (mat[0][3] - mat[0][2]*z - mat[0][1]*y)/mat[0][0];

printf("\n\nx = %.3f",x);

printf("\n\ny = %.3f",y);

printf("\n\nz = %.3f",z);

getch();

OUTPUT:
Q4:

17x1 - 20x2 + 2x3=20

- 3*x1-18 x2 + x3=20

- 2x1 + 3x2+25 x3=20

Solve it through Gauss-Seidel Method.

ANS:

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

#include<math.h>

#define ESP 0.0001

#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)

#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)

#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)

void main()

double x1=0,x2=0,x3=0,y1,y2,y3;

int i=0;

clrscr();

printf("\n \n");

printf("\n x1\t\t x2\t\t x3\n");

printf("\n \n");

printf("\n%f\t%f\t%f",x1,x2,x3);

do

y1=X1(x2,x3);

y2=X2(y1,x3);

y3=X3(y1,y2);
if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP

printf("\n_ \n");

printf("\n\nx1 = %.3lf",y1);

printf("\n\nx2 = %.3lf",y2);

printf("\n\nx3 = %.3lf",y3);

i = 1;

else

x1 = y1;

x2 = y2;

x3 = y3;

printf("\n%f\t%f\t%f",x1,x2,x3);

}while(i != 1);

getch();

}
OUTPUT:

Q5:

X 1 2 3
F(x) 6 11 18

Solve it through Lagrange’s Interpolation.

ANS:

#include<stdio.h>

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

void main()

float x[10],y[10],temp=1,f[10],sum,p;

int i,n,j,k=0,c;

clrscr();

printf("\nhow many record you will be enter: ");

scanf("%d",&n);

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

printf("\n\nenter the value of x%d: ",i);

scanf("%f",&x[i]);

printf("\n\nenter the value of f(x%d): ",i);

scanf("%f",&y[i]);

printf("\n\nEnter X for finding f(x): ");

scanf("%f",&p);

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

temp = 1;
k = i;

for(j=0;j<n;j++)

if(k==j)

continue;

else

temp = temp * ((p-x[j])/(x[k]-x[j]));

f[i]=y[i]*temp;

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

sum = sum + f[i];

printf("\n\n f(%.1f) = %f ",p,sum);

getch();
}

OUTPUT:

Gauss Jordan Elimination

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

#include<math.h>

#define SIZE 10

int main()

float a[SIZE][SIZE], x[SIZE], ratio;

int i,j,k,n;

clrscr();

/* Inputs */

/* 1. Reading number of unknowns */

printf("Enter number of unknowns: ");

scanf("%d", &n);

/* 2. Reading Augmented Matrix */

printf("Enter coefficients of Augmented Matrix:\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]);

/* Applying Gauss Jordan Elimination

*/ for(i=1;i<=n;i++)

if(a[i][i] == 0.0)

printf("Mathematical Error!");

exit(0);

for(j=1;j<=n;j++)

if(i!=j)

ratio = a[j][i]/a[i][i];

for(k=1;k<=n+1;k++)

a[j][k] = a[j][k] - ratio*a[i][k];

}
}

/* Obtaining Solution */

for(i=1;i<=n;i++)

x[i] = a[i][n+1]/a[i][i];

/* Displaying Solution */

printf("\nSolution:\n");

for(i=1;i<=n;i++)

printf("x[%d] = %0.3f\n",i, x[i]);

getch();

return(0);

Gauss Sidel

#include<stdio.h>

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

#define f1(x,y,z) (17-y+2*z)/20

#define f2(x,y,z) (-18-3*x+z)/20

#define f3(x,y,z) (25-2*x+3*y)/20

/* Main function */

int main()

float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3,

e; int count=1;

clrscr();

printf("Enter tolerable error:\n");

scanf("%f", &e);

printf("\nCount\tx\ty\tz\n");

do

/* Calculation */

x1 = f1(x0,y0,z0);
y1 = f2(x0,y0,z0);

z1 = f3(x0,y0,z0); printf("%d\t%0.4f\t%0.4f\t%0.4f\

n",count, x1,y1,z1);

/* Error */

e1 = fabs(x0-x1);

e2 = fabs(y0-y1);

e3 = fabs(z0-z1);

count++;

/* Set value for next iteration */

x0 = x1;

y0 = y1;

z0 = z1;

}while(e1>e && e2>e && e3>e);

printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);

getch();

return 0;
}

Lagrange Interpolation

#include<stdio.h>

#include<conio.h>

int main()

float x[100], y[100], xp, yp=0, p;

int i,j,n;

//clrscr();

/* Input Section */

printf("Enter number of data: ");

scanf("%d", &n);

printf("Enter data:\n");

for(i=1;i<=n;i++)

printf("x[%d] = ", i);

scanf("%f", &x[i]);

printf("y[%d] = ", i);

scanf("%f", &y[i]);

}
printf("Enter interpolation point: ");

scanf("%f", &xp);

/* Implementing Lagrange Interpolation */

for(i=1;i<=n;i++)

p=1;

for(j=1;j<=n;j++)

if(i!=j)

p = p* (xp - x[j])/(x[i] - x[j]);

yp = yp + p * y[i];

printf("Interpolated value at %.3f is %.3f.", xp, yp);

return (0);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy