Matlab with cvx:
% Decision Variables
cvx_begin
variables xsb xre xc xs
% Objective Function
maximize(0.04*xsb + 0.06*xre + 0.08*xc + 0.09*xs)
% Constraints
subject to
xsb + xre + xc + xs == 2.5e6
xc + xs <= 2*xsb
xs <= xc
xc <= 1.7*xre
xsb >= 0
xre >= 0
xc >= 0
xs >= 0
cvx_end
% Display the optimal solution
fprintf('Optimal Solution:\n');
fprintf('State Bonds: %.1f euros\n', xsb);
fprintf('Real Estate Loans: %.1f euros\n', xre);
fprintf('Car Loans: %.1f euros\n', xc);
fprintf('Scholarship Loans: %.1f euros\n', xs);
Python:
import cvxpy as cp
# Decision Variables
xsb = cp.Variable()
xre = cp.Variable()
xcℓ = cp.Variable()
xsℓ = cp.Variable()
# Objective Function
objective = cp.Maximize(0.04*xsb + 0.06*xre + 0.08*xcℓ + 0.09*xsℓ)
# Constraints
constraints = [
xsb + xre + xcℓ + xsℓ == 2.5e6,
xcℓ + xsℓ <= 2*xsb,
xsℓ <= xcℓ,
xcℓ <= 1.7*xre,
xsb >= 0,
xre >= 0,
xcℓ >= 0,
xsℓ >= 0
]
# Solve the problem
problem = cp.Problem(objective, constraints)
problem.solve()
# Display the optimal solution
print("Optimal Solution:")
print(f"State Bonds: {xsb.value:.1f} euros")
print(f"Real Estate Loans: {xre.value:.1f} euros")
print(f"Car Loans: {xcℓ.value:.1f} euros")
print(f"Scholarship Loans: {xsℓ.value:.1f} euros")