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

4 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Matías Nomboly
Matías Nomboly el 16 de Feb. de 2018
Editada: Matías Nomboly el 16 de Feb. de 2018
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
  3 comentarios
Matías Nomboly
Matías Nomboly el 16 de Feb. de 2018
No, i don't think that it would be optimal to solve this equations by hand because they are too easy for MATLAB and your PC. It would be if what you are trying to solve is way too hard or has sqrts and logs and exponentials and so on, all combined.
Regarding your second question, no, i do not know a standard matlab function that draws your plot and calculates intersections. However, there is a very practical equation plotter to help you visualize.
1) Define a symbolic variable
syms x
2) Define a function
f1=3*exp(x); (exp is e^x, euler's number)
3)Plot automatically
ezplot(f1);
4) Alternatively
ezplot(f1,[XLimMinimum, XLimMaximum]);

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by