
Trying to plot a scatterplot AND Histogram within a loop?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kyra Rubinstein
el 29 de Abr. de 2019
Comentada: Adam Danz
el 30 de Abr. de 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 comentarios
Respuesta aceptada
Adam Danz
el 29 de Abr. de 2019
Editada: Adam Danz
el 29 de Abr. de 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 comentarios
Adam Danz
el 30 de Abr. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Histograms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!