How to generate numbers that follows normal distribution but w.r.t. time in Matlab - matlab

As time moves along x-axis, the output value (may be freq or prob) be shown accordingly i.e. as time moves the values should increase initially till mean and then decrease. I have prespecified mean mu=10 and standard deviation sigma=2.

Do you mean you want to plot a Gaussian function? I am not sure if you also wanted some sort of animation. If not, you can modify the code below to turn off modification.
clear; close all; clc;
% Set the mean and standard deviation
mu = 10;
sigma = 2;
% Time t
t = linspace(0,20,101);
% The equation for a normal distribution
f = 1/(sqrt(2*pi)*sigma)*exp( -(t-mu).^2/(2*sigma^2));
hold on;
xlim([0,20]);
ylim([0,0.25]);
axis manual;
for i=1:length(t)
plot( t(1:i), f(1:i), 'b-');
pause(0.1);
end
hold off;

Related

How to choose a specific value from a loop to be plotted against a range of values?

I cannot think of the proper wording for this, but I am trying to have my code run a loop that will input a value of X into an initial condition variable. This variable is then inputted into the heat equation to be plotted. From the code I want to choose a value which is at X(i=51) and plot it as T(x,T1). As i said before I don't know the proper wording to search for a possible solution. Any advice would be great!
clear;
clc;
% initialize given variables
A= 0.25;
L= pi;
Nx=101; Nt=10^(-4);
dx=L/(Nx-1);
T1=zeros(1,Nx);
x=linspace(0, L, Nx); %x distance
%Initial condition
%T1 will be the "new" T value and To will be the old
T1= x.*(pi-x);
%For plotting, time starts at 0.
t=0;
for n=1:50
To=T1;
t=t+1;
for i=2:Nx-1
T1(i)=To(i)+Nt *A*((To(i+1)-2*To(i)+To(i-1))/(dx^2))+ sin(5*x(i));
end
%B.C given than # T(0,t) & T(L,t) = 0
T1(1)=0; T1(end)=0;
figure(1)
plot(x,T1); set(gca, 'ylim',[-110 110]);
ylabel('Temperature along the Rod');
xlabel('Location on the Rod of length Pi');
title(sprintf('Time = %f seconds', t));
pause(0.001);
end
The expected out put that I want to plot is plot(x(i=51),T1) which would show an image just like this. For this plot I ran my code and altered i to =50:51 to get the needed values for the heat equation. I am trying to have this be plotted in the code shown and not have to rewrite my code over and over to get different plots because I change values such as i or time ect...
You want to plot a consistent value of x, specifically x(51), for every n.
This can be done a couple of ways
First, as individual points with hold on:
for n = 1:50
% ... calculation ...
hold on
plot( x(51), T1(51), '.' );
hold off
end
You could update the plot, this will be quicker to compute and has the advantage of showing a line plot
x_51 = NaN( 50, 1 );
T1_51 = NaN( 50, 1 );
p = plot( [], [] );
for n = 1:50
% ... calculation ...
x_51(n) = x(51);
T1_51(n) = T1(51);
set( p, 'XData', x_51, 'YData', T1_51 );
end

Measuring peaks and the distance between them in my plot

I will keep the explanation, how my codes works, very short. I advise you to try this code yourself, so perhaps you understand it better that way. I have an audio-file and read it in my code. Now i switch from the time domain to the frequency domain by using the function FFT. But the only difference is, that i am performing an STFT on my audio signal. I do it every 30ms, until to the length of my signal. I am aware, that there are many different function in matlab, which also can perform this easily, but there are not giving me the results i need. Now, i am plotting many different frequency spectrums every 30ms. But i split up my signal in three frequency bands. They are called LOW, MEDIUM and HIGH. Basically, this means I have 3 different spectrums plotting every 30ms. The next step I do, is summing all the magnitudes from ONE frequency spectrum together, this means I have ONE VALUE per frequency spectrum, which are being squared.
Now, i have the power from every spectrum ! And all of these values are being plotted in my code. I am only plotting the power values, otherwise my code performance time would be extremely slow.
Btw, the code looks long, but there are two for loop. In the first, i read the low spectrum and when it is finished, the second starts with the medium and high spectrum. Basically they are the same. I am aware, i can probably do that with findpeaks or something similar. But how can i write/pull that of? Or what the necessary steps to do that. At the end, i included a file, Hopefully you can see that.
I want to measure the peaks and get the distance between them from the red plot.
EDIT:
Ok, i got the peaks, but not in the way i imagined. I want to show the peaks, which are above 5000-line. Sorry for not being clear at the beginning. See my plot, what i mean. I want to say my code, that only the peaks should be measured, which are above the 5000-line.
[pks, locs] = findpeaks(ValuesOfYc);
p=plot(x,ValuesOfYc,'r-' ,x(locs), pks,'ob');
This is, what I did above, in my first loop. How should i go on from there?
clear;
clc;
%% MATLAB
%% read file
%_________________________________________
[y,fs]=audioread('Undertale - Megalovania.wav');
% audioread = read wav -file
% y = contains the audio signal
% fs = 44100
% 'UnchainMyHeart' = name of the wav-file
%_________________________________________
%% PARAMETER FOR STFT
%_________________________________________
t_seg=0.03; % length of segment in ms
fftlen = 4096; %FFT-Points
% Defining size of frequency bands
f_low= 1:200; %lower frequencies
f_medium= 201:600; %medium frequencies
f_high= 601:1000; %higher frequencies
%__________________________________________
%% CODE
segl =floor(t_seg*fs);
windowshift=segl/2;
% defining the size of the window shift
window=hann(segl);
% apply hann function on segment length (30 ms)
window=window.';
% transpose vector
si=1;
% defining start index
ei=segl;
% defining end index
N=floor( length(y)/windowshift - 1);
% Calculates the number, how often the window has to shift
% until to length of the audio signal
f1=figure;
% Generating new window
f=0:1:fftlen-1;
f=f/fftlen*fs;
% defining frequency vector
Ya=zeros(1,fftlen);
ValuesOfYc = NaN(1,N);
ValuesOfYd = NaN(1,N);
ValuesOfYe = NaN(1,N);
x =(1:N)*windowshift/fs;
% defining x-axis
for m= 1:1:N
y_a = y(si:ei);
% a segment is taken out from audio signal length(30ms)
y_a= y_a.*window;
% multiplying segment with window (hanning)
Ya=fft(y_a, fftlen);
% Applying fft on segment
Yb=abs(Ya(1:end/2)).^2;
% Squaring the magnitudes from one-sided spectrum
drawnow; % Updating the graphical values
figure(f1);
% Showing the power values
%% frequency bands
y_low = Yb(f_low); % LOW frequency spectrum
Yc=sum(y_low);
% Summing all the power values from one frequency spectrum together
% so you get one power value from one spectrum
ValuesOfYc(m) = Yc;
%Output values are being saved here, which are generated from the for
%loop
% m = start variable from for loop
[pks, locs] = findpeaks(ValuesOfYc);
subplot(2,1,1)
p=plot(x,ValuesOfYc,'r-', x(locs(pks>=5000)), pks(pks>=5000),'ob');
p(1).LineWidth =0.5;
xlabel('time (Audio length)')
ylabel('Power')
grid on
si=si+windowshift;
% Updating start index
ei=ei+windowshift;
% Updating end index
end
for o= 1:1:N
y_a = y(si:ei);
% a segment is taken out from audio signal length(30ms)
y_a= y_a.*window;
% multiplying segment with window (hanning)
Ya=fft(y_a, fftlen);
% Applying fft on segment
Yb=abs(Ya(1:end/2)).^2;
% Squaring the magnitudes from one-sided spectrum
drawnow; % Updating the graphical values
figure(f1);
% Showing the power values
[![enter image description here][1]][1]
%% frequency bands
y_medium = Yb(f_medium); % MEDIUM frequency spectrum
y_high = Yb(f_high); % HIGH frequency spectrum
Yd=sum(y_medium);
Ye=sum(y_high);
% Summing all the power values from one frequency spectrum together
% so you get one power value from one spectrum
ValuesOfYd(o) = Yd;
ValuesOfYe(o) = Ye;
%Output values are being saved here, which are generated from the for
%loop
% m = start variable from for loop
subplot(2,1,2)
p=plot(x, ValuesOfYd,'g-', x, ValuesOfYe,'b-' );
p(1).LineWidth =0.5;
xlabel('time (Audio length)')
ylabel('Power')
grid on
si=si+windowshift;
% Updating start index
ei=ei+windowshift;
% Updating end index
end

Numerically integrating in Matlab. Why does increasing divisions cause less accurate output?

I am currently working on a problem in Matlab where I am given a distributed loaded beam and then numerically integrate (using the composite trapezoidal rule) to find the shear force and bending moment. From there I find the maximum moment value and the position at which this occurs. Then I differentiate twice to again find the shear force and distributed load.
I am using the analytically integrated functions in order to check and make sure that each of my numerical integrations and differentiations are calculated correctly. My issue is that when I use only 13 points to perform the calculations (x = 0:12;) my values are as close as I have gotten them to the results from the analytical values. (This number comes from the beam being 12 ft and the divisions are 1 ft each). When I increase the number of divisions in order to increase the accuracy of my numerical integration the values get further away from the analytical values. I would like to figure out what could have caused this and thus far have been unsuccessful.
My code:
clear; clc;
% n=100;
% x = linspace(0,12,n+1); %dx (100 divisions)
x=0:12; %dx (12 divisions)
w = 12.5.*x; %distributed load
%//Integrated Shear
V(1)=300;
for i = 2: length(w)
weight = [.5 ones(1,i-2) .5];
V(i)=300-sum(weight.*w(1:i));
end
figure(1); clf;
plot(x,V,'--');
legend('Numerical Shear');
xlabel('Position (ft)'); ylabel('Shear Force (lb)');
%//Integrated Moment
Mactual = 300.*x - ((25/12).*(x.^3)); %analytically integrated M
M(1)=0;
for i = 2: length(V)
weight = [.5 ones(1,i-2) .5];
M(i)=sum(weight.*V(1:i));
end
figure(2); clf;
plot(x,M,'--'); hold on;
plot(x,Mactual); hold off;
legend('Numerical Moment' , 'Analytical Moment');
xlabel('Position (ft)'); ylabel('Bending Moment (lbf)');
%//Max Moment and Position at Max Moment
[maxMValue, indexAtMaxM] = max(M);
xValueAtMaxMValue = x(indexAtMaxM(1));
dispM = [ 'Maximum bending moment: ' , num2str(maxMValue) , ' lbf'];
disp(dispM);
dispX = [ 'Position at maximum bending moment: ' , num2str(xValueAtMaxMValue) , ' ft' ];
disp(dispX)
%//Derived Shear
dM(1)=300;
for i = 1:length(x)-1
dM(i+1) = (M(i+1)-M(i));
end
figure(3); clf;
plot(x,dM,'--'); hold on;
Vactual = 300 - 6.25.*(x.^2); %analytically integrated shear
plot(x,Vactual); hold off;
legend('Numerical Shear' , 'Analytical Shear');
xlabel('Position (ft)'); ylabel('Shear Force (lb)');
%//Derived Load
dV(1)=0;
for i = 1:length(x)-1
dV(i+1) = -(V(i+1)-V(i));
end
figure(4); clf;
plot(x,dV,'--'); hold on;
plot (x,w); hold off;
legend('Numerical Load' , 'Analytical Load');
xlabel('Position (ft)'); ylabel('Distributed Load (lb/ft)');
In the code above the 2nd and 3rd lines are commented out to show the output with only 12 divisions. If you uncomment those two lines and comment out the 4th line the output shows values with 100 divisions.
Quick Note: The analytical values are plotted against the integrated moment, derived shear, and derived distributed load as a comparison. Consider the analytical values (Mactual and Vactual) as the correct output values.
Any input which could help lead me to a way to solve this issue would be greatly appreciated.
To get the correct integral value, you will need to multiply the function value sums with dx=12/n or identically dx=x(2)-x(1) if n is different from 12.
(11/28) You can also simplify the trapezoidal integration loops as
V(1)=300
M(1)=0
for i=2:n
V(i) = V(i-1) - 0.5*dx*(w(i-1)+w(i))
M(i) = M(i-1) + 0.5*dx*(V(i-1)+V(i))
end
thus avoiding reconstruction of the weights and computation of the sums in each step. This reduces this part of the algorithm from O(n^2) to O(n).

plotting line between points in a loop goes wrong

I am currently trying to simulate a random walk. The idea is to choose a random number between 0 and 2*pi and let the random walker go in that direction. Here is what I tried to do to simulate such a random walk:
before=[0 0]; %start in (0,0)
while 1
x=rand;
x=x*2*pi; %// choose random angle
increment=[cos(x),sin(x)] %// increments using the sine and cosine function
now=before+increment;
plot(before, now)
hold on
before=now;
pause(1);
end
I expect this program to plot lines and each new line starts at the ending point of the previous line, but this does not happen. I have no clue why it is not working.
You got the syntax for plot wrong, which is plot(X,Y). Change the call to
plot([before(1), now(1)], [before(2), now(2)])
and your program should work as expected.
Here is an improved version that does all the calculation vectorized and gives you two choices of output. The first one displays all at once and is very fast. The second one takes a lot of time depending on the amount of samples.
pts = [0,0]; % starting point
N = 10000; % sample count
x = rand(N,1) * 2*pi; % random angle
% calculate increments and points
inc = [cos(x),sin(x)];
pts = [pts;cumsum(inc,1)];
% plot result at once
figure;
plot(pts(:,1),pts(:,2));
% plot results in time steps
figure; hold on;
for i = 1:size(pts,1)
plot(pts(i:i+1,1),pts(i:i+1,2))
pause(1)
end
Here is an example of the output:

simulating a random walk in matlab

i have variable x that undergoes a random walk according to the following rules:
x(t+1)=x(t)-1; probability p=0.3
x(t+1)=x(t)-2; probability q=0.2
x(t+1)=x(t)+1; probability p=0.5
a) i have to create this variable initialized at zero and write a for loop for 100 steps and that runs 10000 times storing each final value in xfinal
b) i have to plot a probability distribution of xfinal (a histogram) choosing a bin size and normalization!!* i have to report the mean and variance of xfinal
c) i have to recreate the distribution by application of the central limit theorem and plot the probability distribution on the same plot!
help would be appreciated in telling me how to choose the bin size and normalize the histogram and how to attempt part c)
your help is much appreciated!!
p=0.3;
q=0.2;
s=0.5;
numberOfSteps = 100;
maxCount = 10000;
for count=1:maxCount
x=0;
for i = 1:numberOfSteps
random = rand(1, 1);
if random <=p
x=x-1;
elseif random<=(p+q)
x=x-2;
else
x=x+1;
end
end
xfinal(count) = x;
end
[f,x]=hist(xfinal,30);
figure(1)
bar(x,f/sum(f));
xlabel('xfinal')
ylabel('frequency')
mean = mean(xfinal)
variance = var(xfinal)
For the first question, check the help for hist on mathworks homepage
[nelements,centers] = hist(data,nbins);
You do not select the bin size, but the number of bins. nelements gives the elements per bin and center is all the bin centers. So to say, it would be the same to call
hist(data,nbins);
as
[nelements,centers] = hist(data,nbins);
plot(centers,nelements);
except that the representation is different (line or pile). To normalize, simply divide nelements with sum(nelements)
For c, here i.i.d. variables it actually is a difference if the variables are real or complex. However for real variables the central limit theorem in short tells you that for a large number of samples the distribution will limit the normal distribution. So if the samples are real, you simply asssumes a normal distribution, calculates the mean and variance and plots this as a normal distribution. If the variables are complex, then each of the variables will be normally distributed which means that you will have a rayleigh distribution instead.
Mathworks is deprecating hist that is being replaced with histogram.
more details in this link
You are not applying the PDF function as expected, the expression Y doesn't work
For instance Y does not have the right X-axis start stop points. And you are using x as input to Y while x already used as pivot inside the double for loop.
When I ran your code Y generates a single value, it is not a vector but just a scalar.
This
bar(x,f/sum(f));
bringing down all input values with sum(f) division? no need.
On attempting to overlap the ideal probability density function, often one has to do additional scaling, to have both real and ideal visually overlapped.
MATLAB can do the scaling for us, and no need to modify input data /sum(f).
With a dual plot using yyaxis
You also mixed variance and standard deviation.
Instead try something like this
y2=1 / sqrt(2*pi*var1)*exp(-(x2-m1).^2 / (2*var1))
ok, the following solves your question(s)
codehere
clear all;
close all;
clc
p=0.3; % thresholds
q=0.2;
s=0.5;
n_step=100;
max_cnt=10000;
n_bin=30; % histogram amount bins
xf=zeros(1,max_cnt);
for cnt=1:max_cnt % runs loop
x=0;
for i = 1:n_step % steps loop
t_rand1 = rand(1, 1);
if t_rand1 <=p
x=x-1;
elseif t_rand1<=(p+q)
x=x-2;
else
x=x+1;
end
end
xf(cnt) = x;
end
% [f,x]=hist(xf,n_bin);
hf1=figure(1)
ax1=gca
yyaxis left
hp1=histogram(xf,n_bin);
% bar(x,f/sum(f));
grid on
xlabel('xf')
ylabel('frequency')
m1 = mean(xf)
var1 = var(xf)
s1=var1^.5 % sigma
%applying central limit theorem %finding the mean
n_x2=1e3 % just enough points
min_x2=min(hp1.BinEdges)
max_x2=max(hp1.BinEdges)
% quite same as
min_x2=hp1.BinLimits(1)
max_x2=hp1.BinLimits(2)
x2=linspace(min_x2,max_x2,n_x2)
y2=1/sqrt(2*pi*var1)*exp(-(x2-m1).^2/(2*var1));
% hold(ax1,'on')
yyaxis right
plot(ax1,x2,y2,'r','LineWidth',2)
.
.
.
note I have not used these lines
% Xp=-1; Xq=-2; Xs=1; mu=Xp.*p+Xq.*q+Xs.*s;
% muN=n_step.*mu;
%
% sigma=(Xp).^2.*p+(Xq).^2.*q+(Xs).^2.s; % variance
% sigmaN=n_step.(sigma-(mu).^2);
People ususally call sigma to variance^.5
This supplied script is a good start point to now take it to wherever you need it to go.