
Trying to plot a scatterplot AND Histogram within a loop?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kyra Rubinstein
le 29 Avr 2019
Commenté : Adam Danz
le 30 Avr 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 commentaires
Réponse acceptée
Adam Danz
le 29 Avr 2019
Modifié(e) : Adam Danz
le 29 Avr 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 commentaires
Adam Danz
le 30 Avr 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Histograms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!