# How to multiply matrices randomly?

3 views (last 30 days)
GULZAR on 1 Apr 2024
Commented: GULZAR on 1 Apr 2024
I need a matlab code for positional-random matrix multiplication (choose M1 or M2 randomly)
frequency = 100 to 200 THz (i.e 100e12 to 200e12 Hz)
M1=[ cos(phi_1), sin(phi_1); sin(phi_1), cos(phi_1) ] where phi_1=d1*n1*frequency, d1= 258 nm and n1=1.45
M2=[ cos(phi_2), sin(phi_2); sin(phi_2), cos(phi_2) ] where phi_2=d2*n2*frequency, d2= 163 nm and n2=2.3
M=M1M2M1M1M2M1M2M1M2M2........ (random multiplication) (total matrix) (200 micro meters length i.e approximately 500 layers)
I need a plot of transmission vs frequency
T=1/M(1,1) (transmission vs frequency)
And I need a plot of average transmission vs total length (200 micro meters)
Mean_T=avg(T) (average transmission vs total length)

Hassaan on 1 Apr 2024
% Parameters
d1 = 258e-9; % meters
n1 = 1.45;
d2 = 163e-9; % meters
n2 = 2.3;
totalLength = 200e-6; % meters
layerCount = 500; % Approximate layers for 200 micrometers
frequencyRange = 100e12:1e12:200e12; % 100 to 200 THz, step by 1 THz for smooth plot
% Preallocate transmission array
transmission = zeros(length(frequencyRange), 1);
% Loop over each frequency
for i = 1:length(frequencyRange)
frequency = frequencyRange(i);
% Initialize total matrix as identity
M_total = eye(2);
% Generate random sequence of M1 and M2 multiplications
for j = 1:layerCount
if rand > 0.5
phi_1 = d1 * n1 * frequency;
M = [cos(phi_1), sin(phi_1); sin(phi_1), -cos(phi_1)];
else
phi_2 = d2 * n2 * frequency;
M = [cos(phi_2), sin(phi_2); sin(phi_2), -cos(phi_2)];
end
M_total = M_total * M;
end
% Calculate transmission
T = 1 / M_total(1,1);
transmission(i) = T;
end
% Plot Transmission vs Frequency
figure;
plot(frequencyRange * 1e-12, abs(transmission)); % Convert frequency to THz for plotting
xlabel('Frequency (THz)');
ylabel('Transmission');
title('Transmission vs Frequency');
% Plot Average Transmission vs Total Length
% Since total length is constant in this scenario, we show how to calculate the average
mean_T = mean(abs(transmission));
figure;
plot(totalLength * 1e6, mean_T, 'ro'); % Convert meters to micrometers for plotting
xlabel('Total Length (micrometers)');
ylabel('Average Transmission');
title('Average Transmission vs Total Length');
ylim([0, max(abs(transmission)) + 0.1]); % Adjust ylim to make the single point more visible
% Note: The calculation of average transmission vs total length doesn't vary with length in this script,
% as we've kept the length constant at 200 micrometers. This plot will just illustrate the average transmission for the given length.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
• Technical Services and Consulting
• Embedded Systems | Firmware Developement | Simulations
• Electrical and Electronics Engineering
Feel free to contact me.
GULZAR on 1 Apr 2024
It's working well.
GULZAR on 1 Apr 2024
I need the plot of average transmission versus sample length (i.e 1 micro meter to 200 micro meters range)

### Categories

Find more on Switches and Breakers in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!