- Apply function to each element of array - MATLAB arrayfun (mathworks.com)
- Apply function to each cell in cell array - MATLAB cellfun (mathworks.com)
- Plot graph nodes and edges - MATLAB plot (mathworks.com)
- Anonymous Functions - MATLAB & Simulink (mathworks.com)
How do I use the matrix elements, all of which are functions of lambda, to obtain R(lambda)? I get an error when I try to use the matrix elements to get R(lambda).)
조회 수: 5 (최근 30일)
이전 댓글 표시
If we let M be a 2x2 matrix with matrix elements M1, M2, M3, and M4, all of which are dependent on lambda, I cannot seem to use the matrix elements to plot R(lambda) vs lambda. M1, M2, M3, and M4 do get displayed in the command window, but I get an error when I try to use them to obtain R(lambda).
Soure code:
lambda_0 = 0.980; % Cavity length of DBR mirror in micron
d_G = 0.06975562993; % Thickness of GaAs layer in micron
d_A = 0.08148984639; % Thickness of AlAs layer in micron
n_G0 = 3.512261308; % GaAs index of refraction at 980 nm
n_A0 = 3.006509533; % AlAs index of refraction at 980 nm
lambda = 0.9:0.01:1.1; % Wavelength range in micron
n_G = @(lambda) sqrt(10.906+(0.97501./(lambda.^2-(0.52886)^2))-... % Index of refraction of GaAs as a function of wavelength
0.002467.*lambda.^2);
n_A = @(lambda) sqrt(7.986+(0.97501./(lambda.^2-(0.19886)^2))-... % Index of refraction of AlAs as a function of wavelength
0.0059457.*lambda.^2);
M_G = @(lambda) [cos((2*pi*d_G.*n_G(lambda))./lambda),... % Characteristic 2x2 Matrix of GaAs
(-i./n_G(lambda)).*sin((2*pi*d_G.*n_G(lambda))./lambda);...
-(i.*n_G(lambda)).*sin((2*pi*d_G.*n_G(lambda))./lambda),...
cos((2*pi*d_G.*n_G(lambda))./lambda)];
M_A = @(lambda) [cos((2*pi*d_A.*n_A(lambda))./lambda),... % Characteristic 2x2 Matrix of AlAs
(-i./n_A(lambda)).*sin((2*pi*d_A.*n_A(lambda))./lambda);...
-(i.*n_A(lambda)).*sin((2*pi*d_A.*n_A(lambda))./lambda),...
cos((2*pi*d_A.*n_A(lambda))./lambda)];
syms lambda
M = @(lambda) (M_G(lambda)*M_A(lambda))^32; % Characteristic matrix of DBR mirror consisting of
% 32 pairs of AlAs/GaAs layers
tmp=num2cell(M(lambda));
[M1,M2,M3,M4]=deal(tmp{:})
syms lambda
M1 = @(lambda) M1;
M2 = @(lambda) M2;
M3 = @(lambda) M3;
M4 = @(lambda) M4;
R = @(lambda) (M2(lambda)./M1(lambda)).^2; % Reflection coefficient of DBR mirror as a function of
% wavelength
figure(1)
plot(lambda,R(lambda),'k'),xlabel('Wavelength (um)'),ylabel('Reflection Coefficient')
댓글 수: 0
답변 (1개)
Karan Singh
2023년 10월 3일
Hi Craig,
From what I understand, the goal is plot the parameters as “M1”, “M2”, “M3”, and “M4”, as they do get displayed in the command window, but you get an error when I try to use them to obtain "R(lambda)”.
The error you're encountering occurs because the “plot” function expects numeric data for the x and y axes. In your case, “lambda” is defined as a symbolic variable, and “R(lambda)” is a symbolic expression. To resolve this issue, you need to convert the symbolic expressions to numeric values before plotting.
Here's an updated version of your code that should work correctly:
lambda_0 = 0.980;
d_G = 0.06975562993;
d_A = 0.08148984639;
n_G0 = 3.512261308;
n_A0 = 3.006509533;
lambda = 0.9:0.01:1.1;
n_G = @(lambda) sqrt(10.906+(0.97501./(lambda.^2-(0.52886)^2))-0.002467.*lambda.^2);
n_A = @(lambda) sqrt(7.986+(0.97501./(lambda.^2-(0.19886)^2))-0.0059457.*lambda.^2);
M_G = @(lambda) [cos((2*pi*d_G.*n_G(lambda))./lambda), (-1i./n_G(lambda)).*sin((2*pi*d_G.*n_G(lambda))./lambda);...
-(1i.*n_G(lambda)).*sin((2*pi*d_G.*n_G(lambda))./lambda), cos((2*pi*d_G.*n_G(lambda))./lambda)];
M_A = @(lambda) [cos((2*pi*d_A.*n_A(lambda))./lambda), (-1i./n_A(lambda)).*sin((2*pi*d_A.*n_A(lambda))./lambda);...
-(1i.*n_A(lambda)).*sin((2*pi*d_A.*n_A(lambda))./lambda), cos((2*pi*d_A.*n_A(lambda))./lambda)];
M = @(lambda) M_G(lambda)*M_A(lambda);
M_values = arrayfun(@(l) M(l), lambda, 'UniformOutput', false);
M1 = cellfun(@(m) m(1, 1), M_values);
M2 = cellfun(@(m) m(1, 2), M_values);
R = @(lambda) (M2./M1).^2;
figure(1)
plot(lambda, R(lambda), 'k')
xlabel('Wavelength (um)')
ylabel('Reflection Coefficient')
In this updated code, I've used “arrayfun” to evaluate the symbolic expressions “M(l)” for each value in the “lambda” array. Then, I used “cellfun” to extract the specific elements “M1” and “M2” from the cell array “M_values”. Finally, the “R(lambda)” calculation and plotting should work without errors.
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!