How to implement density function

I would like to implemnet this "sampling algorithm for the following density function".
I tried with this code.Any help I will appricated.
% Example Monte Carlo Simulation in Matlab
%
% Generate n samples from a normal distribution
% r = ( randn(n,1) * sd ) + mu
% mu : mean
% sd : standard deviation
%
% Generate n samples from a uniform distribution
n = 100000; % The number of function evaluations
% --- Generate vectors of random inputs
% x1 ~ Normal distribution N(mean=100,sd=5)
% x2 ~ Uniform distribution U(a=5,b=15)
x = 5 + rand(0,1) * ( 15 - 5 );
% --- Run the simulation
% Note the use of element-wise multiplication
y = x.^2+(2/3x)+1/3;
% --- Create a histogram of the results (50 bins)
hist(y,50);
% --- Calculate summary statistics
y_mean = mean(y)
y_std = std(y)
y_median = median(y)

7 Comments

y = x.^2+(2/3x)+1/3;
should be
y = x.^2+(2/3)*x+1/3;
It's give one line in centre.would you please chech the rest of the code.
Thnks
x=5+rand(n,1)*(15-5); %In the comment line of the code randn(n,1)
Because in the following rand(0,1) gives empty matrix
>> r1=randn(0,1)
r1 =
Empty matrix: 0-by-1
Must change the following line as already commented by @Adam
y = x.^2+(2/3)*x+1/3;
You can check the code line by line, still not getting the correct output, let me know, I will try.
KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Apr 2019
Edited: KALYAN ACHARJYA on 25 Apr 2019
Code is not running or not getting rresults as expected?
x=5+rand(n,1)*(15-5); I know x value not bounding in 0 and 1?
code is running, result I didnot check
n=100000; % The number of function evaluations
x=5+rand(n,1)*(15-5);
y=x.^2+(2/3)*x+1/3;
hist(y,50);
y_mean=mean(y)
y_std=std(y)
y_median=median(y)
worked thanks

Sign in to comment.

Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 25 Apr 2019

Commented:

on 25 Apr 2019

Community Treasure Hunt

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

Start Hunting!