University of Dhaka, MTH-450 Assignment 4 Solution
University of Dhaka, MTH-450 Assignment 4 Solution
(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=
divergence=
-2
divergence=
0
fluid in (iii) is solinoidal
divergence=
curl=
curl=
curl=
-2
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])
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);
Output:
Optimization terminated.
Value
_____
x1 1.5
x2 3
x3 2
Maximized z 6.5
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).
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
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).
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has stalled:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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).
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
Exiting: One or more of the residuals, duality gap, or total relative error
has grown 100000 times greater than its minimum value so far:
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.5 1 3 3 3
2 1 1.5 3 2 6.5
2 1.5 1 3 3 3
2 2 1 3.1042 4 1.2084
2 3 1 4.4053 6 -0.18943
2.5 1.5 1 3 3 3
3 1 1.5 3 2 6.5
3 1.5 1 3 3 3
3 2 1 3.1042 4 1.2084
3 3 1 4.4053 6 -0.18943
3.5 1.5 1 3 3 3
4 1 1.5 3 2 6.5
4 1.5 1 3 3 3
4 2 1 3.1042 4 1.2084
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
5 1 1.5 3 2 6.5
5 1.5 1 3 3 3
5 2 1 3.1042 4 1.2084
5 3 1 4.4053 6 -0.18943
Optimization terminated.
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;
Output:
First-order
0 3 904 2.42e+03
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
9 45 0.709451 1 3.21
11 54 0.295255 1 4.72
12 57 0.113784 1 1.66
14 66 0.00238401 1 2.26
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
x1 = -0.5773, x2 = -0.6667
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
First-order
0 3 904 2.42e+03
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
9 45 0.709451 1 3.21
11 54 0.295255 1 4.72
12 57 0.113784 1 1.66
14 66 0.00238401 1 2.26
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
First-order
0 3 1 1.49e-06
First-order
0 3 904 2.42e+03
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
9 45 0.709451 1 3.21
11 54 0.295255 1 4.72
12 57 0.113787 1 1.65
14 66 0.00242066 1 2.27
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
First-order
0 3 5021 1.15e+04
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
15 57 0.000431424 10 0.695
16 60 2.45153e-05 1 0.15
17 63 6.1409e-08 1 0.00874
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:
Optimization terminated:
x = 1.0001
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.]