필터 지우기
필터 지우기

Using a function with multiple variable

조회 수: 5 (최근 30일)
mabinj
mabinj 2019년 1월 7일
댓글: Torsten 2019년 1월 7일
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!

채택된 답변

Torsten
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
mabinj
mabinj 2019년 1월 7일
Ah amazing!
Thank you so much!
So for future reference, was the loop the issue?
Torsten
Torsten 2019년 1월 7일
No. Ge_interp and water_interp were not defined correctly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 1월 7일
dp_Ge(i,:) = f(lamda, other parameters )
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 1월 7일
no colon at the end of for. MATLAB does not use colon to separate statement parts like python does.
mabinj
mabinj 2019년 1월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by