Using a function with multiple variable

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!

 Respuesta aceptada

Torsten
Torsten el 7 de En. de 2019
Editada: Torsten el 7 de En. de 2019

1 voto

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 comentarios

mabinj
mabinj el 7 de En. de 2019
Ah amazing!
Thank you so much!
So for future reference, was the loop the issue?
Torsten
Torsten el 7 de En. de 2019
No. Ge_interp and water_interp were not defined correctly.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 7 de En. de 2019

0 votos

dp_Ge(i,:) = f(lamda, other parameters )

4 comentarios

I have tried this and it still doesn't like it.
for i = 1:length(wavenumber):
dp_Ge(i,:) = f(lamda,Ge_interp,water_interp,theta_atr_Ge);
end
I have even tried :
for i = 1:length(wavenumber):
dp_Ge(i,:) = f(lamda,Ge_interp(i),water_interp(i),theta_atr_Ge);
end
As i thought it would iterate through each variable, however matlab throws me an error of:
Invalid expression. Check for missing or extra characters.
I checked for missing brackets (as that's my usual issue) and can't see anything obvious?
Thank yo for your fast response!
Torsten
Torsten el 7 de En. de 2019
Editada: Torsten el 7 de En. de 2019
The name of the function is "dp", not "f".
Furthermore, there is a typo in your function definition: You use "n_crytsal" instead of "n_crystal".
If it still doesn't work, please post the complete code you are using.
Walter Roberson
Walter Roberson el 7 de En. de 2019
no colon at the end of for. MATLAB does not use colon to separate statement parts like python does.
Thank you very much for your help!
I have removed the colon and corrected the spelling for crystal and changed the function name to dp.
I don't think I was very eloquent in what I am trying to do, so here is my full code:
clear all
%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);
water_interp = interp1(water_wavenumber,water_refractive,'linear');
Ge_files = csvread('Ge_IOR_Li-293K.csv');
Ge_wavenumber = Ge_files(:,1);
Ge_refractive = Ge_files(:,2);
Ge_interp = interp1(Ge_wavenumber,Ge_refractive,'linear');
%Angles of incidence
Ge_incidence = 36.2;
%Creating wavenumebr inputs
wavenumber = linspace(1000,3000,2000);
% Caclculating penetration depth
dp_Ge = [];
lamda = 10000./wavenumber;
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;
for i = 1:length(wavenumber)
dp_Ge(i,:) = dp(lamda,Ge_interp,water_interp,theta_atr_Ge);
end
figure(2)
plot(wavenumber,dp_Ge);
What I am trying to do is use the values I have in the excel files attached, (variables: 'water_interp' and 'Ge_interp' ) to create a linear interpolated matrix that can match the length of that of wavenumber.
Then, when lambda, water_interp and Ge_interp all have the same matrix length (can ignore theta_atr as it is an integer value that remains constant) dp_Ge should give me a new matrix of values of the same length.
Sorry again if I've not been clear, and thank you again for your help.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 7 de En. de 2019

Comentada:

el 7 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by