Вы находитесь на странице: 1из 25

OPTIMIZATION TECHNIQUES

ASSIGNMENT






Submitted by: Group Members:
Name: Nitesh Kumar Suresh Kumar Jat (2011UCE1155)
ID: 2011UCE1131 Nitesh Kumar (2011UCE1131)
Group: A
1. NEWTON RAPHSON (EXCEL):

Considering the following function as our problem:
f(x) = - 12 - 21x + 18x
2
24 x
3
Differentiating with respect to x:
f(x) = - 21 + 36x -72x
2
Now, x
i+1
= x
i
f(x)/f(x)
x
i+1
= x
i
(- 12 - 21x
i
+ 18x
i
2
24 x
i
3
) / ( - 21 + 36x
i
-72x
i
2
)
Error can be calculated as {(x
i+1
- x
i
)/ x
i
} * 100 %
Using theseformulas in excel as shown in figure:




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

2. CASE STUDY (CIVIL):

Consider flow through an open rectangular channel. Using
Manings formula, the various relationships for the flow can be
combined to produce this equation:

Q = { S
1/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 = Manings 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
for H. However, the depth can be determined numerically by
reformulating the equation as:
f(H) = { S
1/2
(BH)
5/3
} / { n (B+2H)
2/3
} Q = 0
Consider the following values:
Q = 5 m
3
/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
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.99571336656321600000
Iteration 2: h=0.70249619791775719000,
Error=1.06284687508667260000
Iteration 3: h=0.70229815475230084000,
Error=0.02819135050742826300
Iteration 4: h=0.70229295648381462000,
Error=0.00074017971584315406
>>
Hence, the value of H comes out to be 0.70229 m.



3. NAIVE-GAUSS ELIMINATION:

Consider the following set of equations is to be solved using Naive-
Gauss elimination:
4x
1
+ x
2
x
3
= 2
5x
1
+ x
2
+ 2x
3
= 4
6x
1
+ x
2
+ x
3
= 6
Writing in Matrix Form:
4 1 -1 x
1
-2
5 1 2 x
2
= 4
6 1 1 x
3
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 elimination
fori=k+1:n
xmult = A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j)-xmult*A(k,j);
end
b(i) = b(i)-xmult*b(k);
end
end
% back substitution
x(n) = b(n)/A(n,n);
fori=n-1:-1:1
sum = b(i);
for j=i+1:n
sum = sum-A(i,j)*x(j);
end
x(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:


B =

-2
4
6

Now, we use the function to find out the values of x
1
, x
2
and x
3
as shown
below:

>>naiv_gauss(A,B)

Which gives the following result:

ans =

3
-13
1

>>














4. SYSTEM OF LINEAR EQUATIONS:

Consider the following system of linear equations:

3x
1
0.1x
2
0.2x
3
= 7.85
0.1x
1
+ 7x
2
0.3x
3
= - 19.3
0.3x
1
+ 0.2x
2
+ 10x
3
= 71.4
Writing in Matrix Form:
3 -0.1 -0.2 x
1
7.85
0.1 7 -0.3 x
2
= -19.3
0.3 0.2 10 x
3
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]

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:

x
1
= 3.0067867
x
2
= -2.4958235
x
3
= 7.0997129




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
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.0997
The values obtained are:

x
1
= 3.0068
x
2
= -2.4958
x
3
= 7.0997


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 14
y 0.5 2.5 2 4 3.5 6 5.5

Consider the straight line equation as: y = a
0
+ a
1
x

Using Excel, the values of a
1
and a
0
can be calculated and the error
analysis can also be done:



Now, a
0
= 0.07142857 and a
1
= 0.41964286

So, the equation is y = 0.07142857 + 0.41964286 x



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 39
Y 29 21 29 14 21 15 7 7 13 0 3
We 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:

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:

We can use MATLAB for interpolation via Newtons 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

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

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 12
Now, 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 Newtons interpolating
polynomial:

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



7. CURVE FITTING:

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.5
Y 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:


X 2 4 6 8 10 12 14
Y 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 R
2
= 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
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:

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 are
Distance,m 2.4 1.5 2.4 1.8 1.8 2.9 1.2 3 1.2
Lane
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 this
line 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.55
With a R
2
value of 0.1225
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 = x
2
+ 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:

>> 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(x
1
,x
2
)=2+x
1
x
2
+2x
1
2
+2x
1
x
2
+x
2
2


We can find the minimum value using fminsearch in MATLAB. Taking
an initial guess of (-0.5,0.5)
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.5000
fval =
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) =x
2
+ 32/x
First, 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:
Warning: Gradient must be provided for trust-region algorithm;
using line-search algorithm instead.
> In fminunc at 341

Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the function tolerance.
<stopping criteria details>

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

Вам также может понравиться