
Trying to plot a scatterplot AND Histogram within a loop?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kyra Rubinstein
il 29 Apr 2019
Commentato: Adam Danz
il 30 Apr 2019
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!
0 Commenti
Risposta accettata
Adam Danz
il 29 Apr 2019
Modificato: Adam Danz
il 29 Apr 2019
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 Commenti
Adam Danz
il 30 Apr 2019
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Histograms in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!