Reputation: 1
I am trying to code the affine scaling method for maximization LP problem and I used the following code.
function [X,y,k] = affsm(A,x,c)
xx(:,1)=x';
er=1e-5
k=1;
test=1;
r=2/3;
theta=1
while test>er/theta
D = diag(xx(:,k).^2);
AD = A*D;
dx = -(D-AD'*(AD*A')^(-1)*AD)*c';
theta =r*min([xx(:,k)./abs(dx);1]);
xx(:,k+1) = xx(:,k) + theta*dx ;
test=max([c*dx ,norm(dx)]);
k=k+1;
end
y=xx(:,k);
X=xx;
end
I am trying to solve the following problem
\begin{array}{c}{\max \quad Z=3 x+5 y} \\ {x+3 y \leq 60} \\ {3 x+4 y \leq 120} \\ {x \geq 10} \\ {x, y \geq 0}\end{array}
but it gives me wrong results what I am doing wrong in the above code ?.
Note: The optimal solution must be $(x,y)=(12,24)$ with $z=132$
Upvotes: 0
Views: 601
Reputation: 10790
I don't know what your function is supposed to do since there is no explaination, but to resolve this kind of linear equation with constraints you can use linprog
% define the constraints
% Every constraints have this format: x1*x(1) + x2*x(2) ≤ n
% Where x(1) and x(2) are your variable and x1,x2 and n are integers.
A = [1 3
3 4
-1 0
0 -1
-1 0];
b = [60 120 -10 0 0];
% define the objective function
f = [-3 -5];
% solve
x = linprog(f,A,b)
Result:
x =
24
12
Noticed that I've reversed the sign of the objective function in order to maximize the solution (by default linprog will minimize the solution)
Upvotes: 1