0% found this document useful (0 votes)
21 views35 pages

NM & S Record - Cce Correction

Uploaded by

kirthikirthi1717
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)
21 views35 pages

NM & S Record - Cce Correction

Uploaded by

kirthikirthi1717
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/ 35

DEPARTMENT OF COMPUTER AND

COMMUNICATION ENGINEERING

NUMERICAL METHODS A N D S T A T I S T I C A L
LABORATORY

STUDENT NAME :

REGISTER NO :

YEAR/SEMESTER/SECTION :

BATCH :
TABLE OF CONTENTS

Page Staff
S.N Date Title of the Exercises Mark no
o s signatu
re

1 ROOTS OF NON – LINEAR EQUATION


USING BISECTION METHOD
2
ROOTS OF NON – LINEAR EQUATION
USING NEWTON’S METHOD

3 SOLVE THE SYSTEM OF LINEAR


EQUATIONS USING GAUSS -
ELIMINATION METHOD
4 SOLVE THE SYSTEM OF LINEAR
EQUATIONS USING GAUSS - JORDAN
METHOD
5 SOLVE THE SYSTEM OF LINEAR
EQUATIONS USING GAUSS - SEIDAL
ITERATION METHOD.
6
FIND AREA BY USING TRAPEZOIDAL
RULE

7 FIND AREA BY USING SIMPSON’S 1/3


RULE

8 FIND MEAN, MEDIAN AND MODE

9 FIND QUARTILE DEVIATION

10 FIND PEARSON’S COEFFICIENT OF


SKEWNESS
EX.NO:1
ROOTS OF NON – LINEAR EQUATION USING BISECTION METHOD
DATE:

AIM : To write a program in “C” Language to find out the root of the Algebraic and
Transcendental equations using Bisection Method.
ALGORITHM:

Step - 1 Start of the program.


Step - 2. Input the variable x1, x2 for
the task. Step - 3. Check f(x1)*f(x2)<0
Step - 4. If yes proceed
Step - 5. If no exit and print error
message Step - 6. Repeat 7-11 if
condition not satisfied Step - 7.
X0=(x1+x2)/2
Step - 8. If
f(x0)*f(x1)<0 Step -
9. X2=x0
Step - 10. Else
Step - 11.
X1=x0
Step - 12. Condition:
Step - 13.if | (x1-x2)/x1) | < maximum possible error
or f(x0)=0 Step - 14. Print output
Step - 15. End of program.
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>

/*Uncomment the function u want to use or else add u r own*/

//#define f(x) ((x*x*x)-(4*x)-9)


//#define f(x) ((x*x*x)-(x)-1)
//#define f(x) ((x)-(cos(x)))
//#define f(x) ((x*sin(x))-1)
//#define f(x) ((exp(x))-(3*x))
//#define f(x) ((x*x*x)-(9*x)+1) //”range to be entered mannually 2,4”

void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");
printf("Do you want to choose interval (y/n)?: ");
scanf("%c",&ch);
//If interval exists
if (ch=='y'||ch=='Y')
{
printf("Enter value of A and B\n");
scanf("%f%f",&a,&b);
}
//For finding intervals automatically
else
{
for(i=0;a==-1||b==-1;i++)
{
if(f(0)<0)
{
if(f(i)<0)
a=i;
if(f(i)>0)
b=i;
}
else
{
if(f(i)>0)
a=i;
if(f(i)<0)
b=i;
}
}
printf("\nFinding values of A and B...\nA=%.f\nB=%.f\n",a,b);
}

printf(" A\t\tB\t\tRoot\n");
//Iterration Table
while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
//Prv checks previous iteration value, loop terminates when root and
//prev are equal
prv=c;
c=((a+b)/2);
printf(" %.4lf\t\t%.4f\t\t%.4f\n",a,b,c);

if(f(a)>f(b)) //f(a)>f(b)
{
if(f(c)>0)
a=c;
else
b=c;
}
else //f(b)>f(a)
{
if(f(c)>0)
b=c;
else
a=c;
}
}
printf("\nThe Aproximate Root is %.4f",c);
getch();
}
INPUT/OUTPUT:
f(x)=((x*x*x)-(x)-1)

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:2
ROOTS OF NON – LINEAR EQUATION USING NEWTON’S
METHOD.
DATE:

AIM:

To write a program in “C” Language to implement Newton’s Raphson method.


ALGORITHM:

Step 1. Start the program.

Step 2. Define the function f(x), f’(x)

Step 3. Enter the initial guess of the root, say x0

Step 4. Calculate xn+1 = xn – [f(xn)/ f’(xn)], n = 0, 1, 2, …

Step 5. If |xn+1 – xn| < ϵ, ϵ being prescribed accuracy then

go to step 7. Step 6. Set xn = xn+1 and go to step 4.

Step 7. Print the value

of xn. Step 8. Stop the

program.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) ((3*x)-cos(x)-1) //Function
#define f1(x) (3+sin(x)) //Derivative of the function
void main()
{
float a=-1,b=-1,c,prv=-1;
int i;
char ch;
system("cls");
printf("Do you want to choose interval (y/n)?: ");
scanf("%c",&ch);
if (ch=='y'||ch=='Y')
{
printf("Enter value of A and B\n");
scanf("%f%f",&a,&b);
}
else
{
for(i=0;a==-1||b==-1;i++)
{
if(f(0)<0)
{
if(f(i)<0)
a=i;
if(f(i)>0)
b=i;
}
else
{
if(f(i)>0)
a=i;
if(f(i)<0)
b=i;
}
}
printf("\n Finding values of A and B...\nA=%.f\nB=%.f\n",a,b);
}
printf(" Itt\t\t\tRoot\n");
c=(abs(f(a))>=abs(f(b)))?a:b;
i=0;
EX.NO:3
SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS -
ELIMINATION METHOD.
DATE:

while((ceil(10000*prv)/10000)!=(ceil(10000*c)/10000))
{
prv=c;
c=(c-(f(c)/f1(c)));
printf(" %d\t\t%.4f\n",i,c);
i++;
}
printf("\nThe Aproximate Root is %.4f",c);
}

INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
AIM:

To write a C program to implement the system of linear equations using gauss


- elimination method.

ALGORIHTM:

1. Start

2. Read Number of Unknowns: n

3. Read Augmented Matrix (A) of n by n+1 Size

4. Transform Augmented Matrix (A) to Upper Trainagular Matrix by Row Operations.

5. Obtain Solution by Back Substitution.

6. Display Result.

7. Stop
PROGRAM:

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

int main()
{
int i, j, k, n;
float a[10][11], x[10], r;

system("cls");

printf("Enter the values:\n\n");


n = 3;

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 (i = 1; i <= n - 1; i++)


{
if (a[i][i] == 0.0)
{
printf("Error: Division by zero");
exit(0);
}
for (j = i + 1; j <= n; j++)
{
r = a[j][i] / a[i][i];
for (k = 1; k <= n + 1; k++)
{
a[j][k] = a[j][k] - r * a[i][k];
}
}
}

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

for (i = n - 1; i >= 1; i--)


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

printf("\nAnswer:\n");
for (i = 1; i <= n; i++)
{
printf("x%d = %.3f\n", i, x[i]);
}

return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:4
SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS -
JORDAN METHOD
DATE:

AIM:

To write a C program to implement the system of linear equations using gauss - jordan
method.

ALGORITHM:

1. Start

2. Read Number of Unknowns: n

3. Read Augmented Matrix (A) of n by n+1 Size

4. Transform Augmented Matrix (A) to Diagonal Matrix by Row Operations.

5. Obtain Solution by Making All Diagonal Elements to 1.

6. Display Result.

7. Stop
PROGRAM:

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

int main() {
int i, j, k, n;
float a[10][11], x[10], r;

system("cls");

printf("Enter the number of equations: ");


scanf("%d", &n);
if (n <= 0 || n > 10)
{
printf("Invalid number of equations. Please enter a value between 1 and 10.\n");
return 1;
}
printf("Enter the augmented matrix (coefficients and constants):\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 (i = 1; i <= n - 1; i++)
{
if (a[i][i] == 0.0)
{
printf("Error: Division by zero. The program cannot continue.\n");
return 1;
}
for (j = i + 1; j <= n; j++)
{
r = a[j][i] / a[i][i];
for (k = 1; k <= n + 1; k++)
{
a[j][k] = a[j][k] - r * a[i][k];
}
}
}
x[n] = a[n][n + 1] / a[n][n];
for (i = n - 1; i >= 1; i--) {

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


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

x[i] = x[i] / a[i][i];


}
printf("\nSolution:\n");
for (i = 1; i <= n; i++)
{
printf("x%d = %.3f\n", i, x[i]);
}

return 0;
INPUT/OUTPUT:

RESULT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20
EX.NO:5
SOLVE THE SYSTEM OF LINEAR EQUATIONS USING GAUSS -
SEIDAL ITERATION METHOD.
DATE:

AI
M: To write a C program to implement the system of linear equations using Gauss -
Seidal iteration method.

ALGORITHM:

1. Start

2. Read Number of Unknowns: n

3. Read Augmented Matrix (A) of n by n+1 Size

4. Transform Augmented Matrix (A) to Diagonal Matrix by Row Operations.

5. Obtain Solution by Making All Diagonal Elements to 1.

6. Display Result.

7. Stop
PROGRAM:
#include <stdio.h>
int main()
{
float a[10][10], x[10], ratio;
int i, j, k, n;

printf("Enter the number of unknowns: ");


scanf("%d", &n);
printf("Enter coefficients of the 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]);
}
}
for (i = 1; i <= n; i++)
{
if (a[i][i] == 0.0)
{
printf("Mathematical Error: Division by zero!\n");
return 1; // Exit the program with an error code
}

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];
}
}
}
}

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


{
x[i] = a[i][n + 1] / a[i][i];
}
/* Displaying the Solution */
printf("\n Solution:\n");
for (i = 1; i <= n; i++)
{
printf("x[%d] = %0.3f\n", i, x[i]);
}
return 0;
}

INPUT AND OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:6
FIND AREA BY USING TRAPEZOIDAL RULE
DATE:

AIM:
To write a C program to implement find area by using trapezoidal rule.

ALGORITHM:

1. Start

2. Define function f(x)

3. Read lower limit of integration, upper limit of integration and number of sub interval

4. Calculate: step size = (upper limit - lower limit)/number of sub interval

5. Set: integration value = f(lower limit) + f(upper limit)

6. Set: i = 1

7. If i > number of sub interval then goto

8. Calculate: k = lower limit + i * h

9. Calculate: Integration value = Integration Value + 2* f(k)

10. Increment i by 1 i.e. i = i+1 and go to step 7

11. Calculate: Integration value = Integration value * step size/2

12. Display Integration value as required answer

13. Stop
PROGRAM:
#include <stdio.h>
#include <math.h>

/* Define function here */


#define f(x) (1.0 / (1.0 + pow(x, 2)))

int main() {
float lower, upper, integration = 0.0, stepSize, k;
int i, subInterval;

/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);

printf("Enter upper limit of integration: ");


scanf("%f", &upper);

printf("Enter number of sub intervals: ");


scanf("%d", &subInterval);

/* Calculation */
/* Finding step size */
stepSize = (upper - lower) / subInterval;

/* Finding Integration Value */


integration = f(lower) + f(upper);

for (i = 1; i <= subInterval - 1; i++)


{
k = lower + i * stepSize;
integration += 2 * f(k);
}

integration = integration * stepSize / 2.0;

printf("\n Required value of integration is: %.3f", integration);

return 0;
}
INPUT AND OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:7
FIND AREA BY USING SIMPSON’S 1/3RD RULE
DATE:

AIM:
To write a C program to implement find area by using Simpson’s 1/3 rule.

ALGORITHM:
1. Start

2. Define function f(x)

3. Read lower limit of integration, upper


limit of integration and number of sub
interval

4. Calculate: step size = (upper limit - lower limit)/number of sub interval

5. Set: integration value = f(lower limit) + f(upper limit)

6. Set: i = 1

7. If i > number of sub interval then goto

8. Calculate: k = lower limit + i * h

9. If i mod 2 =0 then
Integration value = Integration Value +
2* f(k) Otherwise
Integration Value = Integration Value +
4 * f(k) End If

10. Increment i by 1 i.e. i = i+1 and go to step 7

11. Calculate: Integration value = Integration value * step size/3

12. Display Integration value as required answer

13. Stop
PROGRAM:
#include <stdio.h>
#include <math.h>

/* Define function here */


#define f(x) (1.0 / (1.0 + x * x))

int main() {
float lower, upper, integration = 0.0, stepSize, k;
int i, subInterval;

/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);

printf("Enter upper limit of integration: ");


scanf("%f", &upper);

printf("Enter number of sub intervals (must be even): ");


scanf("%d", &subInterval);
/* Check if the number of sub intervals is even */
if (subInterval % 2 != 0) {
printf("Number of sub intervals must be even.\n");
return 1; // Exit with an error code
}
/* Calculation */
/* Finding step size */
stepSize = (upper - lower) / subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for (i = 1; i <= subInterval - 1; i++) {
k = lower + i * stepSize;

if (i % 2 == 0) {
integration += 2 * f(k);
} else {
integration += 4 * f(k);
}
}
integration = integration * stepSize / 3.0;
printf("\nRequired value of integration is: %.3f\n", integration);
return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO: 8
TO FIND MEAN, MEDIAN AND MODE
DATE:

AIM:
To write a program in C to implement to find Mean, Median and Mode.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the mean
float calculateMean(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return (float)sum / n;
}
// Function to calculate the median
float calculateMedian(int arr[], int n)
{
// Sort the array
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}}}
// Calculate the median
if (n % 2 == 0)
{
// If the number of elements is even, average the middle two elements
return (float)(arr[(n - 1) / 2] + arr[n / 2]) / 2.0;
}
else
{
// If the number of elements is odd, return the middle element
return (float)arr[n / 2];
}}
// Function to calculate the mode
int calculateMode(int arr[], int n)
{
int mode = arr[0]; // Initialize mode to the first element
int maxCount = 1; // Initialize maxCount to 1

int current = arr[0];


int currentCount = 1;

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


{
if (arr[i] == current)
{
currentCount++;
}
else
{
if (currentCount > maxCount)
{
maxCount = currentCount;
mode = current;
}
current = arr[i];
currentCount = 1;
}
}
// Check for mode at the end of the array
if (currentCount > maxCount)
{
mode = current;
}
return mode;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int *arr = (int *)malloc(n * sizeof(int));


printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
float mean = calculateMean(arr, n);
float median = calculateMedian(arr, n);
int mode = calculateMode(arr, n);
printf("Mean: %.2f\n", mean);
printf("Median: %.2f\n", median);
printf("Mode: %d\n", mode);
free(arr); // Free dynamically allocated memory
return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO: 9
TO FIND QUARTILE DEVIATION
DATE:

AIM:
To write a program in C to implement to find Quartile Deviatrion

PROGRAM:

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

// Function to compare two integers for sorting


int compare(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}

// Function to calculate the quartile deviation


float calculateQuartileDeviation(int arr[], int n)
{
// Sort the array in ascending order
qsort(arr, n, sizeof(int), compare);

// Calculate the median (Q2)


float median;
if (n % 2 == 0)
{
median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
}
else
{
median = arr[n / 2];
}

// Calculate the first quartile (Q1)


float q1;
int mid = n / 2;
if (mid % 2 == 0)
{
q1 = (arr[mid / 2 - 1] + arr[mid / 2]) / 2.0;
}
else
{
q1 = arr[mid / 2];
}

// calculate the third quartile (Q3)


float q3;
if (mid % 2 == 0) {
q3 = (arr[mid + mid / 2 - 1] + arr[mid + mid / 2]) / 2.0;
}

else
{
q3 = arr[mid + mid / 2];
}

// Calculate the quartile deviation


return (q3 - q1) / 2.0;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int *arr = (int *)malloc(n * sizeof(int));

printf("Enter the elements:\n");


for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}

float quartileDeviation = calculateQuartileDeviation(arr, n);

printf("Quartile Deviation: %.2f\n", quartileDeviation);

free(arr); // Free dynamically allocated memory

return 0;
}
INPUT \ OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

RESULT:
EX.NO:10
TO FIND PEARSON'S COEFFICIENT OF SKEWNESS
DATE:

AIM:
To write a program in C to implement to find Pearson's coefficient of skewness.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function to compare two integers for sorting
int compare(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}

// Function to calculate the mean of an array


float calculateMean(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return (float)sum / n;
}

// Function to calculate the median of an array


float calculateMedian(int arr[], int n)
{
// Sort the array
qsort(arr, n, sizeof(int), compare);

// Calculate the median


if (n % 2 == 0)
{
return (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
}
else
{
return arr[n / 2];
}
}

// Function to calculate Pearson's coefficient of skewness


float calculateSkewness(int arr[], int n)
{
float mean = calculateMean(arr, n);

float median = calculateMedian(arr, n);

// Calculate the standard deviation


float sum_squared_diff = 0.0;
for (int i = 0; i < n; i++)
{
float diff = arr[i] - mean;
sum_squared_diff += diff * diff;
}
float standard_deviation = sqrt(sum_squared_diff / n);

// Calculate skewness
return (3.0 * (mean - median)) / standard_deviation;
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));

printf("Enter the elements:\n");


for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}

float skewness = calculateSkewness(arr, n);

printf("Pearson's Coefficient of Skewness: %.2f\n", skewness);

free(arr); // Free dynamically allocated memory

return 0;
}
INPUT \ OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 5
TOTAL 20

You might also like