Using a function with multiple variable
Show older comments
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!
Accepted Answer
More Answers (1)
Walter Roberson
on 7 Jan 2019
0 votes
dp_Ge(i,:) = f(lamda, other parameters )
4 Comments
mabinj
on 7 Jan 2019
Walter Roberson
on 7 Jan 2019
no colon at the end of for. MATLAB does not use colon to separate statement parts like python does.
mabinj
on 7 Jan 2019
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!