Fron Python to Matlab

1 回表示 (過去 30 日間)
Pouyan Msgn
Pouyan Msgn 2018 年 11 月 14 日
コメント済み: Nour Sd 2018 年 12 月 8 日
I got a code in Python that I will write it again in Matlab and get the same plot:
The code is :
npts = 1000 # number of points in uncorrelated data set
Emax = 10. # energy goes from zero to Emax
Ec = 0.1 # correlation energy
kT = 0.25
# create discrete values of Energy
E = np.linspace(0, Emax, npts)
E_extra = np.linspace(0, Emax, 2*npts-1) #sometimes we need this size to convolve
#PLOT 1: Tbar correlated
Tbar = np.random.normal(loc=0., scale=1.0, size=2*npts-1 )
y = np.exp(- ((E-Emax/2.)/Ec)**2)
#convolution between Tbar and correlation energy
Tbar_c = np.convolve(y, Tbar, mode='valid') #valid = no edge effects
# normalize the data (maximum=1, minimum=0)
Tbar_c = Tbar_c - Tbar_c.min()
Tbar_c = Tbar_c/Tbar_c.max()
# plot the correlated data set
ax1.plot(E, Tbar_c)
The plot is (I need the first plot (the one at the top T(E)))
Here is what I have done yet:
clc
clear all
Emax=10;
Ec=0.01;
E=0:0.01:10;
g=exp(-0.5.*((E-Emax).^2)./Ec);
y=rand(1,length(E));
%y=rand(size(E))
T1=conv(y,g,'same');
T=T1./norm(T1);
subplot(3,1,1);
plot(E,T)
The problem is for me here:
Draw random samples from a normal (Gaussian) distribution. in Python we can do it by :
Tbar = np.random.normal(loc=0., scale=1.0, size=2*npts-1 )
  4 件のコメント
Adam Danz
Adam Danz 2018 年 12 月 8 日
編集済み: Adam Danz 2018 年 12 月 8 日
Check out the convolution weight function convwf(). Carefully read the documentation to make sure this is doing what your python function does and that inputs are the same and in the same order (or not).
If this isn't want you're looking for, I suggest you open a new question. I'd rather not this thread turn into a Python --> Matlab conversion forum.
Nour Sd
Nour Sd 2018 年 12 月 8 日
Okay thank you very much :)

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

採用された回答

Adam Danz
Adam Danz 2018 年 11 月 14 日
編集済み: Adam Danz 2018 年 11 月 14 日
I can't run your code right now in python so I can't compare the results but most of these lines should be the correct conversion.
See help normrnd to pull numbers from a given gaussian distribution.
npts = 1000; % number of points in uncorrelated data set
Emax = 10; % energy goes from zero to Emax
Ec = 0.1; % correlation energy
kT = 0.25;
% create discrete values of Energy
E = linspace(0, Emax, npts);
E_extra = linspace(0, Emax, 2*npts-1); %sometimes we need this size to convolve
%PLOT 1: Tbar correlated
Tbar = normrnd(0, 1, 1, 2*npts-1)
y = exp(- ((E-Emax/2.)/Ec).*2);
%convolution between Tbar and correlation energy
Tbar_c = conv(y, Tbar, 'same'); %valid = no edge effects
% normalize the data (maximum=1, minimum=0)
Tbar_c = Tbar_c - min(Tbar_c);
Tbar_c = Tbar_c/max(Tbar_c);
% plot the correlated data set
plot(E, Tbar_c)
  4 件のコメント
Pouyan Msgn
Pouyan Msgn 2018 年 11 月 14 日
Thank you very much but one thing: y = np.exp(- ((E-Emax/2.)/Ec)**2) means exp(-((E-Emax/2)./Ec).^2)
Adam Danz
Adam Danz 2018 年 11 月 14 日
good catch!

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

その他の回答 (1 件)

dpb
dpb 2018 年 11 月 14 日
See
doc randn
When looking for something you don't know function name but have a clue about what is,
lookfor keyword
is useful; in this case either
lookfor random
lookfor normal
would lead you there...also just the venerable old
help
can show you what areas are in base Matlab plus the installed toolboxes available...

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by