0% found this document useful (0 votes)
14 views30 pages

University of Dhaka, MTH-450 Assignment 4 Solution

Uploaded by

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

University of Dhaka, MTH-450 Assignment 4 Solution

Uploaded by

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

Lab Assignment – IV

(Q1)

Solution:
clc
syms x y z
d1= divergence([-y,x],[x,y]);
c1= curl([-y,x,0],[x,y,z]);
d2= divergence([-x,-y],[x,y]);
c2= curl([-x,-y,0],[x,y,z]) ;
d3= divergence([y,-x],[x,y]);
c3= curl([y,-x,0],[x,y,z]);
d4= divergence([x,y],[x,y]);
c4= curl([x,y,0],[x,y,z]);

if d1>0
disp('divergence=')
disp(d1)
disp('fluid in (i) is divergent')
elseif d1<0
disp('divergence=')
disp(d1)
disp('fluid in (i) is convergent')
else
disp('divergence=')
disp(d1)
disp('fluid in (i) is solinoidal')
end

if d2>0
disp('divergence=')
disp(d2)
disp('fluid in (ii) is divergent')
elseif d2<0
disp('divergence=')
disp(d2)
disp('fluid in (ii) is convergent')
else
disp('divergence=')
disp(d2)
disp('fluid in (ii) is solinoidal')
end

if d3>0
disp('divergence=')
disp(d3)
disp('fluid in (iii) is divergent')
elseif d3<0
disp('divergence=')
disp(d3)
disp('fluid in (iii) is convergent')
else
disp('divergence=')
disp(d3)
disp('fluid in (iii) is solinoidal')
end

if d4>0
disp('divergence=')
disp(d4)
disp('fluid in (iv) is divergent')
elseif d4<0
disp('divergence=')
disp(d4)
disp('fluid in (iv) is convergent')
else
disp('divergence=')
disp(d4)
disp('fluid in (iv) is solinoidal')
end

if c1==[0;0;0]
disp('fluid in (i) is irrotational')
disp('curl=')
disp(c1)
else
disp('fluid in (i) is rotational')
disp('curl=')
disp(c1)
end

if c2==[0;0;0]
disp('fluid in (ii) is irrotational')
disp('curl=')
disp(c2)
else
disp('fluid in (ii) is rotational')
disp('curl=')
disp(c2)
end

if c3==[0;0;0]
disp('fluid in (iii) is irrotational')
disp('curl=')
disp(c3)
else
disp('fluid in (iii) is rotational')
disp('curl=')
disp(c3)
end
if c4==[0;0;0]
disp('fluid in (iv) is irrotational')
disp('curl=')
disp(c4)
else
disp('fluid in (iv) is rotational')
disp('curl=')
disp(c4)
end
clear x y
[x,y]=meshgrid(-4:0.2:4,-4:0.2:4);
u1=-y;
v1= x;
u2=-x;
v2=-y;
u3= y;
v3=-x;
u4= x;
v4= y;
figure()
quiver(x,y,u1,v1,1)
title('figure of (i)')
axis tight
figure()
quiver(x,y,u2,v2,1)
title('figure of (ii)')
axis tight
figure()
quiver(x,y,u3,v3,1)
title('figure of (iii)')
axis tight
figure()
quiver(x,y,u4,v4,1)
title('figure of (iv)')
axis tight

Output:
divergence=

fluid in (i) is solinoidal

divergence=

-2

fluid in (ii) is convergent

divergence=

0
fluid in (iii) is solinoidal

divergence=

fluid in (iv) is divergent

fluid in (i) is rotational

curl=

fluid in (ii) is irrotational

curl=

fluid in (iii) is rotational

curl=

-2

fluid in (iv) is irrotational

curl=

0
(Q2)

Solution:
clc
syms u(x,y) v(x,y) a c d psi(x,y)
u(x,y)=a*(x^2-y^2);
v(x,y)=-2*a*x*y;
psi(x,y)=int(u,y)+int(-v-diff(int(u,y),x),x); %stream function
c1=strcat('stream fuction:',char(psi(x,y)));
disp(c1)
psi(x,y)=subs(psi(x,y),a,1);
figure
for i=-3:3
g1=ezplot(psi(x,y)+i);
set(g1,'color','m')
hold on
end
[X,Y]=meshgrid(-3:0.2:3,-3:0.2:3);
U=X.^2-Y.^2; %for arbitrary a=1
V=-2.*X.*Y;
quiver(X,Y,U,V,2)
title('streamlines')
axis([-3 3 -3 3])
hold off

Output:
stream fuction:(a*y*(3*x^2 - y^2))/3
(Q3)

Solution:
clc
syms u(x,y) v(x,y) z psi(x,y) phi(x,y)
u=-2*(x^2-y^2);
v=4*x*y;
d(x,y)=diff(u,x)+diff(v,y);
if d==0
disp('motion is possible for incompressible fluid')
else
disp('motion is possible for incompressible fluid')
end

if d==0
disp('stream function exist')
psi(x,y)=int(u,y)+int(-v-diff(int(u,y),x),x);
c1=strcat('stream fuction:',char(psi(x,y)));
disp(c1)
else
disp('stream function does not exist')
end

c=curl([u,v,0],[x,y,z]);
if c==[0;0;0];
disp('velocity potential exists')
phi(x,y)=int(u,x)+int(v-diff(int(u,x),y),y);
c2=strcat('velocity potential:',char(phi(x,y)));
disp(c2)
figure()
for i=-2:2
g1=ezplot(phi(x,y)+i);
set(g1,'color','red')
hold on
end
hold off
title('equipotential lines')
else
disp('velocity potential does not exists')
end

Output:
motion is possible for incompressible fluid
stream function exist
stream fuction:(2*y^3)/3 - 2*x^2*y
velocity potential exists
velocity potential:2*x*y^2 - (2*x^3)/3

(Q4)

Solution:
clc
syms phi(x,y) psi(x,y) u(x,y) v(x,y)
phi(x,y)=x^2-y^2;
u(x,y)=diff(phi(x,y),x);
v(x,y)=diff(phi(x,y),y);
[X,Y]=meshgrid(-3:0.2:3,-3:0.2:3);
figure()
quiver(X,Y,subs(u,{x,y},{X,Y}),subs(v,{x,y},{X,Y}))
axis([-3 3 -3 3])

psi(x,y)=int(u,y)+int(-v-diff(int(u,y),x),x); %stream function


c1=strcat('stream fuction:',char(psi(x,y)));
disp(c1)
flowrate=psi(2,2)-psi(2,0);
fprintf('flow rate between streamline at (2,0) and (2,2) is=
%3.2f\n',flowrate)
figure()
for i=-3:3
g1=ezplot(phi(x,y)+i);
set(g1,{'color','linestyle'},{'black','--'})
hold on
end
title('streamlines')

Output:
stream fuction:2*x*y
flow rate between streamline at (2,0) and (2,2) is= 8.00
Optimization
(Q1)

(a)
Solution:
clc
f = [1; 2]; % Minimize z = x + 2*y
A = [1, -2; 3, 2; -1, 1];
b = [0; 10; 1];
lb = [1; 0]; % Lower bounds
ub = [3; 5]; % Upper bounds
[x_opt, z_min] = linprog(f, A, b, [], [], lb, ub);
var_Names = {'Variable', 'OptimalValue'};
opt_values = {'x', x_opt(1); 'y', x_opt(2); 'Objective (z)', z_min};
T = cell2table(opt_values, 'VariableNames', var_Names);
disp('Optimal solution:')
disp(T)

Output:
Optimization terminated.

Optimal solution:

Variable OptimalValue

_______________ ____________

'x' 1

'y' 0.5

'Objective (z)' 2

(b)
Solution:
clc
f = [-3; -2; 2]; % Max z = 3*x1+2*x2-2*x3 --> Min -z = -3*x1-2*x2+2*x3
% Inequality constraints
A = [2, 1, 1; -1, -2, 1]; % x1+2*x2-x3 >= 3 --> -x1-2*x2+x3 <= -3
b = [8; -3];
alpha = 1;
beta = 1;
lb = [1; 0; 2*beta]; % Lower bounds
ub = [3*alpha; 3; Inf]; % Upper bounds
[x_opt1, z_max1] = linprog(f, A, b, [], [], lb, ub);
fprintf('Solution for alpha = 1 and beta = 1:\n');
table1 = table([x_opt1; -z_max1], 'VariableNames', {'Value'}, ...
'RowNames', {'x1', 'x2', 'x3', 'Maximized z'});
disp(table1);

fprintf('\nSolutions for different values of alpha and beta:\n');


alpha_vals = 1:0.5:5;
beta_vals = 1:0.5:3;
results = [];
for alpha = alpha_vals
for beta = beta_vals
lb = [1; 0; 2*beta];
ub = [3*alpha; 3; Inf];

[x_opt, z_max] = linprog(f, A, b, [], [], lb, ub);


results = [results; alpha, beta, x_opt', -z_max];
end
end
table2 = array2table(results, ...
'VariableNames', {'alpha', 'beta', 'x1', 'x2', 'x3', 'Maximized_z'});
disp(table2);
lb = [1; 0; 2]; %beta=1
ub = [3; Inf; Inf]; % Remove the upper bound on x2
[x_opt_unbounded, z_max_unbounded] = linprog(f, A, b, [], [], lb, ub);
fprintf('\nSolution when x2 is unbounded:\n');
table3 = table([x_opt_unbounded; -z_max_unbounded], 'VariableNames',
{'Value'}, ...
'RowNames', {'x1', 'x2', 'x3', 'Maximized z'});
disp(table3);

[x1_vals, x2_vals] = meshgrid(0:0.1:10, 0:0.1:10);


x3_val = zeros(size(x1_vals)); % Assume x3 = 0 for 2D projection
C1 = (2*x1_vals + x2_vals + x3_val <= 8);
C2 = (x1_vals + 2*x2_vals - x3_val >= 3);
feasible_area = C1 & C2;
figure;
surf(x1_vals, x2_vals, double(feasible_area), 'EdgeColor', 'none');
xlabel('x1');
ylabel('x2');
zlabel('Feasibility');
title('Feasible Region (x3 = 0)');
colormap([0.8 0.8 1; 0.4 0.6 0.8]); % Color map to distinguish feasible area
colorbar;

Output:
Optimization terminated.

Solution for alpha = 1 and beta = 1:

Value

_____

x1 1.5

x2 3

x3 2

Maximized z 6.5

Solutions for different values of alpha and beta:

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:
the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:
the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has stalled:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:
the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.
Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Optimization terminated.

Optimization terminated.

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

Exiting: One or more of the residuals, duality gap, or total relative error

has grown 100000 times greater than its minimum value so far:

the primal appears to be infeasible (and the dual unbounded).

(The dual residual < OptimalityTolerance=1.00e-08.)

alpha beta x1 x2 x3 Maximized_z

_____ ____ ___ __________ __ ___________

1 1 1.5 3 2 6.5

1 1.5 1 3 3 3

1 2 1 3.1042 4 1.2084
1 2.5 1 3.7569 5 0.51381

1 3 1 4.4053 6 -0.18943

1.5 1 1.5 3 2 6.5

1.5 1.5 1 3 3 3

1.5 2 1 3.1042 4 1.2084

1.5 2.5 1 3.7569 5 0.51381

1.5 3 1 4.4053 6 -0.18943

2 1 1.5 3 2 6.5

2 1.5 1 3 3 3

2 2 1 3.1042 4 1.2084

2 2.5 1 3.7569 5 0.51381

2 3 1 4.4053 6 -0.18943

2.5 1 1.5 3 2 6.5

2.5 1.5 1 3 3 3

2.5 2 1 3.1042 4 1.2084

2.5 2.5 1 3.7569 5 0.51381

2.5 3 1 0.00022026 6 -8.9996

3 1 1.5 3 2 6.5

3 1.5 1 3 3 3

3 2 1 3.1042 4 1.2084

3 2.5 1 3.7569 5 0.51381

3 3 1 4.4053 6 -0.18943

3.5 1 1.5 3 2 6.5

3.5 1.5 1 3 3 3

3.5 2 1 3.1042 4 1.2084

3.5 2.5 1 3.7569 5 0.51381

3.5 3 1 4.4053 6 -0.18943

4 1 1.5 3 2 6.5

4 1.5 1 3 3 3

4 2 1 3.1042 4 1.2084

4 2.5 1 3.7569 5 0.51381

4 3 1 4.4053 6 -0.18943
4.5 1 1.5 3 2 6.5

4.5 1.5 1 3 3 3

4.5 2 1 3.1042 4 1.2084

4.5 2.5 1 3.7569 5 0.51381

4.5 3 1 4.4053 6 -0.18943

5 1 1.5 3 2 6.5

5 1.5 1 3 3 3

5 2 1 3.1042 4 1.2084

5 2.5 1 3.7569 5 0.51381

5 3 1 4.4053 6 -0.18943

Optimization terminated.

Solution when x2 is unbounded:

Value

_____

x1 1

x2 4

x3 2

Maximized z 7
(Q2)

(a)
Solution:
Define @outfun in the same folder.
function stop = outfun(x, optimValues, state)
% This function records the function values at each iteration
global func_values
stop = false;
if strcmp(state, 'iter')
func_values(end+1) = optimValues.fval; % Store function value at each
iteration
end
end
Main function.
clc
f = @(x) 100*(x(2) + 2*x(1)^2)^2 + (1 - 3*x(1)^2)^2;
options = optimoptions('fminunc', 'Display', 'iter', 'OutputFcn', @outfun,
'Algorithm', 'quasi-newton');
% Initial starting point
x0 = [1, 1];
% Initialize a global variable
global func_values
func_values = [];
[x_min, f_min] = fminunc(f, x0, options);
fprintf('\nOptimal Solution Found:\n');
fprintf('x1 = %.4f, x2 = %.4f\n', x_min(1), x_min(2));
fprintf('Minimum Function Value = %.4f\n', f_min);
iter_table = table((1:length(func_values))', func_values', 'VariableNames',
{'Iteration', 'FunctionValue'});
disp(iter_table);
% Plot function values
figure;
plot(1:length(func_values), func_values, '-o');
xlabel('Iteration');
ylabel('Function Value');
title('Convergence of Function Value');
grid on;

% Experiment with other starting points


start_pts = {[1, 1], [0, 0], [-1, 1], [2, -1]};
fprintf('\nMinimization with Different Starting Points:\n');
for i = 1:length(start_pts)
x0 = start_pts{i};
func_values = []; % Reset function values storage
[x_min, f_min] = fminunc(f, x0, options);
fprintf('Starting Point (%.2f, %.2f): Final Function Value = %.4f\n',
x0(1), x0(2), f_min);
end

Output:
First-order

Iteration Func-count f(x) Step-size optimality

0 3 904 2.42e+03

1 6 57.6219 0.000412541 150

2 12 2.18956 10 21.9

3 15 0.991227 1 0.428

4 18 0.991013 1 0.466

5 21 0.990877 1 0.482
6 24 0.989997 1 0.57

7 27 0.987571 1 0.962

8 42 0.718174 2.85538 4.69

9 45 0.709451 1 3.21

10 51 0.493207 0.5 7.06

11 54 0.295255 1 4.72

12 57 0.113784 1 1.66

13 63 0.0894178 0.264031 6.51

14 66 0.00238401 1 2.26

15 72 0.000240075 0.435308 0.135

16 75 2.84782e-05 1 0.0306

17 78 1.45222e-08 1 0.00524

18 81 6.90164e-12 1 2.09e-05

Local minimum found.

Optimization completed because the size of the gradient is less than

the default value of the optimality tolerance.

<stopping criteria details>

Optimal Solution Found:

x1 = -0.5773, x2 = -0.6667

Minimum Function Value = 0.0000

Iteration FunctionValue

_________ _____________

1 904

2 57.622

3 2.1896

4 0.99123
5 0.99101

6 0.99088

7 0.99

8 0.98757

9 0.71817

10 0.70945

11 0.49321

12 0.29525

13 0.11378

14 0.089418

15 0.002384

16 0.00024008

17 2.8478e-05

18 1.4522e-08

19 6.9016e-12

Minimization with Different Starting Points:

First-order

Iteration Func-count f(x) Step-size optimality

0 3 904 2.42e+03

1 6 57.6219 0.000412541 150

2 12 2.18956 10 21.9

3 15 0.991227 1 0.428

4 18 0.991013 1 0.466

5 21 0.990877 1 0.482

6 24 0.989997 1 0.57

7 27 0.987571 1 0.962

8 42 0.718174 2.85538 4.69

9 45 0.709451 1 3.21

10 51 0.493207 0.5 7.06

11 54 0.295255 1 4.72
12 57 0.113784 1 1.66

13 63 0.0894178 0.264031 6.51

14 66 0.00238401 1 2.26

15 72 0.000240075 0.435308 0.135

16 75 2.84782e-05 1 0.0306

17 78 1.45222e-08 1 0.00524

18 81 6.90164e-12 1 2.09e-05

Local minimum found.

Optimization completed because the size of the gradient is less than

the default value of the optimality tolerance.

<stopping criteria details>

Starting Point (1.00, 1.00): Final Function Value = 0.0000

First-order

Iteration Func-count f(x) Step-size optimality

0 3 1 1.49e-06

Local minimum possible.

fminunc stopped because it cannot decrease the objective function

along the current search direction.

<stopping criteria details>

Starting Point (0.00, 0.00): Final Function Value = 1.0000

First-order

Iteration Func-count f(x) Step-size optimality

0 3 904 2.42e+03

1 6 57.6219 0.000412541 150


2 12 2.18956 10 21.9

3 15 0.991227 1 0.428

4 18 0.991013 1 0.466

5 21 0.990877 1 0.482

6 24 0.989997 1 0.57

7 27 0.987571 1 0.962

8 42 0.718174 2.85539 4.69

9 45 0.709451 1 3.21

10 51 0.493207 0.5 7.06

11 54 0.295255 1 4.72

12 57 0.113787 1 1.65

13 63 0.0894552 0.263237 6.51

14 66 0.00242066 1 2.27

15 72 0.0002629 0.438076 0.136

16 75 3.07445e-05 1 0.0305

17 78 1.74888e-08 1 0.00579

18 81 7.71074e-12 1 1.33e-05

Local minimum found.

Optimization completed because the size of the gradient is less than

the default value of the optimality tolerance.

<stopping criteria details>

Starting Point (-1.00, 1.00): Final Function Value = 0.0000

First-order

Iteration Func-count f(x) Step-size optimality

0 3 5021 1.15e+04

1 6 81.0671 8.72296e-05 726

2 9 38.4994 1 465

3 12 3.01943 1 103
4 15 0.712645 1 22.7

5 18 0.588568 1 2.22

6 21 0.5874 1 2.09

7 24 0.586752 1 2.26

8 27 0.582994 1 2.9

9 30 0.575228 1 4.47

10 33 0.55315 1 8.66

11 36 0.497459 1 14.2

12 39 0.336959 1 19

13 45 0.00798052 0.632911 3.58

14 51 0.00672049 0.397539 2.27

15 57 0.000431424 10 0.695

16 60 2.45153e-05 1 0.15

17 63 6.1409e-08 1 0.00874

Local minimum found.

Optimization completed because the size of the gradient is less than

the default value of the optimality tolerance.

<stopping criteria details>

Starting Point (2.00, -1.00): Final Function Value = 0.0000


(b)
Solution:
Define @outfun in the same folder.
function stop = outfun(x, optimValues, state)
% This function records the function values at each iteration
global func_values
stop = false;
if strcmp(state, 'iter')
func_values(end+1) = optimValues.fval; % Store function value at each
iteration
end
end

Main function.
clc
f = @(x) 6*x + 7*x^2 + 4*x^3 + x^4;
%the interval
x_min = 1;
x_max = 7;
%output record at each iteration
options = optimset('Display', 'iter', 'OutputFcn', @outfun);
% Initialize global variable
global func_values
func_values = [];
[x_opt, f_opt] = fminbnd(f, x_min, x_max, options);
fprintf('\nOptimal Solution Found:\n');
fprintf('x = %.4f\n', x_opt);
fprintf('Minimum Function Value = %.4f\n', f_opt);
iters = table((1:length(func_values))', func_values', 'VariableNames',
{'Iteration', 'FunctionValue'});
disp(iters);
%graph
figure;
plot(1:length(func_values), func_values, '-o');
xlabel('Iteration');
ylabel('Function Value');
title('Convergence of Function Value');
grid on;

Output:

Func-count x f(x) Procedure

1 3.2918 355.698 initial

2 4.7082 1092.27 golden

3 2.41641 145.904 golden

4 1.8746 74.5463 parabolic

5 1.54054 46.1126 golden

6 1.33407 33.1273 golden

7 1.20647 26.5707 golden

8 1.1276 23.0177 golden

9 1.07886 20.9985 golden

10 1.04874 19.815 golden

11 1.03012 19.1073 golden

12 1.01862 18.6789 golden

13 1.01151 18.4175 golden

14 1.00711 18.2573 golden

15 1.00439 18.1587 golden

16 1.00272 18.098 golden


17 1.00168 18.0605 golden

18 1.00104 18.0374 golden

19 1.00064 18.0231 golden

20 1.0004 18.0143 golden

21 1.00024 18.0088 golden

22 1.00015 18.0054 golden

23 1.00009 18.0034 golden

24 1.00006 18.0021 golden

Optimization terminated:

the current x satisfies the termination criteria using OPTIONS.TolX of


1.000000e-04

Optimal Solution Found:

x = 1.0001

Minimum Function Value = 18.0021

Iteration FunctionValue

_________ _____________

1 355.7

2 1092.3

3 145.9

4 74.546

5 46.113

6 33.127

7 26.571

8 23.018

9 20.999

10 19.815

11 19.107

12 18.679
13 18.418

14 18.257

15 18.159

16 18.098

17 18.061

18 18.037

19 18.023

20 18.014

21 18.009

22 18.005

23 18.003

24 18.002

[N.B. the function @outfun is only needed to be created once in the same
folder.]

You might also like