Trying to plot a scatterplot AND Histogram within a loop?

8 ビュー (過去 30 日間)
Kyra Rubinstein
Kyra Rubinstein 2019 年 4 月 29 日
コメント済み: Adam Danz 2019 年 4 月 30 日
Hi, I was given a circuit with resistance of 1000 Ohms (tolerance of 5%) and a Capacitance of 1MicroFarad (tolerance of 10%) and was asked to create a loop of 1000 simulations (Similar to Monte Carlo simulations) to find the frequency (f=1/(2*pi*R*C)) and put those values into a scatter plot and histogram. I can't seem to get the histogram to output correctly, and I'm not even sure where to start on the scatter plot.
clear; clc;
figure; hold on
for i=1:1000
R=(rand)*100+950;
C=(rand)*0.0000002+0.0000009;
f=1/(2*pi*R*C);
histogram(f)
end
This is what I currently have. I appreciate any help, thank you so much!

採用された回答

Adam Danz
Adam Danz 2019 年 4 月 29 日
編集済み: Adam Danz 2019 年 4 月 29 日
This will get you started on the figures and you can adapt it to your needs. I haven't assessed whether the simulation is what you're supposed to be doing or not. Feel free to ask follow-up questions.
n = 1000;
f = zeros(1,n);
for i=1:n
R=(rand)*100+950;
C=(rand)*0.0000002+0.0000009;
f(i)=1/(2*pi*R*C);
end
figure;
subplot(2,1,1)
histogram(f)
xlabel('f value')
ylabel('frequency')
subplot(2,1,2) %or just create another figure
plot(1:n, f, 'o')
xlabel('iterations')
ylabel('f value')
  2 件のコメント
Kyra Rubinstein
Kyra Rubinstein 2019 年 4 月 30 日
Thank you so much! This is perfect.
I had struggled a lot with the graph in or out of the loop. It seemed like any time I put the graph outside of the loop it would only graph the last value of f. How does this code keep all the values of f and graphs all of them in the same plot?
Adam Danz
Adam Danz 2019 年 4 月 30 日
f(i)=1/(2*pi*R*C);
That line within the loop stores the results of each iteration within the variable f. You were only storing the most recent iteration's results and overwriting it on each loop.
Please take a moment to understand what each line is doing and if you have more questions let me know.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by