Plot system of linear equations

One of the first things I want to do in Matlab is enter a system of linear equations. I already found the example that helps me solve that system but I also want to plot those to see them visual.
The example I have is:
2*x - y == 7
x + y ==2
The code I use for solving this is the following:
syms x y
eq1 = 2*x-y==7
eq2 = x + y ==2
[A,B] = equationsToMatrix([eq1, eq2], [x, y])
X = linsolve(A,B)
This works.
But how do I plot eq1 and eq2?
If I'm not mistaken, the plot function requires actual values and not a function.
I know I can do something like this:
x = -10:0.1:10
y = 2*x- 7
plot(x,y)
But I was wondering if there's another way so that I can simply plot eq1 and eq2 without transforming it to something like y = ....

4 Kommentare

darova
darova am 14 Mai 2020
try ezplot
Yannick Ongena
Yannick Ongena am 15 Mai 2020
Thx, that seemed to do the trick. The documentation for ezplot says that it is not recommended and you should use flplot instead.
Why is that, the doc doesn't say why... Is it performance? Doesn't it always work? Just so I understand why...
Walter Roberson
Walter Roberson am 15 Mai 2020
fplot() had a number of new features added in R2015a, including the ability to plot symbolic expressions. ezplot() was left unenhanced.
Also, fplot() does a lot more work to try to find discontinuities, and to draw in the background.
ezplot() is based upon the much older technology of inline functions, which is not as efficient. inline() effectively creates scripts and eval()'s them, with all the problems that can cause.
Yannick Ongena
Yannick Ongena am 15 Mai 2020
ok that makes sense.
I noticed when I enter the code in Live Editor, the editor suggest to use fimplicit instead of ezplot as it is recommended when you use the == operator.
So I did that now and I simply can replace ezplot with fimplicit and that seems to work on the code I have.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sudhir Solkampally
Sudhir Solkampally am 20 Mai 2021

0 Stimmen

Hi Yannick,
As per my understanding you are trying to plot linear equations. Below is the code snippet for reference.
syms x y
eq1 = 2*x-y==7;
eq2 = x + y ==2
[A,B] = equationsToMatrix([eq1, eq2], [x, y])
X = linsolve(A,B);
fimplicit(eq1);
hold on
fimplicit(eq2);
hold off
For more details, you may refer to syms on symbolic equations in MATLAB
Also you can refer to fimplicit for plotting equations in MATLAB.
Thanks

2 Kommentare

kenzo
kenzo am 29 Jul. 2022
What if you want to plot x=4, y=6, t=3*x+2*y=18 on the same graph
syms x y
t = 3*x + 2*y == 18
t = 
ty = solve(t, y)
ty = 
fplot([6, ty], [-10 10])
xline(4)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020a

Gefragt:

am 14 Mai 2020

Kommentiert:

am 30 Jul. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by