SLP optimization yields "Error Z must be at least a 2x2 matrix"

1 view (last 30 days)
I'm trying to estimate the coefficient of lift and drag and then find the optimal geometry of the body using the SLP method, when using meshgrid I'm getting an error saying Z must be a 2x2 matrix, any help would be appreciated.
clc;
clear all;
close all;
global FUNC_COUNT
FUNC_COUNT.count=0;
save('FUNC_COUNT.mat','FUNC_COUNT')
%% Statement of Assumed Data Conditions
L = 1600; % Lift (Kg) with engine (Based on tomohawk missile)
Fd = 0.05; % Drag Force (Newtons)
rho = 0.4135; % Air Density (kg/m^3)
U = 1372; % Velocity (Mach 4 = 1372 m/s)
%% Visualisation of the Function and Constraint
hold all;grid minor;axis equal;
S = linspace(0, 4, 100);
A = linspace(0, 4, 100);
[x, y] = meshgrid(S, A);
F = (2*L)\(rho*U^2*S);
H = x.^2 + y.^2;
G = (2*Fd)\(rho*U^2*A);
cv1 = [100 2];
a = contour(x, y, H, cv1, 'k');
clabel(a);
cv2 = [1 100];
b = contour(x, y, G, cv2, 'r');
clabel(b);
bb = contour(x, y, F, 'green');
clabel(bb);
xlabel('X-Axis');
ylabel('Y-Axis');
title('Function Contour Plot')
ax = gca;
set(ax, 'FontName', 'Times', 'Fontsize', 14, 'FontWeight', 'bold');
set(gcf, 'color', [1 1 1])
text(-0.7, 1.55, 'Equality Constraint');
text(-0.7, 0.95, 'Inequality Constraint');
text(3, 2, 'Starting Point');
%% Initial Conditions
x0 = 3;
y0 = 2;
Initialvalues = [x0; y0]; %Initial Point
Iteration = 1;
Max_Iterations = 5;
%% Linearization Process
while Iteration < Max_Iterations
[F, h, g] = FunctionSLP(x0, y0);
[fx, fy, hx, hy, gx, gy]= finitediff(x0, y0);
tempval = [x0 y0];
%% Sequential Part
x = optimvar('x');
y = optimvar('y');
prob = optimproblem('Objective', fx.*x + fy.*y + F, 'ObjectiveSense', 'min');
prob.Constraints.c1 = hx*x + hy*y + h == 0;
prob.Constraints.c2 = gx*x + gy*y + g <= 0;
problem = prob2struct(prob);
[sol] = linprog(problem);
aa = sol(1,1);
bb = sol(2,1);
x0 = aa + x0;
y0 = bb + y0;
points = [x0; y0];
xpoints = [tempval(1) points(1)];
ypoints = [tempval(2) points(2)];
plot(xpoints, ypoints, '-bd');
hold on;
fprintf(' \n')
fprintf('Iteration Number :');
fprintf(' %d\n\n', Iteration)
fprintf(' Optimum Values :');
fprintf(' %f, %f\n', x0,y0)
fprintf(' Optimum cost function value :');
fprintf(' %f\n', FunctionSLP(x0,y0))
Iteration = Iteration + 1;
end
fprintf(' \n')
fprintf(' Sequential Linear Programming Method \n')
fprintf(' \n')
fprintf(' Initial Values :');
fprintf(' %f, %f\n',Initialvalues(1),Initialvalues(2))
fprintf(' Optimum Values :');
fprintf(' %f, %f\n', x0,y0)
fprintf(' Optimum cost function value :');
fprintf(' %f\n', FunctionSLP(x0,y0))
fprintf(' Number of Iterations :');
fprintf(' %d\n', Iteration)
fprintf(' Total Function Evaluations : %d\n', FUNC_COUNT.count)
The output looks like this:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
Error in SLP_Mainprog (line 24)
b = contour(x, y, G, cv2, 'r');

Accepted Answer

Yongjian Feng
Yongjian Feng on 21 Nov 2021
Your X is a 100x100 matrix, Y is also a 100x100 matrix. But your G is a 1x100 vector. The error is about your G (referred as Z).
Something must be wrong, the sizes don't match.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!