Graphing a Histogram for Simulation
Mostrar comentarios más antiguos
I am trying to do a simulation and I am having an issue with the graph, I am trying to get a histogram graph with the most frequent occurrence happening in the middle with the other value frequences on the sides but my code is only graphing what seems to be the middle bar. I have included my code as well as an image of the type of graph I am trying to recreate.
close all
delta_cp = @(t) (-2.4433*t + 7.623) + 7.3125;
n_sims = 100000;
t_range = linspace(0, 3.1, 1000);
results = zeros(1, n_sims);
for i = 1:n_sims
t = i * t_range(end);
results(i) = delta_cp(t);
end
figure
hist(results(:,1), 50);
xlabel('Change in Center of Gravity (m)')
ylabel('Frequency')
title('Simulation Results')

3 comentarios
results(:,1) is one single value, namely results(1,1).
Besides this fact: I don't know what you are trying to Monte-Carlo-simulate with your code. You get deterministic values for "results", namely delta_cp(i*3.1) for 1 <=i<=n_sims.
John D'Errico
el 28 de Mzo. de 2023
There is nothing random in the code you wrote. You have labels like apogee, but there is nothing in your code that would seem to generate something of the sort. You create a function called delta_cp, but what does that mean? It is just a linear function of t, and we don't know what t means. Is it time? Distance? What?
Clearly you don't understand Monte Carlo simulations. But we don't know enough about what you are trying to do to really help you either.
I would suggest you need to explain clearly what you want to simulate, not only that you expect a picture like the one you show.
Leo
el 29 de Mzo. de 2023
Respuestas (1)
Image Analyst
el 28 de Mzo. de 2023
Editada: Image Analyst
el 28 de Mzo. de 2023
0 votos
How is it a distribution? You're not doing a Monte Carlo - you're not getting random numbers. Not sure what that function is. Is it a PDF or CDF or something else?
I'm attaching some Monte Carlo-related demos.
1 comentario
Leo
el 29 de Mzo. de 2023
Categorías
Más información sobre Histograms en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!