
Trying to plot a scatterplot AND Histogram within a loop?
조회 수: 7 (최근 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!
댓글 수: 0
채택된 답변
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
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 Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!