You are on page 1of 6

Module VI

Fitting a Straight line - Fitting regression line of y on x

1. Start
2. Input n
3. sx=0
4. sx2=0
5. sy=0
6. sxy=0
7. for I=0 to n-1
a. do read xi, yi
b. sx=sx+xi
c. sx2=sx2+xi*xi
d. sy=sy+yi
e. sxy=sxy+xi * yi
8. denom = n * sx2 – sx * sx
9. a1 = (n * sxy – sx * sy) / denom
10. a0 = (sy * sx2 – sx * sxy)/denom
11. Output “ Slope=” a1
12. Output y intercept =” a0
13. Stop
Program
void main(){
float n;
cout<<”Enter the value of n;
float sx=0,sx2=0,sy=0,sxy=0;
float x[30],y[30];
for(int i=0;i<n;i++){
cout<<”Enter the value of xi and yi”;
cin>>x[i]>>y[i];
sx=sx+x[i];
sx2=sx2+x[i]*x[i];
sy=sy+y[i];
sxy=sxy+x[i]*y[i];
}
float denom= n* sx2 – sx+ sx;
float a1= (n * sxy – sx * sy) / denom;
float a0 = (sy * sx2 – sx * sxy)/denom
cout<<“ Slope=”<< a1
cout<<”Intercept=”<<a0;

Sangeeth N Page 1
}

Fitting a Parabola - Study only after all portions. Probability of asking is less. Study the
problem related to this method.

1. Start
2. Output “How many data points available”
3. Input n
4. For I =0 to n-1
a. do read xi,yi
5. for I =0 to 2
a. do for j=0 to 2
i. cij=0
ii. for k=0 to n-1
 do cij=cij+(xk)i+j-2
b. ci3=0
c. for k =0 t n-1
i. ci3 =ci3 + (xk)j-1 * yk
6. Solve for a by Gauss Elimination with pivoting
7. Output Regression Coefficient are
8. For i=0 to n-1
a. Output ai

Trapezoidal Rule

To evaluate the integral when the function f(x) is known

1. Start
2. Input a,b, n
3. h=( b-a)/n
4. sum=f(a)+f(b)
5. for i=1 to n-1
a. do sum = sum+ 2 * f(a + i * h)
6. integral = h * sum/2 ;
7. Output integral
8. Stop

#include<iostream.h>
using namespace std;
1
// Implement the function – 1+𝑥2
float f(int x){
float y;

Sangeeth N Page 2
y=1/(1+x*x);
return y;
}

void main(){

float a,b,n;
cout<<”Enter the value of a,b and n”;
cin>>a>>b>>n;
float h=(b-a)/n;
float sum=f(a)+f(b);
for(int i=1;i<=n-1;i++){
sum=sum+ 2 * f(a+i*h);
}

float integral = h * sum / 2 ;


cout<<”Value of Integral =”<<integral;

Simpson’s Rule
1. Start
2. Input a,b,n
3. If n is odd the return
4. h = (b-a)/n
5. s = f(a) + f(b)
6. s2=0; s4=0
7. for i=1 to n-1
a. if i is odd
i. then s4 = s4 + f(a + i *h )
b. else
i. s2 = s2+ f(a+ i * h)
8. integral = (h/3) * (s+ 2 * s2 + 4 * s4)
9. Output integral
10. Stop

#include<iostream.h>
using namespace std;
𝑥
// Implement the function – 1+3𝑥
float f(int x){
float y;
y=x/(1+3*x);

Sangeeth N Page 3
return y;
}

void main(){

float a,b,n;
cout<<”Enter the value of a,b and n”;
cin>>a>>b>>n;
float h=(b-a)/n;
float s=f(a)+f(b);
float s2=0,s4=0;
for(int i=1;i<n;i++){
if(i%2 !=0){ // odd
s4= s4 +f(a+i*h);
}
else{
s2=s2+f(a+i*h);
}
}
float integral = (h/3) * (s+ 2 * s2 + 4 * s4)
cout<<”Integral =”<<integral;
}

Gauss Quadrature Formula

1. Start
2. Input n
3. Input a,b
4. For i=1 to n
a. do ri,yi
5. sum=0
6. p=(a+b)/2
7. q=(b-a)/2
8. for i=1 to n-1
a. sum = sum + ri * f(p+q*yi)
9. integral = q * sum
10. Output Integral
11. Stop

#include<iostream.h>
using namespace std;

// Implement the function – 𝑥2 + 2𝑥

Sangeeth N Page 4
float f(int x){
float y;
y=x*x+2*x);
return y;
}

void main(){

float a,b,n;
cout<<”Enter the value of a,b and n”;
cin>>a>>b>>n;
for(int i=0;i<n;i++){
cin>>r[i]>>y[i];
}
float sum=0;
float p= (a+b)/2;
float q=(b-a)/2;
for(int i=1;i<n;i++){
sum=sum+r[i] *f(p+q*y[i]);
}
float integral = q * sum;
cout<<”Integral =”<<integral;
}

Gauss Elimination Method – Study only after all portions. Probability of asking is less.
Study the problem related to this method.
Algorithm
1. Start
2. Input n
3. For I =0 to n-1
a. Do for j=0 to n
i. Input a[i][j]
Triangularization
4. For k =0 to n-2
a. Do For i= k+1 to n-1
i. do m=a[i][k]/a[k][k]
ii. For j=k to n
 do a[i][j]=a[i][j]- m* a[k][j]
Back Substitution
5. x[n-1] = a[n-1]n] / a[n-1][n-1]
6. for i=n-2 down to 0
a. do sum=0

Sangeeth N Page 5
b. for j=i+1 to n-1
i. do sum = sum + a[i][j] * s[j]
ii. x[i]= (a[i][n] –sum) /a[i][i];
7. for i =0 to n-1
a. do output x[i]
Program
#include<iostream.h>
using namespace std;
void main(){
float a[20][20],x[20],n,sum;
int i,j,k;
cout<<”Enter no of quations”;
cin>>n;
for( i=0;i<n;i++){
for( j=0;j<n;j++){
cin>>a[i][j];
}
}
for(k=0;k<n-1;k++){
for(i=k+1;i<n;i++){
m=a[i][k]/a[k][k];
for(j=k;j<n;j++){
a[i][j]=a[i][j]-m*a[k][j];
}
}
}
x[n-1] = a[n-1]n] / a[n-1][n-1];
for(int i=n-2;i>=0;i--){
sum=0;
for(j=i+1;j<=n-1;j++){
sum=sum+a[i][j]*x[j];
x[i]=(a[i][n]-sum)*a[i][i];
}
}
for(i=0;i<n;i++){
cout<<x[i]<<”\t”;
}
}

Sangeeth N Page 6

You might also like