0% found this document useful (0 votes)
68 views19 pages

EENG703 - Assignment04 (AutoRecovered)

This document is a 3 page assignment submitted in partial fulfillment of the requirements for the Doctorate of Philosophy (Ph.D) in Chemical Engineering program at the University of Bahrain. It was prepared by Eng. Faisal Mumtaz Ahmad, supervised by Dr. Mohammed Bin Shams, and submitted in December 2021. The assignment contains solutions to optimization problems using various numerical methods like interval halving, dichotomous method, and Newton's method. Graphs and calculations are shown to find the minimum or zero of functions over different intervals.

Uploaded by

Faisal Mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views19 pages

EENG703 - Assignment04 (AutoRecovered)

This document is a 3 page assignment submitted in partial fulfillment of the requirements for the Doctorate of Philosophy (Ph.D) in Chemical Engineering program at the University of Bahrain. It was prepared by Eng. Faisal Mumtaz Ahmad, supervised by Dr. Mohammed Bin Shams, and submitted in December 2021. The assignment contains solutions to optimization problems using various numerical methods like interval halving, dichotomous method, and Newton's method. Graphs and calculations are shown to find the minimum or zero of functions over different intervals.

Uploaded by

Faisal Mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

University of Bahrain

College of Engineering Department of Chemical Engineering

Assignment-04

This assignment is submitted in partial fulfillment of the requirements for


the course of ‘Optimization Techniques’ (ENGG703)’ part of
Doctorate of Philosophy (Ph.D) in Chemical Engineering

Prepared by:
Eng. Faisal Mumtaz Ahmad
20101361

Supervised by:
Dr. Mohammed Bin Shams
(Assoc. Professor)
University of Bahrain

Kingdom of Bahrain

December 2021
Answer:
(a)

With initial guess x0=40; increment=5;

[ -35 < x* < 25 ]

Page 1 of 19
(b)

It seems the minimum is at negative infinity and cannot be bracketed


manually.

However using Matlab, with initial guess 5 and step size 5; we get
[ -2.2E103 < X* < -5.5E102 ]

Page 2 of 19
(c)

On using initial guesses less than 1, the code gives an error implying
that it cnt be bracketed with that initial guess. On using initial guess
10 and step size 1 the bracketing was achieved at negative infinity.
[-1.7 E103 < x* < -4.479E102]

Page 3 of 19
(d)

From the graph it is learnt that there are two minima and hence based
on the initial guess it will be bracketed.
At initial guess -10, step size 1; [-7 < x* < 5]
At initial guess 10, step size 1; [7 < x* < -5]

(e)

[0.24<x*<0.54]

Page 4 of 19
(a) For interval halving of f(x)= exp(x)+1.5*(x^2) on choosing [-10,10]
clc
clear
f=@(x) exp(x)+1.5*(x^2);
a=-10;
b=10;
L=b-a
xm=(1/2)*(a+b)
x1=a+(1/4)*L
x2=b-(1/4)*L
while abs(L)>1e-4
if f(x1)<f(xm)
a=a
b=xm
L=b-a
xm=x1
x1=a+(1/4)*L
x2=b-(1/4)*L
else
if f(x1)>=f(xm) && f(x2)<f(xm)
a=xm
b=b
L=b-a
xm=x2
x1=a+(1/4)*L
x2=b-(1/4)*L
else
if f(x1)>=f(xm) && f(x2)>=f(xm)
a=x1
b=x2
L=b-a
xm=xm
x1=a+(1/4)*L
x2=b-(1/4)*L
end
end
end
end

a = -0.2577
b = -0.2576
L = 7.6294e-05
xm = -0.2576
x1 = -0.2577
x2 = -0.2576
Page 5 of 19
(b) For interval halving of f(x)= exp(x)+1.5*(x^2) on choosing [-100,100]
clc
clear
f=@(x) 0.5*((x^2)+1)*(x+1);
a=-10;
b=10;
L=b-a
xm=(1/2)*(a+b)
x1=a+(1/4)*L
x2=b-(1/4)*L
while abs(L)>1e-4
if f(x1)<f(xm)
a=a
b=xm
L=b-a
xm=x1
x1=a+(1/4)*L
x2=b-(1/4)*L
else
if f(x1)>=f(xm) && f(x2)<f(xm)
a=xm
b=b
L=b-a
xm=x2
x1=a+(1/4)*L
x2=b-(1/4)*L
else
if f(x1)>=f(xm) && f(x2)>=f(xm)
a=x1
b=x2
L=b-a
xm=xm
x1=a+(1/4)*L
x2=b-(1/4)*L
end
end
end

end

No matter how big negative bound is chosen, it keeps moving to negative


infinity. Hence the solution lies somewhere in negative infinity or it is the
negative infinity.

a = -100
b = -99.9999
L = 9.5367e-05
xm = -100.0000
x1 = -100.0000
x2 = -99.9999

Page 6 of 19
(c) For Fibonacci method of f(x)= (x^3)-3*x on choosing [-3,3]
clc;
clear all;
n=input('Enter the no of iterations : \n');
N=n+1;
a=input('\n Enter lower limit : \n');
b=input('\n\nEnter upper limit : \n');
fold=1;
fnew=1;
func = @(x)((x^3)-(3*x));
for i=1:N
if i==1 || i==2
f(i)=1;
continue;
end
f(i)=fold+fnew;
fold=fnew;
fnew=f(i);
end
L2=(b-a)*f(N-2)/f(N);
j=2;
while j<N
L1=(b-a);
if L2>L1/2
anew=b-L2;
bnew=a+L2;
else if L2<=L1/2
anew=a+L2;
bnew=b-L2;
end
end
k1=func(anew);
k2=func(bnew);
if k2>k1
b=bnew;
L2=f(N-j)*L1/f(N-j+2);
else if k2<k1
a=anew;
L2=f(N-j)*L1/f(N-(j-2));
else if k2==k1
b=bnew;
L2=f(N-j)*[b-a]/f(N-(j-2));
j=j+1;
end
end
end
j=j+1;
end
disp(a);
disp(b);

Enter the no of iterations : 100


Enter lower limit : -3
Enter upper limit : 3
1.0000
1.0000
Page 7 of 19
(d) For Dichotomous method of f(x)= 2*(x^2)*(x-2)*(x+2) on
choosing [-10,10]

clc
clear all
close all
f=@(x) (x.^3)-3*x ;
epsil=0.01;
k=1;
a(k)=-10;
b(k)=10;
L(k)=abs(b(k)-a(k));
UC=0.05;
while L(k)>UC
Alpha(k)=((a(k)+b(k))/2)-epsil;
Beta(k)=((a(k)+b(k))/2)+epsil;
if f(Alpha(k))>=f(Beta(k))
a(k+1)=Alpha(k);
b(k+1)=b(k);
else
b(k+1)=Beta(k);
a(k+1)=a(k);
end
L(k+1)=abs(b(k+1)-a(k+1));
k=k+1;
end
x_star=(a(k)+b(k))/2;
f(x_star);
disp(['Minimum at f(',num2str(x_star),')=',num2str(f(x_star))])
% str = sprintf('Iteration \t a');
T=[[1:k]' a' b' L' f(a)' f(b)'];
% [str;T]

Minimum at f(1.0049)=-1.9999

Page 8 of 19
(e) Golden section search method of |
f(x)= 0.1*(x^6)-0.29*(x^5)+2.31*(x^4)-8.33*(x^3)+12.89*(x^2)-
6.8*x+1 on choosing [-10,10]

figure; hold on;


a=0; % start of interval
b=2; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient, around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);
f_x1=f(x1);
f_x2=f(x2);
plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);
f_x1=f(x1);
f_x2=f(x2);
plot(x2,f_x2,'rx')
end
k=k+1;
end
% chooses minimum point
if(f_x1<f_x2)
sprintf('x_min=%f', x1)
sprintf('f(x_min)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_min=%f', x2)
sprintf('f(x_min)=%f ', f_x2)
plot(x2,f_x2,'ro')
end

>> golden
ans = 'x_min=0.392627'
ans = 'f(x_min)=-0.134418 '

Page 9 of 19
The function has a local minimum which is a global minimum as seen from
the graph (1,0).
F(x)= (x-1)4.
F’(x)=4(x-1)3. ;on equating this to zero to find the zero of the polynomial,
we get x=1.
F’’(x)=12(x-1)2. On equating the x=1 stationary point to check for second
order; we learn that it satisfies the necessary condition for a local minimum
as it equates to zero.

For Newton:
clc
clear
close all
syms x
x0=0.95;
f=@(x) (x-1)^4;
k=1;
epsil=1e-4;
Page 10 of 19
f_p1=diff(f(x),1);
f_p2=diff(f(x),2);
Ev_f_p1=@(y1) subs(f_p1,y1);
Ev_f_p2=@(y2) subs(f_p2,y2);
x(k)=x0;
x(k+1)=x(k)-((Ev_f_p1(x(k)))/(Ev_f_p2(x(k))));
x_n=double(vpa(x(k+1)));
% disp(['x(',num2str(k+1),')=', (num2str(x_n))])
Cr=abs(double(Ev_f_p1(x(k+1))));
while Cr>epsil
x(k+1)=x(k)-((Ev_f_p1(x(k)))/(Ev_f_p2(x(k))));
x_n=double(vpa(x(k+1)));
Cr=abs(double(Ev_f_p1(x(k+1))));
disp(['x(',num2str(k),')=', (num2str(x_n)),...
',f`(',num2str(k),')=',num2str(Cr)])
k=k+1;
end

for x=-1;
x(1)=-0.33333,f`(1)=9.4815
x(2)=0.11111,f`(2)=2.8093
x(3)=0.40741,f`(3)=0.83239
x(4)=0.60494,f`(4)=0.24664
x(5)=0.73663,f`(5)=0.073077
x(6)=0.82442,f`(6)=0.021652
x(7)=0.88294,f`(7)=0.0064155
x(8)=0.92196,f`(8)=0.0019009
x(9)=0.94798,f`(9)=0.00056323
x(10)=0.96532,f`(10)=0.00016688
x(11)=0.97688,f`(11)=4.9447e-05
>>

For x=-0.5;
x(1)=0,f`(1)=4
x(2)=0.33333,f`(2)=1.1852
x(3)=0.55556,f`(3)=0.35117
x(4)=0.7037,f`(4)=0.10405
x(5)=0.80247,f`(5)=0.030829
x(6)=0.86831,f`(6)=0.0091346
x(7)=0.91221,f`(7)=0.0027066
x(8)=0.94147,f`(8)=0.00080194
x(9)=0.96098,f`(9)=0.00023761
x(10)=0.97399,f`(10)=7.0404e-05
>> >>
Page 11 of 19
For x=0 ;
x(1)=0.33333,f`(1)=1.1852
x(2)=0.55556,f`(2)=0.35117
x(3)=0.7037,f`(3)=0.10405
x(4)=0.80247,f`(4)=0.030829
x(5)=0.86831,f`(5)=0.0091346
x(6)=0.91221,f`(6)=0.0027066
x(7)=0.94147,f`(7)=0.00080194
x(8)=0.96098,f`(8)=0.00023761
x(9)=0.97399,f`(9)=7.0404e-05
>>

For Secant (Quasi-newton) method:


m=inputdlg('Enter the function with RHS=0:');
s=m{:};
d=str2func(['@(x)' s]);
f=str2func(['@(x)' vectorize(s)])
x=0:0.01:10;
plot(x,f(x));
grid;
p=input('The 1st initial guess is:');
r=input('The 2nd initial guess is:');
term=0;
%Lets the user choose which termination criterion it desires, whether be it a
relative error or number of iterations
while (term<1)||(term>2)
term=input('Please choose what termination criteria you desire.\nPress "1"
for approximate relative error and "2" for number of iterations:');
if term==1
e=input('The desired approximate relative error in % is:');
itr=NaN;
break;
end
if term==2
itr=input('The desired number of iterations is:');
e=NaN;
break;
end
fprintf('Wrong input! Please try again.\n\n');
end
n=0;
fprintf('\n');
%This is for labels
q='itrn';
v='previous x';
j='current x';
w='solved root';
o='%rel. error';
b='current f(x)';
k=' ';
a=[q,k,v,k,j,k,w,k,b,k,o];
disp(a);

Page 12 of 19
%This is tthe loop program
for i=1:100
z=p;
fp=f(p);
fr=f(r);
pprime=(fp-fr)/(p-r);
p=r;
fp=f(p);
r=p-(fp/pprime);
g=(abs(r-p)/abs(r))*100;
fprintf('%2.0f %13.4f %13.4f %13.4f %13.4f %16.4f\n',n,z,p,r,fp,g);
n=n+1;
if (g<=e)||(n==itr) break;
end
end
%Interpretation of results
if term==1
fprintf('\nThe desired relative error of %f is now reached at %.0fth
iteration,\nwith a solved root of %.9f\n',e,n-1,r);
end
if term==2
fprintf('\nThe desired number of iterations of %.0f is now reached at %.0fth
iteration,\nwith a solved root of %.9f\n',itr,n-1,r);
end
return

For x=0 ;
f = function_handle with value: @(x)(x-1).^4
The 1st initial guess is:0
The 2nd initial guess is:0.1
Please choose what termination criteria you desire.
Press "1" for approximate relative error and "2" for number of iterations:2
The desired number of iterations is:20

itrn previous x current x solved root current f(x) %rel. error


0 0.0000 0.1000 0.2908 0.6561 65.6100
1 0.1000 0.2908 0.4105 0.2530 29.1679
2 0.2908 0.4105 0.5198 0.1207 21.0294
3 0.4105 0.5198 0.6058 0.0532 14.1908
4 0.5198 0.6058 0.6774 0.0241 10.5631
5 0.6058 0.6774 0.7356 0.0108 7.9193
6 0.6774 0.7356 0.7835 0.0049 6.1056
7 0.7356 0.7835 0.8226 0.0022 4.7592
8 0.7835 0.8226 0.8547 0.0010 3.7536
9 0.8226 0.8547 0.8810 0.0004 2.9827
10 0.8547 0.8810 0.9025 0.0002 2.3852
11 0.8810 0.9025 0.9201 0.0001 1.9164
12 0.9025 0.9201 0.9346 0.0000 1.5456
13 0.9201 0.9346 0.9464 0.0000 1.2503

Page 13 of 19
14 0.9346 0.9464 0.9561 0.0000 1.0138
15 0.9464 0.9561 0.9640 0.0000 0.8236
16 0.9561 0.9640 0.9705 0.0000 0.6702
17 0.9640 0.9705 0.9759 0.0000 0.5460
18 0.9705 0.9759 0.9802 0.0000 0.4453
19 0.9759 0.9802 0.9838 0.0000 0.3634

The desired number of iterations of 20 is now reached at 19th iteration,


with a solved root of 0.983802593

For x=-1 ;
>> Secantmethod
f = function_handle with value: @(x)(x-1).^4
The 1st initial guess is:-1
The 2nd initial guess is:-1.1
Please choose what termination criteria you desire.
Press "1" for approximate relative error and "2" for number of iterations:2
The desired number of iterations is:20

itrn previous x current x solved root current f(x) %rel. error


0 -1.0000 -1.1000 -0.5360 19.4481 105.2329
1 -1.1000 -0.5360 -0.3098 5.5659 72.9872
2 -0.5360 -0.3098 -0.0560 2.9435 453.2323
3 -0.3098 -0.0560 0.1297 1.2436 143.1882
4 -0.0560 0.1297 0.2887 0.5738 55.0876
5 0.1297 0.2887 0.4168 0.2559 30.7301
6 0.2887 0.4168 0.5224 0.1157 20.2170
7 0.4168 0.5224 0.6087 0.0520 14.1772
8 0.5224 0.6087 0.6795 0.0234 10.4143
9 0.6087 0.6795 0.7375 0.0106 7.8578
10 0.6795 0.7375 0.7849 0.0048 6.0484
11 0.7375 0.7849 0.8238 0.0021 4.7206
12 0.7849 0.8238 0.8557 0.0010 3.7231
13 0.8238 0.8557 0.8818 0.0004 2.9595
14 0.8557 0.8818 0.9032 0.0002 2.3670
15 0.8818 0.9032 0.9207 0.0001 1.9021
16 0.9032 0.9207 0.9350 0.0000 1.5342
17 0.9207 0.9350 0.9468 0.0000 1.2412
18 0.9350 0.9468 0.9564 0.0000 1.0065
19 0.9468 0.9564 0.9643 0.0000 0.8178
Page 14 of 19
The desired number of iterations of 20 is now reached at 19th iteration,
with a solved root of 0.964276940

For x=-0.5;
>> Secantmethod
f = function_handle with value @(x)(x-1).^4
The 1st initial guess is:-.5
The 2nd initial guess is:-.55
Please choose what termination criteria you desire.
Press "1" for approximate relative error and "2" for number of iterations:2
The desired number of iterations is:20

itrn previous x current x solved root current f(x) %rel. error


0 -0.5000 -0.5500 -0.1432 5.7720 283.9768
1 -0.5500 -0.1432 0.0277 1.7082 616.2314
2 -0.1432 0.0277 0.2153 0.8935 87.1115
3 0.0277 0.2153 0.3535 0.3792 39.1054
4 0.2153 0.3535 0.4716 0.1747 25.0338
5 0.3535 0.4716 0.5668 0.0780 16.7942
6 0.4716 0.5668 0.6452 0.0352 12.1590
7 0.5668 0.6452 0.7093 0.0158 9.0387
8 0.6452 0.7093 0.7619 0.0071 6.8997
9 0.7093 0.7619 0.8050 0.0032 5.3479
10 0.7619 0.8050 0.8402 0.0014 4.1975
11 0.8050 0.8402 0.8691 0.0007 3.3240
12 0.8402 0.8691 0.8928 0.0003 2.6508
13 0.8691 0.8928 0.9122 0.0001 2.1253
14 0.8928 0.9122 0.9281 0.0001 1.7112
15 0.9122 0.9281 0.9411 0.0000 1.3824
16 0.9281 0.9411 0.9517 0.0000 1.1197
17 0.9411 0.9517 0.9605 0.0000 0.9089
18 0.9517 0.9605 0.9676 0.0000 0.7391
19 0.9605 0.9676 0.9735 0.0000 0.6018
The desired number of iterations of 20 is now reached at 19th iteration,
with a solved root of 0.973462238

Page 15 of 19
For maximize; F(x)= -[100-(10-x1)2-(5-x2)2]
dfx/dfx1= -2(10-x1)
df2x/dfx1= 2
dfx/dfx2= -2(5-x2)
df2x/dfx2= 2
df2x/dfx1x2=0
H(x)= 0 2 [ 2 0]
Crosscheck: >> syms x y
>> f = -[100-(10-x)^2-(5-y)^2];
>> hessian(f,[x,y])
ans =[2, 0]
[0, 2]
Lamda1=2
Lamda2=2

Hence it is strictly convex and hence it is Positive definite.

(a)
function y=him(x)
x1=x(1);
x2=x(2);
y=-(100-(10-x1)^2-(5-x2)^2);
>> x0=[0,0];
>> options=optimset('display','iter','tolfun',1e-8);
>> [x, fval, exitflag]=fminsearch('him',[0 0],options)

Iteration Func-count min f(x) Procedure


0 1 25
1 3 24.995 initial simplex
2 5 24.9888 expand
3 7 24.9806 expand
4 9 24.9641 expand
5 11 24.9395 expand
6 13 24.8942 expand
7 15 24.8225 expand
8 17 24.6959 expand
9 19 24.4894 expand
10 21 24.1337 expand
11 23 23.5447 expand
12 25 22.5437 expand
13 27 20.8792 expand
14 29 18.0846 expand
15 31 13.4722 expand
Page 16 of 19
16 33 5.89858 expand
17 35 -6.19476 expand
18 37 -24.7852 expand
19 39 -50.8506 expand
20 41 -80.0073 expand
21 43 -88.4292 reflect
22 44 -88.4292 reflect
23 46 -88.4292 contract inside
24 48 -90.4927 contract inside
25 49 -90.4927 reflect
26 51 -90.4927 contract inside
27 53 -90.4927 contract inside
28 55 -90.4927 contract inside
29 57 -90.4927 contract outside
30 59 -90.5309 expand
31 61 -90.5309 contract inside
32 63 -90.5671 expand
33 65 -90.6406 expand
34 67 -90.728 expand
35 69 -90.9437 expand
36 71 -91.1195 expand
37 73 -91.686 expand
38 74 -91.686 reflect
39 76 -92.7412 expand
40 78 -93.2822 expand
41 80 -95.2924 expand
42 82 -96.966 expand
43 84 -99.3171 expand
44 86 -99.9503 reflect
45 88 -99.9503 contract outside
46 90 -99.9503 contract outside
47 92 -99.9503 contract inside
48 94 -99.9732 contract inside
49 96 -99.9866 contract inside
50 98 -99.9978 contract inside
51 100 -99.9978 contract outside
52 102 -99.9978 contract inside
53 104 -99.9994 reflect
54 106 -99.9995 contract inside
55 108 -99.9997 contract inside
56 110 -99.9999 contract inside
57 112 -99.9999 contract outside
58 114 -99.9999 contract inside
59 116 -100 contract inside
60 118 -100 contract inside
61 120 -100 contract outside
62 122 -100 contract inside
63 124 -100 reflect
64 126 -100 contract outside
65 128 -100 contract inside
66 130 -100 contract inside
67 132 -100 contract outside
68 134 -100 contract inside
69 136 -100 contract inside
70 138 -100 contract inside
71 140 -100 contract inside
72 142 -100 reflect
73 144 -100 contract inside
74 146 -100 contract inside

Optimization terminated:

Page 17 of 19
the current x satisfies the termination criteria using OPTIONS.TolX of
1.000000e-04
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of
1.000000e-08
The maximum sits at:
x = 10.0000 5.0000
fval = -100.0000
exitflag = 1

Page 18 of 19

You might also like