MATLAB: Code to get matlab to solve for x and plug that number to the 2nd equation to find intersection point

linear programming

Hello,
What a beautiful piece of software!.. thank you for making it! I'm trying to get some linear programming work done. and i'm having problems with finding the intersection of two equations on a plot. I have these two constraints
% X + 2Y <= 16 #Constraint 1
% 5X+ 3Y <= 45 #Constraint 2
% X, Y >= 0
My try was:
clc, clear, close all
% X + 2*Y <= 16
x = [0 16];
y = [8 0];
% 5*X2 + 3*Y2 <= 45
x2=[0 9];
y2=[15 0];
%%PLOTTING
figure; plot(x,y, 'r');
hold on
plot(x2,y2, 'b');
hold off;
title('Représentation Graphique du PL'), xlabel('x - Axe des abscisses'), ylabel('Y - Axe des Ordonnées');
legend('X+2Y<=16', '5X+3Y<=45');
TRIED THIS CODE TO SHOW THE INTERSECTION POINT, BUT DIDN'T WORK…
%method 2
%---------
% sub = find(x == y)
%

% x_intersect = y(sub);
% intersect_pt = y(sub);
%
% figure(1);
% p = plot(x,y,...
% x2,y2,...
% x_intersect, intersect_pt, 'ko');
Thanks!

Best Answer

In order to do this, I can show you a way to solve it, but its different from your approach I hope that what you are trying to find is the intersection between two functions, right?
So lets define a symbolic variable: syms x y 2) lets define both of the functions that you need as if they were equal to zero f1=x + 2*y - 16; f2=5*x + 3*y - 45;
3) we shall now solve for x or y in the second equation we'll choose to solve for y, just because I chose to yaux=solve(f2,'y');
4) we'll replace yaux in the f1 function f1x=subs(f1,'y',yaux);
5) Now f1x is only a function of x, so xintersection=solve(f1x); yintersection=subs(f1x,'x',xintersection);
so point (x,y) of intersection is (xintersection,yintersection) if you need them as a number you should do xintersection=double(xintersection); same for y intersection
code:
syms x y
f1=x + 2*y - 16;
f2=5*x + 3*y - 45;
yaux=solve(f2,'y');
f1x=subs(f1,'y',yaux);
xintersection=solve(f1x);
yintersection=subs(f1x,'x',xintersection);
xintersection=double(xintersection); %if needed

yintersection=double(yintersection); %if needed