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

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.

回答(2 个)

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-')
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)

类别

帮助中心File Exchange 中查找有关 Discrete Data Plots 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by