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

4 ビュー (過去 30 日間)
Lucas Barbosa
Lucas Barbosa 2021 年 7 月 10 日
編集済み: Soniya Jain 2021 年 7 月 10 日
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 件)

DGM
DGM 2021 年 7 月 10 日
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 2021 年 7 月 10 日
編集済み: Soniya Jain 2021 年 7 月 10 日
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)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by