Using a function with multiple variable
5 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to create a function (from an equation) with four variables: lambda,n_crystal,n_sample and bounce_angle. A colleage of mine has been able to create code in python which defines the function and appends it and I am trying to translate it into matlab and failing.
The python code reads as follows:
def dp(lam,n_crys,n_sample,phi):
bottom=2*np.pi*n_crys*np.sqrt((np.sin(phi*np.pi/180))**2-(n_sample/n_crys)**2)
return lam/bottom
def microns_from_wavenumbers(wavenumber):
return 10000.0/wavenumber
The code above is the equation I want to define, with the only difference being my colleage replacing variable name 'bounce_angle' with 'phi'.
Ge_IOR_n_interp=interp1d(Ge_IOR_X,Ge_IOR_n,kind='linear')
wavenumbers=np.arange(1000.0,3000.1,1)
dp_Ge=[]
for wavenumber in wavenumbers:
lam=microns_from_wavenumbers(wavenumber)
beta_Ge=(180.0/np.pi)*(1/Ge_IOR_n_interp(lam))*(np.arcsin(np.pi*incidence_Ge/180.0))
bounce_angle_Ge=(90.0-incidence_Ge+beta_Ge)
dp_Ge.append(dp(lam,Ge_IOR_n_interp(lam), water_IOR_n_interp(lam), bounce_angle_Ge))
And this code shows how I want to append the function (ignore the variables). This essentially takes the interpolated files: Ge_IOR_n_interp and water_IOR_n_interp and changes the length of them to match the length of wavenumbers, then inputs them and the other calculated variables into the function to give dp_Ge a matrix of values.
This is what I have come up with in matlab:
dp = @(lam,n_crystal,n_sample,bounce_angle) lam/(2*pi*n_crytsal*sqrt((sind(bounce_angle)^2) - (n_sample/n_crystal)^2));
dp_Ge = [];
for i = 1:length(wavenumber);
lamda = 10000./wavenumber;
theta_atr_Ge = 90 - a_Ge;
dp_Ge = append(f(lamda,Ge_interp,water_interp,theta_atr_Ge));
end
This doesn't work, but after reading about anonymous functions I'm still lost as to where I've gone wrong.
Any help is thoroughly appreciated!
0 个评论
采纳的回答
Torsten
2019-1-7
编辑:Torsten
2019-1-7
function main
%Manual inputs
a_Ge = 9.64;
a_ZnS = 26.4;
%Reading files
water_files = csvread('Water_IOR_Hale.csv');
water_wavenumber = water_files(:,1);
water_refractive = water_files(:,2);
Ge_files = csvread('Ge_IOR_Li-293K.csv');
Ge_wavenumber = Ge_files(:,1);
Ge_refractive = Ge_files(:,2);
%Angles of incidence
Ge_incidence = 36.2;
%Creating wavenumebr inputs
wavenumber = linspace(1000,3000,2000);
% Caclculating penetration depth
lambda = 10000./wavenumber;
Ge_interp = interp1(Ge_wavenumber,Ge_refractive,lambda,'linear');
water_interp = interp1(water_wavenumber,water_refractive,lambda,'linear');
dp = @(lam,n_crystal,n_sample,bounce_angle) lam./(2.*pi.*n_crystal.*sqrt((sind(bounce_angle).^2) - (n_sample./n_crystal).^2));
theta_atr_Ge = 90 - a_Ge;
dp_Ge = dp(lambda,Ge_interp,water_interp,theta_atr_Ge);
plot(wavenumber,dp_Ge);
end
2 个评论
更多回答(1 个)
Walter Roberson
2019-1-7
dp_Ge(i,:) = f(lamda, other parameters )
4 个评论
Walter Roberson
2019-1-7
no colon at the end of for. MATLAB does not use colon to separate statement parts like python does.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!