Skip to content

Create Pk Jha assignmnet #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 274 additions & 0 deletions Pk Jha assignmnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
int main() {
int n;
cout<<"Enter the value of n";
cin>>n;

N = 2*n; // Defining the value of N as the size of the Jacobian and other matrices

vector<float> X;
for(int i=0;i<2*n;i++)
{
X.push_back(0);
}
vector<float> fx;
for(int i=0;i<2*n;i++)
{
fx.push_back(0);
}
float F,xf,Cp,lam,xp,tfeed,Ts; // Getting the values of feed and product from the user

cout<<"Enter the value of feed";
cin>>F;
cout<<"Enter the value of xfeed";
cin>>xf;
cout<<"Enter the value of Cp";
cin>>Cp;
cout<<"Enter the value of latent heat";
cin>>lam;
cout<<"Enter the value of x product";
cin>>xp;
cout<<"Enter the value of T feed";
cin>>tfeed;
cout<<"Enter the value of temp of steam";
cin>>Ts;
vector<float> U; // Getting the values of U
float temp;

cout<<"Enter the value of Overall Heat Transfer Coefficient";

for(int i=0;i<n;i++)
{
cin>>temp;
U.push_back(temp);
}

vector<float> T; // Vector of Tempuratures

cout<<"Enter the Exit temperature of product";
for(int i = 0; i < n-1; i++)
{
T.push_back(0);
}

cin>>temp;
T.push_back(temp);


vector<float> L; // Vector of liquid flowrates

for(int i = 0; i < n-1; i++)
{
L.push_back(0);
}

L.push_back(F*xf/xp);

// guess array of x
for(int i=0;i<2*n;i++)
{
if(i==0)
{
cout<<"Enter the guess value of S";
cin>>X[i];
break;
}

if(i==(2*n-1))
{
cout<<"Enter the guess value of A";
cin>>X[i];
break;
}
if((i>0)&&(i<=n-1))
{
cout<<"Enter the temp guess value of "<<i<<"th effect";
cin>>X[i];
}
if((i>=n)&&(i<=2*n-1))
{
cout<<"Enter the liquid flowrates guess value of "<<i<<"th effect";
cin>>X[i];
}
}

for(int i=0;i<2*n;i++) // Making the array of different functions from 1 to 2n
{
if(i==0)
{
fx[i]=F*Cp*(tfeed-T[0])+X[0]*lam-(F-X[n])*lam;
}

if(i==n)
{
fx[i]=X[0]*lam-U[0]*X[2*n-1]*(Ts-X[1]);
}

if((i>1)&&(i<=n-1))
{
fx[i]=X[i+n-1]*Cp*(X[i]-X[i+1]) + (X[i+n-2]-X[i+n-1])*lam - (X[i+n-1]-X[n+i])*lam;
}

if( i==1)
{
fx[i]=X[i+n-1]*Cp*(X[i]-X[i+1]) + (F-X[n])*lam - (X[n]-X[n+1])*lam;
}

if( i == n+1)
{
fx[i] = (F - X[n])*lam - U[1]*X[2*n-1]*(Ts - X[1]);
}

if((i > n+1) && ( i<=2*n-1))
{
fx[i] = ( X[i-2] - X[i-1])*lam - U[i-n]*X[2*n-1]*(X[i-n] - X[i-n+1]);
}
}

// Defining the jacobian vector and initiallizing it
float J[2*n][2*n];

int count = 0;
int count1=0;
float flowrate[n+1];
flowrate[0] = F;
for( int i = 1; i < n+1; i++)
{
flowrate[i] = L[i-1];
}

for(int i = 0; i < 2*n; i++)
{
if( i == 0)
{

for(int j = 0; j < 2*n; j++)
{
if( j == 0 || j == n)
{
J[j][i] = lam;
}
else
{
J[j][i] = 0.0;
}
}
}

if( i > 0 && i < n)
{
count = 0;
for(int j = 0; j < n; j++) // Temperature above
{
if( j >= i-1 && j <= i)
{
J[j][i] = Cp * flowrate[j] * pow(-1,i+j);
}
else
{
J[j][i] = 0;
}
}

for(int j = n; j < 2*n; j++) // Temperature Below
{
if( j >= i-1+n && j <= i+n)
{
J[j][i] = X[2*n-1] * U[j-n] * pow(-1,i+j+1);
}
else
{
J[j][i] = 0;
}
}
}
if( i >=n && i < 2*n-2)
{
count = 0;
for(int j = 0; j < n; j++) // Temperature above
{
if( j >= i-n && j <= i-n+2)
{
if(j==(i-n+1))
{
J[j][i]=Cp*(T[i-n]-T[i-n+1])-2*lam;
}
else
{
J[j][i] = lam;
}
}
else
{
J[j][i] = 0;
}
}

for(int j = n; j < 2*n; j++) // Temperature Below
{
if( j == i+1 )
{
J[j][i] = -lam;
}
else if ( j == i+2)
{
J[j][i] = lam;
}
else
{
J[j][i] = 0;
}
}
}


if( i == 2*n-2)
{
count = 0;
for(int j = 0; j < n; j++) // Temperature above
{
if( j >= i-n && j <= i-n+1)
{
if(j==(i-n+1))
{
J[j][i]=Cp*(T[i-n]-T[i-n+1])-2*lam;
}
else
{
J[j][i] = lam;
}
}
else
{
J[j][i] = 0;
}
}

for(int j = n; j < 2*n; j++) // Temperature Below
{
if( j == i+1 )
{
J[j][i] = -lam;
}
else if ( j == i+2)
{
J[j][i] = lam;
}
else
{
J[j][i] = 0;
}
}
}
}

float J1[N][N]; // Defining the inverse of the matrix J
inverse(J, J1);
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cout<<J1[i][j]<<" ";
}
cout<<endl;
}
}