Week 7 NOzzle
The first program is about the Non conservative Equation solved in the macromack Method.It is seen that this program takes more than 1000 iterations to get within the requiered Tolerance.
clear all
close all
clc
%testing the speed and accuracy of the non conservative vs the Conservative
%Form for the continuity equation
%Non Conservative Form
n=31
x=linspace(0,3,n);
dx=x(2)-x(1);
gamma=1.4;
rho=1-0.3146*x;
t=1-0.2314*x;
v=(0.01+1.09*x).*t.^0.5;
a=1+2.2*(x-1.5).^2;
iter=1% to calculate the speed
%Calculating error and tolerance
error= 100
tolerance =1e-4
rhoold= rho
dt=0.001
while error>tolerance
%predictor step
for j=2:n-1
drhodx=(rho(j+1)-rho(j))/dx;
dlogadx=(log(a(j+1))-log(a(j)))/dx;
dvdx=(v(j+1)-v(j))/dx;
%continuity equation
drhodt_p(j)=-rho(j)*dvdx-rho(j)*v(j)*dlogadx-v(j)*drhodx;
%solution update
rho(j)=rho(j)+drhodt_p(j)*dt;
end
for j=2:n-1
%corrector method
drhodx=(rho(j-1)-rho(j))/dx;
dvdx=(v(j-1)-v(j))/dx;
dlogadx=(log(a(j-1))-log(a(j)))/dx;
%continuity equation
drhodt_c(j)=-rho(j)*dvdx-rho(j)*v(j)*dlogadx-v(j)*drhodx;
end
%updating the Values
for i=2:n-1
drhodt(i)=0.5*(drhodt_p(i)+drhodt_c(i));
end
%initial Conditions
%inlet
v(1)=2*v(2)-v(3);
%outlet
v(n)=2*v(n-1)-v(n-2);
rho(n)=2*rho(n-1)-rho(n-2);
t(n)=t(n-1)-t(n-2);
%new Value
for i=2:n-1
rho(i)=rho(i)+drhodt(i)*dt;
end
error = max(abs(rho-rhoold))
rhoold=rho
iter= iter+1
end
plot(x,rho)
title(\'Distance vs Density(Non Conservative)\')
The Following Program is about the Conservative Form of Equation as this includes all the Variables witihin the Differentiation the solving time is less this form of Equation takes less than 1000 iterations to solve the same.
clear all
close all
clc
%testing the speed and accuracy of the non conservative vs the Conservative
%Form for the continuity equation
% Conservative Form
n=31
x=linspace(0,3,n);
dx=x(2)-x(1);
gamma=1.4;
rho=1-0.3146*x;
t=1-0.2314*x;
v=(0.01+1.09*x).*t.^0.5;
a=1+2.2*(x-1.5).^2;
iter=1% to calculate the speed
%Calculating error and tolerance
error= 100
tolerance =1e-4
rhoold= rho
dt=0.001
iter=1
%Conservative Form of equation
%d/dt(rho*a)+d/dx(rho*a*v)=0;
new_error=100
new_tolerance=1e-4
while new_error>new_tolerance
%By discritising the eqaution and re arrangin we get the following equation
for i=2:n-1
%predictor step
drhodx=rho(i+1)-rho(i)/dx
dadx=a(i+1)-a(i)
dvdx=v(i+1)-v(i)
drhodt_p(i)= drhodx*v(i)+dadx*a(i)*rho(i)+dvdx*rho(i)
%intialising the old values
rho(i)=rho(i)+drhodt_p(i)*dt
end
for i=2:n-1
%Corrector step
drhodx=rho(i-1)-rho(i)/dx
dadx=a(i-1)-a(i)
dvdx=v(i-1)-v(i)
drhodt_c(i)= drhodx*v(i)+dadx*a(i)*rho(i)+dvdx*rho(i)
end
%Updating the Values
drhodt=0.5*(drhodt_c+drhodt_p)
for i= 2:n-1
rho(i)=rho(i)+drhodt(i)*dt
end
%intial Conditions
%inlet
v(1)=2*v(2)-v(3);
%outlet
v(n)=2*v(n-1)-v(n-2);
rho(n)=2*rho(n-1)-rho(n-2);
t(n)=t(n-1)-t(n-2);
new_error=max(abs(rho-rhoold))
rhoold=rho
iter=iter+1
end
plot(x,rho)
title(\'density vs distance(Conservative)\')
From the above it can be concluded that the Conservative Form of Equation has the best accuracy and takes less time to solve the same.
Please make a report and explain the results with plots