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

3 次查看(过去 30 天)
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!

采纳的回答

Matías Nomboly
Matías Nomboly 2018-2-16
编辑:Matías Nomboly 2018-2-16
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 个评论
Matías Nomboly
Matías Nomboly 2018-2-16
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]);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by