Need help with a problem

There is a program I have one part I have to modify, could anyone help with this?
function y = run (lambda, n_mea, n_smpls_per_mea)
global xacc hist bin_range bin_size n_bins bin_size_inv midpt bin
% define parameters and procedures for histogram:
bin_range = 6.0 * lambda %NOTE: prefactor can be changed
bin_size = 0.1 * lambda %NOTE: prefactor can be changed
n_bins = floor(bin_range/bin_size)+1
bin_size_inv = 1.0/bin_size
% bin is a function: bin(x) provides the bin number for x:
bin =@(x)(min(floor(x*bin_size_inv)+1,n_bins))
% hist accumulates the number of entries in each bin:
hist = zeros(1,n_bins)
xacc = [n_mea]
for m=1:n_mea
xacc(m)=0
for n=1:n_smpls_per_mea
% unifrnd(0,1) gives a random uniform on (0,1)
ranxi = unifrnd(0,1)
% ranexp gives exponential distribution
ranexp = ranxi
% *^^^^^^^^^ <-- MODIFY -- fill in correct form**
xacc(m) = xacc(m) + ranexp
bi = bin(ranexp)
hist(bi) = hist(bi) +1
end
xacc(m) = xacc(m)/n_smpls_per_mea
end
midpt = 0.5*bin_size:bin_size:(n_bins-0.5)*bin_size
end

8 件のコメント

OCDER
OCDER 2017 年 10 月 2 日
編集済み: OCDER 2017 年 10 月 2 日
Before we can attempt to help:
What do you need to modify?
What is the correct form?
" *% *^^^^^^^^^ <-- MODIFY -- fill in correct form**
What is the error message, if any?
Have you heard of camelcase notation of variables? It'll make codes easier to read because tracking underscores in variable names is just confusing. Read this https://en.wikiversity.org/wiki/CamelCase.
midpt = 0.5*bin_size:bin_size:(n_bins-0.5)*bin_size
midpt = 0.5*binSize:binSize:(nBins-0.5)*binSize
Also:
  • do not use hist as a variable name because hist is a matlab function.
  • instead of unifrnd(0, 1), you could use rand() for a uniform number between 0 to 1.
Jan
Jan 2017 年 10 月 2 日
編集済み: Jan 2017 年 10 月 2 日
You point out, where the changes should be applied, but do not explain with any detail, what you want to change. What is the "correct form"?
Do not call your function "run", because this is an important Matlab command.
Gaëtan Poirier
Gaëtan Poirier 2017 年 10 月 2 日
For the modification: the expression for the exponential random number generator ranexp in run
OCDER
OCDER 2017 年 10 月 2 日
Have you looked up this on the google search?
"matlab exponential distributed random number"
Gaëtan Poirier
Gaëtan Poirier 2017 年 10 月 2 日
Yes, I added histogram('Normalization','probability'); but seem to have the error "not enough input arguments."
Jan
Jan 2017 年 10 月 2 日
Whenever you mention an error in the forum, post a complete copy of the message, not just a part of it.
Gaëtan Poirier
Gaëtan Poirier 2017 年 10 月 2 日
Error in mkhist (line 13) exact_pdf = exp(-x/lambda)/lambda;
Walter Roberson
Walter Roberson 2017 年 10 月 2 日
mkhist is not called from the code that you posted.

回答 (0 件)

この質問は閉じられています。

質問済み:

2017 年 10 月 2 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by