0% found this document useful (0 votes)
67 views12 pages

LAB Sheet # 10: Regression (Least Square Method/linear Model)

The document contains summaries of 14 lab sheets covering various topics in numerical methods including: 1) Regression using the least squares method to find the line of best fit for a dataset 2) Curve fitting to find the exponential curve of best fit for a dataset 3) Lagrange interpolation to find the interpolated value for a given x-value 4) Gaussian elimination and Gauss-Seidel methods for solving systems of linear equations 5) Gauss-Jordan method for finding the inverse of a matrix

Uploaded by

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

LAB Sheet # 10: Regression (Least Square Method/linear Model)

The document contains summaries of 14 lab sheets covering various topics in numerical methods including: 1) Regression using the least squares method to find the line of best fit for a dataset 2) Curve fitting to find the exponential curve of best fit for a dataset 3) Lagrange interpolation to find the interpolated value for a given x-value 4) Gaussian elimination and Gauss-Seidel methods for solving systems of linear equations 5) Gauss-Jordan method for finding the inverse of a matrix

Uploaded by

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

LAB Sheet # 10

Regression (Least square method/linear model)


#include<stdio.h>

#include<conio.h>

#define S 50

int main()

float x[S], y[S], sumY=0,sumX=0, sumX2=0, sumXY=0, a, b;

int n, i;

printf(" How many data points? \n");

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

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

sumX=sumX + x[i];

sumX2=sumX2 + x[i]*x[i];

sumY= sumY + y[i];

sumXY = sumXY + x[i]*y[i];

b=(n*sumXY - sumX*sumY)/(n*sumX2-sumX*sumX);

a=(sumY-b*sumX)/n;
printf("values are : a= %0.2f and b = %0.2f", a, b);

printf("\n Equation of best fit is : y = %0.2f + %0.2f x", a, b);

return 0;

LAB Sheet # 11
Curve fitting for type y = ax^b
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define S 50
int main()
{
float x[S], y[S], sumY=0,sumX=0, sumX2=0, sumXY=0, a, b, A;
int n, i;
printf(" How many data points? \n");
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]);
}
for(i = 1; i<=n; i++)
{
sumX=sumX + log(x[i]);
sumX2=sumX2 + log(x[i])*log(x[i]);
sumY= sumY + log(y[i]);
sumXY = sumXY + log(x[i])*log(y[i]);
}
b=(n*sumXY - sumX*sumY)/(n*sumX2-sumX*sumX);
A=(sumY-b*sumX)/n;
a = exp(A);
printf("values are : a= %0.2f and b = %0.2f", a, b);
printf("\n Equation of best fit is : y = %0.2f + %0.2f x", a, b);
return 0;
}

LAB Sheet # 12
Lagrange’s Interpolation

#include<stdio.h>
#include<conio.h>
int main()
{
float x[100], y[100], xp, yp = 0, p;
int i, j, n;
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);
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;
}

LAB Sheet # 13
Gauss Elimination Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i, j, k, n;
printf("Enter number of unknowns:");
scanf("%d", &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("Mathematical Error!");
exit(0);
}
for(j=i+1; j<=n; 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];
}
}
}
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("\n Solution: \n");
for(i =1; i<=n; i++)
{
printf("x[%d] = %0.3f\n", i, x[i]);
}

return 0;
}

LAB Sheet # 14
Gauss Seidel Method

#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
int main()
{
float x0 = 0, y0 =0, z0 = 0, x1, y1, z1, e1, e2,e3, e;
int count = 1;
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\tx\ty\tz\n");
do
{
x1 = f1(x0, y0, z0);
y1 = f2(x1, y0, z0);
z1 = f3(x1, y1, z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n", count, x1, y1, z1);
e1 = fabs(x0 - x1);
e2 = fabs(y0 - y1);
e3 = fabs(z0 - z1);
count++;
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e&&e2>e&&e3>e);
printf("\n Solution: x = %0.3f, y=%0.3f and z = %0.3f\n", x1,
y1, z1);
return 0;
}

LAB Sheet # 15
Gauss Jordan Method (Inverse Matrix Method)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i, j, k, n;
printf("Enter order of matrix:");
scanf("%d", &n);
printf("Enter coefficients of Matrix: \n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
{
printf("a[%d][%d] = ", i, j);
scanf("%f", &a[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1; j<=n; j++)
{
if(i==j)
{
a[i][j+n] = 1;
}
else
{
a[i][j+n]=0;
}
}
}
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<=2*n; k++)
{
a[j][k]=a[j][k]-ratio*a[i][k];
}
}
}
}
for(i=1; i<=n; i++)
{
for(j = n+1; j<=2*n; j++)
{
a[i][j] = a[i][j]/a[i][i];
}
}
printf("\n The inverse matrix is: \n");
for(i =1; i<=n; i++)
{
for(j=n+1; j<=2*n;j++)
{
printf(" %0.3f\t", a[i][j]);
}
printf("\n");
}

return 0;
}

You might also like