MATLAB Answers

How do I plot a function with a variable that has multiple values?

8 views (last 30 days)
Lucas Barbosa
Lucas Barbosa on 10 Jul 2021
Edited: Soniya Jain on 10 Jul 2021
Hi! I'm new to matlab and i don't know how do i plot a graph that need to have X equal to 0, 0,5 and 1, while my Y is 0,5, and after that i need X to be 0,5 and Y to vary between 0, 0,5 and 1. How do i do that? I want to make 2 graphs, one with X varying and Y constant and the second graph having the second case, all in the same code.

Answers (2)

DGM
DGM on 10 Jul 2021
v = [0 0.5 1];
c = [1 1 1]*0.5;
subplot(2,1,1)
plot(v,c,'x-')
subplot(2,1,2)
plot(c,v,'x-')

Soniya Jain
Soniya Jain on 10 Jul 2021
Edited: Soniya Jain on 10 Jul 2021
Note that the below code will just plot a graph with the marker we mention and not any line on it.
X = [0, 0.5, 1];
Y = [0.5];
plot(X,Y,"-o")
So in 1st case you have to make Y vector same as length of X vector, which can be done without witing it manually, using the below code:
X = [0, 0.5, 1];
Y = zeros(1,length(X)) + 0.5;
plot(X,Y,"-o")
This will plot a straight line parallel to x-axis at y = 0.5.
Similarly, you can do for 2nd case.
To understand plot function in more detail, you can refer the MATLAB documentation of it. (https://in.mathworks.com/help/matlab/ref/plot.html)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by