Thursday 16 June 2016

Computational Fluid Dynamics: Finite Difference Approximations VS Analytical Differentiation (MATLAB)

Description of the Program

The program takes user input for the function of the variable ‘x’ e.g. x2, sin(x), cos(x)/x2 etc., the point on which the derivative is evaluated and initial and final grid sizes.

The program then calculates its first derivative analytically.

It then calculates finite difference approximations of the first derivative of order h, with point A unknown and the point A known, using various step sizes, with maximum and minimum step size enter by the user.

 It then calculates and plots the error between the exact value and the approximations of the first derivative.

Code along with Guidelines


clc; %clears the command window

 

%data input and analytical differentiation

 

syms x

fx=input('Enter the function of ''x'' to be differentiated '); %input a function like sin(x) or x.^4 + x.^3

clc; %clears the command window

fxp=(diff (fx,x)); %calculate the derivative analytically

x=input('Enter the point at which the derivative is to be evaluated '); %input the value at which the derivative to be evaluated

clc; %clears the command window

y=inline(fxp,'x'); %convert a statement in to a function so it can be evaluated at some point

z=y(x); %derivative evaluated at the point

 

 

dxmin=input('Enter the smallest value of increment ');

clc;

dxmax=input('Enter the largest value of increment ');

clc;

%finite difference approximation

for dx=dxmin:0.001:dxmax; %loop for different values of grid spacing, dx=h1

    a=1.5;

    h1=a*dx; %h2 in the problem sheet

    h2=(2*a*dx); %h3 in the problem sheet

   

    fc=cos(5+h2)/(5+h2).^2; %value of the function at point c

    fb=cos(5+h1)/(5+h1).^2; %of the function at point b

    fa=cos(5-dx)/(5-dx).^2; %of the function at point a

    fi=cos(x)/(x).^2; %of the function at point A

    fxpu=(fc+fb-(2*fa))/((2*dx)+h1+h2); %formulation with A unknown

    fxpa=(fc+fb+(2*fa)-(4*fi))/((-2*dx)+h1+h2); %formulation with A known

    hold on

    plot ((z-fxpa)/z,dx,'.k') %error plot for approximation with A known

    plot ((z-fxpu)/z,dx,'.r') %error plot for approximation with A unknown

end

%text editing

title('Plot Between Error VS Grid Spacing') %title of the graph

xlabel('Error from the Exact Value'); %x-axis label

ylabel('Step Size/Grid Spacing') %y-axis label

text(0.6, 0.9,'Red: Error plot for approximation with A unknown') %identification

text(0.6, 0.85,'Black: Error plot for approximation with A known') %identification

Calculation


Sample Problem

Command Window after running the code

At Step One


Enter the function of 'x' to be differentiated cos(x)/x.^2

At Step Two


Enter the point at which the derivative is to be evaluated 5

At Step Three


Enter the smallest value of increment 0.001

At Step Four


Enter the largest value of increment 1
Result

 

Observations and Recommendations

It was observed that the error in the finite difference approximation when the value of the function at point A is unknown is less as compared to the case where value of the function at point A is known.
In physical/real-world problems, the exact derivatives of the functions are not known, therefore the error values between the exact and the finite difference approximation cannot be calculated. As it is also observed that the error between finite difference approximation and the exact value of the derivative decreases as the step size decreases, it is advisable to use a step size of ~1/1000 to make a compromise between CPU/RAM usage, time taken to solve the problem and error.


Karush–Kuhn–Tucker Conditions (MATLAB)

Code

%KKT with 2 variables, 2 linear constraints with one equality and one

%inequality constraint at once or 1 equality and no inequality constraint at once or no

%equality and one inequality constraint at once

clc;

syms x1 x2 g1 h1 u1 v1 s1

%input of a function like (x1 - 1.5).^2 + (x2 - 1.5).^2 for up to two variables x1, x2

fx=input('Enter the function of ''x'' to be minimized of the form (x - 4).^2 + (y - 6).^2 ');

clc;

C=input('Enter the number of constraints in the problem, e.g. 10 for one equality and no inequality constraint ');

clc;

if C==01

    %constraints input

    g1=input('Enter inequality constraint in the form x1+x2-10 ');

    clc;

    %Lagrange function calculation

    L=fx+(u1*(g1+s1.^2));

    %gradient conditions

    dx1=(diff (L,x1)); dx2=(diff (L,x2)); du1=(diff (L,u1)); ds1=(diff (L,s1));

    %simultaneous solution of gradient conditions

    [sols1,solu1,solx1,solx2]=solve(dx1==0,dx2==0,du1==0,ds1==0);

    a=zeros(3,4);

    for i=1:3

        a(i,1)=solx1(i,1);

        a(i,2)=solx2(i,1);

        a(i,3)=sols1(i,1);

        a(i,4)=solu1(i,1);

    end

    SET1=a(1,:); SET2=a(2,:); SET3=a(3,:);

    %feasibility and non-negativity of Lagrange multiplier check

    if SET1(1,3)>0 || SET1(1,4)>0

        disp('Set 1 '); disp ('     x1    x2    s    u'); disp (SET1)

    end

    if SET2(1,3)>0 || SET2(1,4)>0

        disp('Set 2 '); disp ('     x1    x2    s    u'); disp (SET2)

    end

    if SET3(1,3)>0 || SET3(1,4)>0

        disp('Set 3 '); disp ('     x1    x2    s    u'); disp (SET3)

    end

end

if C==10

    %constraints input

    h1=input('Enter equality constraint in the form x1+x2-10 ');

    clc;

    %Lagrange function calculation

    L=fx+(v1*h1);

    %gradient conditions

    dx1=(diff (L,x1)); dx2=(diff (L,x2)); dv1=(diff (L,v1));

    %simultaneous solution of gradient conditions

    [solv1,solx1,solx2]=solve(dx1==0,dx2==0,dv1==0);

    a=zeros(1,3);

    a(1,1)=solx1;

    a(1,2)=solx2;

    a(1,3)=solv1;

    disp('Set 1 '); disp ('     x1    x2    v'); disp (a);

end

if C==11

    %constraints input

    g1=input('Enter inequality constraint in the form x1+x2-10 ');

    clc;

    h1=input('Enter equality constraint in the form x1+x2-10 ');

    clc;

    %Lagrange function calculation

    L=fx+(v1*h1)+(u1*(g1+s1.^2));

    %gradient conditions

    dx1=(diff (L,x1)); dx2=(diff (L,x2)); du1=(diff (L,u1)); ds1=(diff (L,s1)); dv1=(diff (L,v1));

    %simultaneous solution of gradient conditions

    [sols1,solu1,solv1,solx1,solx2]=solve(dx1==0,dx2==0,du1==0,ds1==0,dv1==0);

    a=zeros(3,5);

    for i=1:3

        a(i,1)=solx1(i,1);

        a(i,2)=solx2(i,1);

        a(i,3)=sols1(i,1);

        a(i,4)=solu1(i,1);

        a(i,5)=solv1(i,1);

    end

    SET1=a(1,:); SET2=a(2,:); SET3=a(3,:);

    %feasibility and non-negativity of Lagrange multiplier check

    if SET1(1,3)>0 || SET1(1,4)>0

        disp('Set 1 '); disp ('       x1         x2         s         u         v'); disp (SET1)

    end

    if SET2(1,3)>0 || SET2(1,4)>0

        disp('Set 2 '); disp ('       x1         x2         s         u         v'); disp (SET2)

    end

    if SET3(1,3)>0 || SET3(1,4)>0

        disp('Set 3 '); disp ('       x1         x2         s         u         v'); disp (SET3)

    end

end


Example 4.29 Arora pg. 128


Step One


In this step the user enters the objective function:

Command Window

Enter the function of 'x' to be minimized (x1 - 1.5).^2 + (x2 - 1.5).^2

Step Two


In this step the user specifies the type(s) and quantity of constraints the objective function is subjected to.

01 means no equality and one inequality constraint, 10 means one equality and no inequality constraint, 11 means one equality and one inequality constraint.

Command Window

Enter the number of constraints in the problem, e.g. 10 for one equality and no inequality constraint 01

Step Three


In this step the user enter the constraint(s).

Command Window

Enter inequality constraint in the form x1+x2-10 x1 + x2 – 2

Step Four


Output of the program; feasible set with candidate minimum point(s).

Command Window

Set 1

     x1    x2    s    u

     1     1     0     1

Example 4.27 Arora pg. 122


Step One


Command Window

Enter the function of 'x' to be minimized (x1 - 1.5).^2 + (x2 - 1.5).^2

Step Two


Command Window

Enter the number of constraints in the problem, e.g. 10 for one equality and no inequality constraint 10

Step Three


Command Window

Enter equality constraint in the form x1+x2-10 x1 + x2 – 2

Step Four


Command Window

Set 1

     x1    x2    v

     1     1     1

Modifications

Extension to five or even more variables and more constraints can be made via simple modifications to the code. The structure of the new code will remain similar to the code mention above, though it will have many more lines of instructions.

Friday 23 October 2015

Pipe Flow Simulation

Just ran another simulation related to HMT, this problem became steady state after about 36 seconds.

Water at 318 K starts flowing (0.00035 m^3/s) through a steel pipe initially at 298 K. The steel pipe had convection to air at 298 K at 3,000 W/m^2.K. A simple simulation yielded inner and outer wall temperatures of the pipe to be 309.07 K and 311.26 K respectively. Then I ran a transient simulation, to find out the time taken by the pipe’s walls to reach these temperatures (f...rom 298 K) as water flows through it. It came out to be around 36 seconds.

Then I ran a FEA. To calculate stresses induced in the pipe due to water pressure, thermal effects, gravity etc. The pipe’s diameter increased by 0.005866 mm and von-mises stress induced was 117,016,056 N/m^2 with a factor of safety of 5.302.

Then I ran fatigue study to see if the pipe will survive under these loads for 20 years or not. It will I think. The fatigue S-N curves were not available so I used the ones for carbon steel (slightly different from the ones I used for CFD analysis and FEA); so will it last for 20 years I am not sure yet (searching for curves).



 Temperatures at inner wall surface
 Temperature at outer wall surface
displacement and stress animation

LU Decomposition MATLAB





%Please don't mess around with my code
clc;
R=input('How many variables are there in your system of linear equations? ');



clc;
disp ('Please enter the elements of the Coefficient matrix "A" and the Constant/Known matrix "B" starting from first equation');



A=zeros(R,R);

B=zeros(R,1);
for H=1:R

for i=1:R

fprintf ('Coefficient Matrix Column \b'), disp(i), fprintf ('\b\b Row \b'), disp(H);

I=input(' ');



A(H,i)=A(H,i)+I;
end

fprintf ('Known Matrix Row \b'), disp(H);

J=input(' ');



B(H,1)=B(H,1)+J;
end
clc;

[L,U] = lu(A);

Y = L\B;

X = U\Y;
fprintf ('The Coefficient Matrix "A" is \n'), disp(A);

fprintf ('The Constant/Known Matrix "B" is \n'), disp(B);

fprintf ('The Solution Matrix "X" is \n'), disp(X);