Optimization Techniques Assignment Nk

32
OPTIMIZATION TECHNIQUES ASSIGNMENT Submitted by: Group Members:

description

assignment on optimization technique

Transcript of Optimization Techniques Assignment Nk

Page 1: Optimization Techniques Assignment Nk

OPTIMIZATION TECHNIQUES ASSIGNMENT

Submitted by: Group Members:

Name: Nitesh Kumar Suresh Kumar Jat (2011UCE1155)

ID: 2011UCE1131 Nitesh Kumar (2011UCE1131)

Group: A

Page 2: Optimization Techniques Assignment Nk

1. NEWTON RAPHSON (EXCEL):

Considering the following function as our problem:

f(x) = - 12 - 21x + 18x2 – 24 x3

Differentiating with respect to x:

f’(x) = - 21 + 36x -72x2

Now, xi+1 = xi – f(x)/f’(x)

xi+1= xi – (- 12 - 21xi + 18xi2 – 24 xi

3) / ( - 21 + 36xi -72xi2)

Error can be calculated as {(xi+1 - xi)/ xi} * 100 %

Using theseformulas in excel as shown in figure:

The value comes out to be -0.382299009 after 7 iterations.

Page 3: Optimization Techniques Assignment Nk

2. CASE STUDY (CIVIL):

Consider flow through an open rectangular channel. Using

Maning’s formula, the various relationships for the flow can be

combined to produce this equation:

Q = { S1/2 (BH)5/3 } / { n (B+2H)2/3}

Where,

Q = Discharge

S = Slope of channel bottom

B = Width of the channel

H = Depth of water in the channel

n = Maning’s Coefficient.

Thus, the equation now contains a single unknown H along with the

given value for Q and the channel parameters (n, S, and B). Although we

have one equation with an unknown, it is impossible to solve explicitly

Page 4: Optimization Techniques Assignment Nk

for H. However, the depth can be determined numerically by

reformulating the equation as:

f(H) = { S1/2 (BH)5/3 } / { n (B+2H)2/3} – Q = 0

Consider the following values:

Q = 5 m3/s

B = 20 m

n = 0.03

S = 0.0002

f(H) = 0.471405 { (20H)5/3 } / {20+2H}2/3} – 5 = 0

This equation can be arranged in the following manner:

H = (1/20) (5/0.471405)3/5 (20+2H)2/5

Now, MATLAB can be used to solve it via fixed point iteration.

Using a criterion of 0.001% error, the MATLAB code is following:

>> h=1;

h_old=100;

iter=0;

while abs(((h_old-h)/h_old)*100) > 0.001

Page 5: Optimization Techniques Assignment Nk

h_old=h;

h=.05*((5/0.471405)^0.6)*(20 + 2*h)^0.4;

iter=iter + 1;

fprintf('Iteration %d: h=%.20f, Error=%.20f\n', iter, h, ((h_old-h)/h_old)*100);

pause;

end

Which produces the following output:

Iteration 1: h=0.71004286633436786000, Error=28.99571336656321600000Iteration 2: h=0.70249619791775719000, Error=1.06284687508667260000Iteration 3: h=0.70229815475230084000, Error=0.02819135050742826300Iteration 4: h=0.70229295648381462000, Error=0.00074017971584315406>>Hence, the value of H comes out to be 0.70229 m.

Page 6: Optimization Techniques Assignment Nk

3. NAIVE-GAUSS ELIMINATION:

Consider the following set of equations is to be solved using Naive-

Gauss elimination:

4x1 + x2 − x3= −2

5x1 + x2 + 2x3 = 4

6x1 + x2 + x3 = 6

Writing in Matrix Form:

4 1 -1 x1 -2

5 1 2 x2 = 4

6 1 1 x3 6

To solve it using MATLAB, first we need to create a *.m to hold the

function.

function x = naiv_gauss(A,b);n = length(b); x = zeros(n,1);for k=1:n-1 % forward eliminationfori=k+1:nxmult = A(i,k)/A(k,k);for j=k+1:nA(i,j) = A(i,j)-xmult*A(k,j);end

Page 7: Optimization Techniques Assignment Nk

b(i) = b(i)-xmult*b(k);endend% back substitutionx(n) = b(n)/A(n,n);fori=n-1:-1:1sum = b(i);for j=i+1:nsum = sum-A(i,j)*x(j);endx(i) = sum/A(i,i);end

Save this file as naiv_gauss.m.

Now we enter the [A] matrix in the command windows as:

A = [ 4 1 -1 ; 5 1 2 ; 6 1 1 ]

Which produces the following output:

A =

4 1 -1 5 1 2 6 1 1

Similarly, we save the B matrix as:

>> B = [ -2 ; 4 ; 6 ]

Which gives the following output:

Page 8: Optimization Techniques Assignment Nk

B =

-2 4 6

Now, we use the function to find out the values of x1, x2 and x3 as shown below:

>>naiv_gauss(A,B)

Which gives the following result:

ans =

3 -13 1

>>

Page 9: Optimization Techniques Assignment Nk

4. SYSTEM OF LINEAR EQUATIONS:

Consider the following system of linear equations:

3x1 – 0.1x2 – 0.2x3 = 7.85

0.1x1 + 7x2 – 0.3x3 = - 19.3

0.3x1 + 0.2x2 + 10x3 = 71.4

Writing in Matrix Form:

3 -0.1 -0.2 x1 7.85

0.1 7 -0.3 x2 = -19.3

0.3 0.2 10 x3 71.4

A. Using Excel:First we save matrix [A] and [B] as shown in figure below.Now, we can use the MINVERSE function to find out inverse of [A] and

save it in the form of matrix using the following formula:

=MINVERSE(B1:D3)

Now the matrix [X] can be found out using the following formula:

[X] = [A]-1[B]

Page 10: Optimization Techniques Assignment Nk

We can use the MMULT function to find put [X] using the following

formula:

=MMULT(B9:D11,C5:C7)

So, the result comes out to be:

x1 = 3.0067867

x2 = -2.4958235

x3 = 7.0997129

Page 11: Optimization Techniques Assignment Nk

B. Using MATLAB:First, we save the matrix [A]:

>> A = [ 3 -0.1 -0.2 ; 0.1 7 -0.3 ; 0.3 0.2 10 ]

Which gives the following output:

A =

3.0000 -0.1000 -0.2000 0.1000 7.0000 -0.3000 0.3000 0.2000 10.0000

Now, we save the matrix [B]:

>> B = [ 7.85 ; -19.3 ; 71.4 ]

Which gives the following output:

B =

7.8500 -19.3000 71.4000

Page 12: Optimization Techniques Assignment Nk

Now, we check the condition number of [A]:

>>cond(A)

ans =

3.3304

Which is very low.

Now, to find out X:

>> X=A\B

Which gives the following output:

X = 3.0068 -2.4958 7.0997The values obtained are:

x1 = 3.0068

x2 = -2.4958

x3 = 7.0997

Page 13: Optimization Techniques Assignment Nk

5. SIMPLE LINEAR REGRESSION:

A. Using Excel:Fit a straight line to the following values of x and y:

x 2 4 6 8 10 12 14y 0.5 2.5 2 4 3.5 6 5.5

Consider the straight line equation as: y = a0 + a1x

Using Excel, the values of a1 and a0 can be calculated and the error analysis can also be done:

Now, a0 = 0.07142857 and a1 = 0.41964286

So, the equation is y = 0.07142857 + 0.41964286 x

Page 14: Optimization Techniques Assignment Nk

B. Using MATLAB:Consider the following set of data to be fit in a straight line equation:

X 6 7 11 15 17 21 23 29 29 37 39Y 29 21 29 14 21 15 7 7 13 0 3We can fit a straight line using the regress function in MATLAB.First, we input the X data points:

>> x = [ 6 7 11 15 17 21 23 29 29 37 39]

Which gives the following output:

x =

6 7 11 15 17 21 23 29 29 37 39

Now, we input the Y data points:

>> y = [ 29 21 29 14 21 15 7 7 13 0 3 ]

Which gives the following output:

y =

29 21 29 14 21 15 7 7 13 0 3

Now, considering the straight line equation to be y = mx + c, the coefficients can be found out by using the following command:

>> [r,m,c] = regression(x,y)Which gives the following values:

Page 15: Optimization Techniques Assignment Nk

r =

-0.9015

m =

-0.7805

c =

31.0589

Where r is the regression value.

Now, the equation of the straight line can be written as:

y = mx + c

Putting the values, y = -0.7805 x + 31.0589

6. NEWTON INTERPOLATION:

Page 16: Optimization Techniques Assignment Nk

We can use MATLAB for interpolation via Newton’s divided difference method.

This is the MATLAB code for Newton interpolating function:

functionyi = NewtonInter(x,y,xi)n = length(x) - 1;ni = length(xi); D = ones(n,n);

for k = 1 : n

D(k,1) = (y(k+1)-y(k))/(x(k+1)-x(k));

end

for k = 2 : n

forkk = 1 : (n-k+1)

D(kk,k) = (D(kk+1,k-1) - D(kk,k-1))/(x(kk+k)-x(kk));

end

end

a(1) = y(1);

for k = 2 : (n+1)

a(k) = D(1,k-1);

end

Page 17: Optimization Techniques Assignment Nk

yi = a(n+1)*ones(1,ni);

for k = 1 : n

yi = a(n+1-k)+yi.*(xi-x(n+1-k));

end

Save this file as NewtonInter.m .

Now, we can use it to interpolate and find values on unknown data points.

First we enter the x data points:

>> x = [ 1 2 3 4 5 6 7 ]

Which shows the following output:

x =

1 2 3 4 5 6 7

Now, we enter the y data points:

>> y = [ 1 0.5 0.3333 0.25 0.2 0.1667 0.1429 ]

Which shows the following output:

y =

1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429

Page 18: Optimization Techniques Assignment Nk

Now, we enter the points xi at which the y values are to be found out:

>>xi = [ 8 9 10 11 12 ]

Which shows the following output:

xi =

8 9 10 11 12Now, we can use the function to find out the values at these data points in the following way:

>>NewtonInter(x,y,xi)

Which gives the function values:

ans =

0.2489 0.9928 3.6730 10.9229 27.3978

>>

So, these are the function values using Newton’s interpolating polynomial:

X 8 9 10 11 12Y 0.2489 0.9928 3.6730 10.9229 27.3978

7. CURVE FITTING:

Page 19: Optimization Techniques Assignment Nk

A. Using Excel:

We can use Excel to fit regression curves to a given set of data. Consider the following set of data in which a logarithmic curve is to be fit:

X 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5Y 0.53 0.69 1.5 1.5 2 2.06 2.28 2.23 2.73 2.42 2.79

We can use the logarithmic trendline to find out the equation of the curve.

The equation of the curve comes out to be: y = 0.9846ln(x) + 1.0004

And the regression coefficient comes out to be 0.9444

Excel can be used to fit a straight line too. Consider the following data points:

Page 20: Optimization Techniques Assignment Nk

X 2 4 6 8 10 12 14Y 6.5 7 13 17.8 19 25.8 26.9

We can use the linear trendline to find out the equation of the best fit straight line.

The equation comes out to be:

y = 1.8714x + 1.6

With a regression coefficient value of R2 = 0.9684

B. Using MATLAB:Consider the following data set in which a cubical spline is to be fit:

X 2 4 6 8 10 12

Page 21: Optimization Techniques Assignment Nk

Y 20 20 12 7 6 6

First, we enter the X data points:

>> X = [ 2 4 6 8 10 12 ]

Which gives the output:X =

2 4 6 8 10 12

Now, we enter the Y data points:

Y = [ 20 20 12 7 6 6 ]

Which gives the output:

Y =

20 20 12 7 6 6

Now, type the following code in MATLAB command window:

>>xi=2:.25:12;>>yi=spline(X,Y,xi);>>plot(X,Y,'o',xi, yi)>>Which plots the function and the resulting plot produced is shown below:

Page 22: Optimization Techniques Assignment Nk

8. CASE STUDIES (CIVIL):Consider the following problem:

A transportation engineering study was conducted to determine the proper design of bike lanes. Data were gathered on bike lane widths and average distance between bikes and passing cars.The data from nine streets areDistance,m 2.4 1.5 2.4 1.8 1.8 2.9 1.2 3 1.2Lane width, m

2.9 2.1 2.3 2.1 1.8 2.7 1.5 2.9 1.5

(a) Plot the data.(b) Fit a straight line to the data with linear regression. Add thisline to the plot.(c) If the minimum safe average distance between bikes and passing cars is considered to be 2 m, determine the correspondingminimum lane width.Now we can use Excel to plot the data values and fit a straight line with linear regression.The equation of the straight line comes out to be y = -0.07 x + 2.55With a R2 value of 0.1225

Page 23: Optimization Techniques Assignment Nk

The lane width for 2m distance can be predicted using the forecast function in the following way: =FORECAST(2, B2:B10, A2:A10)

9.USE OF FMIN IN MATLAB:A. FMINBND:

It is used to find out the minimum value of a function within a selected range (local minima).

Consider the function: y = x2 + 54/x

First, we need to create a *.m file to save the function we want to minimize.

function [ y ] = my_fun( x )

y=x^2 + 54/x;end

Save it as my_fun.m .

Now we want to find the minimum value within range (0,5). Type the following code in Command Window:

Page 24: Optimization Techniques Assignment Nk

>> x=fminbnd(@my_fun,0,5)

It will produce the following output:

x =

3.0000

>>

Hence the function has a local minima at x = 3.

Now, we can also find the value of function at this point by typing the following code:

>>my_fun(3)

Which will give the following result:

ans =

27

>>

B.FMINSEARCH:It is used to find the minimum value of a multivariable function.Consider the following two variable function to be minimized:

f(x1,x2)=2+x1−x2+2x12+2x1x2+x2

2

We can find the minimum value using fminsearch in MATLAB. Taking an initial guess of (-0.5,0.5)

Page 25: Optimization Techniques Assignment Nk

Type the following code in MATLAB command window:

>> f=@(x) 2+x(1)-x(2)+2*x(1)^2+2*x(1)*x(2)+x(2)^2;[x,fval]=fminsearch(f,[-0.5,0.5])

It will produce the following output:x = -1.0000 1.5000fval =0.7500>>

Which shows the function value is minimum at X = (-1,1.5) and the minimum value of function is 0.75

C. FMINUNC:It is used to find out the global minima of a function.Consider the following function is to be minimized:f(x) =x2 + 32/xFirst, we need to create a *.m file to save the function.

function [ y ] = my_fun2( x )y=x^2 +32/x;end

Save it as my_fun2.m .Now, to find out the minima, we have to type the following code in the command window (Taking initial guess as x=1):>> x = fminunc(@my_fun2,1)

Which produces the following output:

Page 26: Optimization Techniques Assignment Nk

Warning: Gradient must be provided for trust-region algorithm;using line-search algorithm instead. > In fminunc at 341Local minimum found.Optimization completed because the size of the gradient is less thanthe default value of the function tolerance.<stopping criteria details>

x =2.5198>>Hence, the minima is at x = 2.5198