How to plot a random line in between 0 and a random variable?

1 回表示 (過去 30 日間)
Matthew
Matthew 2023 年 9 月 19 日
コメント済み: Walter Roberson 2023 年 9 月 19 日
I am currently learning how to use Matlab, this is what I have so far.
clc; clear;
V = randi([1 12]);
t = linspace(0,100,100);
y = V*sin(8*pi*t);
plot(t,y);
xlim([0 100]);
ylim([-14 14]);
V_sat = % I am trying to have this equal a random number between 0 and V
yLineP = V_sat;
yLineN = V_sat * -1;
yline(yLineP);
yline(yLineN);

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 19 日
I assume you want to you don't just want integers, otherwise you would have used randi for that.
V = randi([1 12])
V = 5
t = linspace(0,100,100);
y = V*sin(8*pi*t);
plot(t,y);
xlim([0 100]);
ylim([-14 14]);
%rand returns a random scalar drawn from the uniform distribution in the interval (0,1).
V_sat = V*rand % I am trying to have this equal a random number between 0 and V
V_sat = 1.5908
yLineP = V_sat;
yLineN = V_sat * -1;
yline(yLineP);
yline(yLineN);
  3 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 19 日
You are welcome!
Walter Roberson
Walter Roberson 2023 年 9 月 19 日
V_sat = V*rand % I am trying to have this equal a random number between 0 and V
Note by the way, that unless V happens to be exactly 0, that V*rand will never exactly equal 0, and will never exactly equal V .
In the default mode, rand() should be understood to be effectively randi([1 2^53-1])/2^53 where the range of random integers excludes 0 and excludes 2^53 exactly, so rand() can never return exactly 0 and can never return exactly 1.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by