EENG703 - Assignment04 (AutoRecovered)
EENG703 - Assignment04 (AutoRecovered)
Assignment-04
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)
Page 1 of 19
(b)
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
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);
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]
>> 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
>>
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
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
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
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
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
(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)
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