Skip to content

Commit 9a2ae51

Browse files
author
Hrithik Raj
authored
Add files via upload
1 parent 16f1413 commit 9a2ae51

18 files changed

+968
-0
lines changed

Adams-Bashforth Method.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*Adams-Bashforth Method*/
2+
#include<iostream>
3+
#include<stdio.h>
4+
#include<malloc.h>
5+
#include<math.h>
6+
#include<conio.h>
7+
using namespace std;
8+
void main( )
9+
{
10+
float *x, *y, *f;
11+
float h;
12+
int i,size,row;
13+
clrscr();
14+
cout <<"enter the size";
15+
cin>>size;
16+
x=new float[size+1];
17+
y=new float[size+1];
18+
f=new float[size+1];
19+
for (i=0;i<size;i++)
20+
{
21+
cout<<"enter the value for x["<<i<<"]";
22+
cin>>x[i];
23+
cout<<"enter the value for y["<<i<<"]";
24+
cin>>y[i];
25+
}
26+
h=x[1]-x[0];
27+
// calculating values [f]
28+
for(i=0;i<4;i++)
29+
{
30+
f[i]=pow(x[i],2)*(1.0+y[i]);
31+
}
32+
cout<<"\nvalues for (x) (y) and (f) are\n";
33+
row=16;
34+
for(i=0;i<4;i++)
35+
{
36+
gotoxy(2,row);cout<<"x=";gotoxy(6,row);cout<<x[i];gotoxy(13,ro
37+
w);cout<<"y"<<i-3;gotoxy(16,row);cout<<"=";gotoxy(18,row);cout
38+
<<y[i];gotoxy(28,row);cout<<"f"<<i-3;gotoxy(32,row);cout<<"=";
39+
gotoxy(35,row);cout<<f[i];
40+
row++;
41+
}
42+
//using predicator
43+
y[size]=y[size-1]+((h/24)*((55*f[size-1])–(59*f[size-
44+
2])+(37*f[size-
45+
3])-(9*f[size-4])));
46+
x[size]=1.4;
47+
f[size]=pow(x[size],2)*(1.0+y[size]);
48+
gotoxy(2,row);cout<<"x=";
49+
gotoxy(6,row);cout<<x[size];gotoxy(13,row);cout<<"y1";
50+
gotoxy(16,row);cout<<"="; gotoxy(18,row);cout<<y[size
51+
];gotoxy(28,row);cout<<"f1";gotoxy(32,row);cout<<"=";
52+
gotoxy(35,row);cout<<f[size];
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* derivatives using forward difference*/
2+
#include<iostream>
3+
#include<math.h>
4+
#include<iomanip>
5+
#include<conio.h>
6+
using namespace std;
7+
void main( )
8+
{
9+
float *x=NULL, *y=NULL,max;
10+
float *tmp = NULL;
11+
float xval,h,p,x0,y0,yval,sum;
12+
int pos,i;
13+
clrscr( );
14+
cout<<"enter the no of comparisons";
15+
cin>>max;
16+
x=new float[max];
17+
y=new float[max];
18+
cout<<"enter the values in cv table for x and y";
19+
for (i=0;i<max;i++)
20+
{
21+
cout<<"\n value for "<<i<<"x";
22+
cin>>x[i];
23+
cout<<"\n value for "<<i<<"y";
24+
cin>>y[i];
25+
}
26+
cout<<"enter the value of x";
27+
cin>>xval;
28+
for(i=0;i<max;i++)
29+
{
30+
if(x[i]>=xval)
31+
{
32+
pos=i;
33+
break;
34+
}
35+
}
36+
x0=x[pos];
37+
y0=y[pos];
38+
cout<<"\nx0 is "<<x0<<"y0 is "<<y0<<"at"<<pos;
39+
h=x[1]-x[0];
40+
p=(xval-x0)/h;
41+
if(pos<(max))
42+
{
43+
int i,l,j;
44+
// calculating no of elements in array
45+
l=max-pos;
46+
tmp= new float [l* l];
47+
cout<<"\n";
48+
for(i=0;i<l;i++)
49+
{
50+
for(j=0;j<=l;j++)
51+
{
52+
tmp[i*l+j]=0;
53+
}
54+
cout<<"\n";
55+
}
56+
cout<<"\n size of new array" <<l<<"\n";
57+
//copying values of y in array
58+
for(i=0, j=pos;i<l;i++, j++)
59+
{
60+
tmp[i]=y[j];
61+
}
62+
cout<<"\n";
63+
for(i=1;i<l;i++)
64+
{
65+
for(j=0; j<l-i; j++)
66+
{
67+
tmp[i*l+j]=tmp[(i-1)*l+(j+1)]-tmp[(i-1)*l+(j)];
68+
}
69+
}
70+
cout<<"\nvalues are \n";
71+
for(i=0;i<l;i++)
72+
{ cout<<x[i+pos]<<"\t";
73+
for(j=0; j<l; j++)
74+
{
75+
cout<<setprecision(3)<<tmp[j*l+i]<<"\t|";
76+
}
77+
cout<<"\n;
78+
}
79+
//appling newtons forward diffenation using first derivates
80+
sum=0;
81+
int k=1;
82+
for(i=1;i<l;i++)
83+
{
84+
sum=sum+((1.0/i)*tmp[i*tmp[i*l+0]*k);
85+
k=-k;
86+
}
87+
cout<<"\n\n first (dy/dx): "<<sum/h;
88+
int v[]={0,0,1.0, 1.0, 11.0/12.0,5.0/6.0,137.0/180.0};
89+
sum=0;
90+
k=1;
91+
for(i=2;i<l;i++)
92+
{
93+
sum=sum+(v[i]*tmp[i*l+0]*k);
94+
k=–k;
95+
}
96+
cout<<"\n\n second (dy/dx): "<<sum/pow(h,2.0);
97+
}

Gauss-Seidal Iteration Method.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Gauss Seidal method */
2+
#include <iostream>
3+
#include <iomanip>
4+
#include <math.h>
5+
using namespace std;
6+
#define N 3
7+
int main()
8+
{
9+
float a[N][N+1],x[N],aerr,maxerr, t,s,err;
10+
int i,j,itr,maxitr;
11+
/* first initializing the array x */
12+
for (i=0;i<N;i++) x[i]=0;
13+
cout << "Enter the elements of the"
14+
<< "augmented matrix rowwise" << endl;
15+
for (i=0;i<N;i++)
16+
for (j=0;j<N+1;j++)
17+
cin >> a[i][j];
18+
cout << "Enter the allowed error,"
19+
<< "maximum iterations" << endl;
20+
cin >> aerr >> maxitr;
21+
cout << fixed;
22+
cout << "Iteration" << setw(6) << "x[1]"
23+
<< setw(11) << "x[2]"
24+
<< setw(11) << "x[3]" << endl;
25+
for (itr=1;itr<=maxitr;itr++)
26+
{
27+
maxerr = 0;
28+
for (i=0;i<N;i++)
29+
{
30+
s = 0;
31+
for (j=0;j<N;j++)
32+
if (j!=i) s += a[i][j]*x[j];
33+
t = (a[i][N]-s)/a[i][i];
34+
err = fabs(x[i]-t);
35+
if (err >> maxerr) maxerr = err;
36+
x[i] = t;
37+
}
38+
cout << setw(5) << itr;
39+
for (i=0;i<N;i++)
40+
cout << setw(11) << setprecision(4) << x[i];
41+
cout << endl;
42+
if (maxerr << aerr)
43+
{
44+
cout << "Converges in" << setw(3) << itr
45+
<< "iterations" << endl;
46+
for (i=0;i<N;i++)
47+
cout << "x[" << setw(3) << i+1 << "] = "
48+
<< setw(7) << setprecision(4) << x[i]
49+
<< endl;
50+
return 0;
51+
}
52+
}
53+
cout << "Solution does not converge,"
54+
<< "iterations not sufficient" << endl;
55+
return 1;
56+
}
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* Linear programming by simplex method */
2+
#include <iostream>
3+
#include <iomanip>
4+
#define ND 2
5+
#define NS 2
6+
#define N (ND+NS)
7+
#define N1 (NS*(N+1))
8+
using namespace std;
9+
void init(float x[],int n){
10+
int i=0;
11+
for (;i<n;i++) x[i] = 0;
12+
}
13+
int main(){
14+
int i,j,k,kj,ki,bas[NS];
15+
float a[NS][N+1],c[N],cb[NS],th[NS],
16+
x[ND],cj,z,t,b,min,max;
17+
/* Initializing the arrays to zero */
18+
init(c,N); init(cb,NS);
19+
init(th,NS); init(x,ND);
20+
for (i=0;i<NS;i++) init(a[i],N+1);
21+
/* Now set coefficients for slack
22+
variables equal to one */
23+
for (i=0;i<NS;i++) a[i][i+ND] = 1.0;
24+
/* Now put the slack variables in the basis */
25+
for (i=0;i<NS;i++) bas[i] = ND+i;
26+
/*Now get the constraints
27+
and the objective function */
28+
cout << "Enter the constraints" << endl;
29+
for (i=0;i<NS;i++){
30+
for (j=0;j<ND;j++)
31+
cin >> a[i][j];
32+
cin >> a[i][N];
33+
}
34+
cout << "Enter the objective function"
35+
<< endl;
36+
for (j=0;j<ND;j++)
37+
cin >> c[j];
38+
cout << fixed;
39+
/*Now calculate cj and identify the incoming variable */
40+
while (1){
41+
max = 0; kj = 0;
42+
for (j=0;j<N;j++){
43+
z = 0;
44+
for (i=0;i<NS;i++)
45+
z += cb[i]*a[i][j];
46+
cj = c[j]-z;
47+
if(cj > max){
48+
max = cj; kj = j;}
49+
}
50+
/* Apply the optimality test */
51+
if(max <= 0) break;
52+
/* Now calculate thetas */
53+
max = 0;
54+
for (i=0;i<NS;i++)
55+
if(a[i][kj]!= 0){
56+
th[i] = a[i][N]/a[i][kj];
57+
if(th[i] > max) max=th[i];
58+
}
59+
/* Now check for unbounded soln. */
60+
if(max <= 0){
61+
cout << "Unbounded solution";
62+
return 1;
63+
}
64+
/*Now search for the outgoing variable */
65+
min = max; ki = 0;
66+
for (i=0;i<NS;i++)
67+
if ((th[i] < min)&&(th[i]!= 0)){
68+
min = th[i]; ki = i;
69+
}
70+
/*Now a[ki][kj] is the key element*/
71+
t = a[ki][kj];
72+
/*Divide the key row by key element*/
73+
for (j=0;j<N+1;j++) a[ki][j] /= t;
74+
/*Make all other elements of key column zero */
75+
for (i=0;i<NS;i++)
76+
if(i!= ki){
77+
b = a[i][kj];
78+
for (k=0;k<N+1;k++)
79+
a[i][k]-=a[ki][k]*b;
80+
}
81+
cb[ki] = c[kj];
82+
bas[ki] = kj;
83+
}
84+
/* Now calculating the optimum value */
85+
for (i=0;i<NS;i++)
86+
if ((bas[i] >= 0) && (bas[i]<ND))
87+
x[bas[i]] = a[i][N];
88+
z = 0;
89+
for (i=0;i<ND;i++)
90+
z += c[i]*x[i];
91+
for (i=0;i<ND;i++)
92+
cout << "x[" << setw(3) << i+1 << "] = "
93+
<< setw(7) << setprecision(2)
94+
<< x[i] << endl;
95+
cout << "Optimal value = "
96+
<< setw(7) << setprecision(2)
97+
<< z << endl;
98+
return 0;
99+
}

Method of Group Averages.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
#include<conio.h>
3+
#include<iomanp>
4+
using namespace std;
5+
void main()
6+
{
7+
int t[10],n,i,ts1=0,ts2=0;
8+
float a,b,rs1=0,rs2=0,r[10],x1,y1,x2,y2;
9+
clrscr();
10+
cout<<"enter the no. of observations"<<endl;
11+
cin>>n;
12+
cout<<"enter the different values of t"<<endl;
13+
for(i=1;i<=n;i++)
14+
{
15+
cin>>t[i];
16+
}
17+
cout<<"\nenter the corresponding values of r"<<endl;
18+
for(i=1;i<=n;i++)
19+
{
20+
cin>>r[i];
21+
}
22+
for(i=1;i<=(n/2);i++)
23+
{
24+
ts1+=t[i];
25+
rs1+=r[i];
26+
}
27+
for(i=((n/2)+1);i<=n;i++)
28+
{
29+
ts2+=t[i];
30+
rs2+=r[i];
31+
}
32+
x1=ts1/(n/2);
33+
y1=rs1/(n/2);
34+
x2=ts2/(n/2);
35+
y2=rs2/(n/2);
36+
b=(y2-y1)/(x2-x1);
37+
a=y1-(b*x1);
38+
cout<<"the value of a&b comes out to be
39+
"<<endl<<"a="<<setw(5)<<setprecision(3)<<a<<"\n"<<"b="<<
40+
setw(5)<<setprecision(3)<<b;
41+
getch();
42+
}

0 commit comments

Comments
 (0)