I need to find 10000 ramdom # in the range of (0.078,0.0078) for mass and (2, 0.1) for diameter. Then calculate the density of the 10k and do a histogram of them. Can anybody help me please?. This is what I got so far but is not working.

1 回表示 (過去 30 日間)
Shirly S
Shirly S 2016 年 8 月 29 日
編集済み: Walter Roberson 2017 年 1 月 14 日
clc, clear
%samples for the mass
a1 = 0.078;
b1 = 0.0078;
m = b1.*randn(10000,1)+a1;
%samples for the diameter
a2 = 2;
b2 = 0.1;
d = b2.*randn(10000,1)+a2;
%finding the density
density = (m/((4/3).*pi.*(d/2).*(d/2).*(d/2)));
histogram(density)

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 29 日
You mean in the range of [0.0078 0.078] not [0.078, 0.0078], because 0.0078 is less then 0.078.
a1 = 0.078;
b1 = 0.0078;
m = a1.*rand(10000,1)+b1
min(m)
max(m)
  1 件のコメント
John D'Errico
John D'Errico 2016 年 8 月 29 日
編集済み: John D'Errico 2016 年 8 月 29 日
Please stop adding answers every time you want to make a simple comment. There is a link for comments. Use it.
Moved answer by Shirly into a comment:
"the values for my density are zeros and the histogram is blank"

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 8 月 30 日
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create samples for the mass with specified mean and standard deviation.
meanMass = 0.078;
sdMass = 0.0078;
mass = sdMass .* randn(10000,1)+ meanMass;
% Create samples for the diameter with specified mean and standard deviation.
meanDiameter = 2;
sdDiameter = 0.1;
diameters = sdDiameter .* randn(10000,1) + meanDiameter;
% Find the volumes
volumes = (4/3) * pi * (diameters/2) .^ 3;
% Find the density
density = mass ./ volumes;
histogram(density);
grid on;
title('Density Distribution', 'FontSize', 15);
xlabel('Density', 'FontSize', 15);
ylabel('Count', 'FontSize', 15);
  2 件のコメント
Shirly S
Shirly S 2016 年 8 月 30 日
It worked. Thank you so much!! If is not too much to ask, what was my mistake??
Image Analyst
Image Analyst 2016 年 8 月 30 日
編集済み: Image Analyst 2016 年 8 月 30 日
Other than non-descriptive variable names, the main error was using m slash instead of m dot slash, which will do an element by element divide instead of a matrix inverse.
Also, please read this for your future posts.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConway's Game of Life についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by