How to generate fx value according to number of samples?
2 views (last 30 days)
Show older comments
I have a matlab code of matrix multiplication with normal distribution
I need a output corresponding to number of samples = 20
I have a thickness data by normal distribution with 10x20 matrix . That number of rows data i used to generate matrix multiplication. I need fx values corresponds with that number of columns data( i.e, i need 20 output values)
How to do this in matlab.
clc
clear
close all
x=pi:0.01:2*pi; %%% x domain
mu=25;
sigma=1.5;
n=10;
num_samples=20;
d0=25; %%% thickness of layer 1
d1= normrnd(mu,sigma,[n,num_samples]); %%% thickness of layer 2 by normal distribution
for i =1:length(x)
x_value=x(i);
X1=x_value*d0;
Fx=[sin(X1) 1i; -1i cos(X1)]; %%% Matrix for layer 1
Fy = eye(2); % Initialize total matrix as identity for layer 2
for j = 1:n
X2=x_value*d1(j);
M=[sin(X2) 1i; -1i cos(X2)]; %%% Matrix for layer 1
Fy= Fy * M;
end
Fxx=(Fx^n*Fy); %%% Matrix multiplication
fx=Fxx(1,1)+Fxx(2,2); %%% output
end
disp(fx)
0 Comments
Accepted Answer
Ayush Aniket
on 11 Jun 2024
Hi Gulzar,
It seems like you are trying to perform matrix multiplication for a layered medium simulation, where the thickness of each layer is determined by a normal distribution. You want to generate 20 output values corresponding to the number of samples in your normal distribution matrix ('d1'), for a given set of 'x' values.
However, your code calculates 'fx' for each value of 'x' in the loop but overwrites it in each iteration, and the final 'disp(fx)' only shows the last computed value of 'fx'.
To achieve 20 distinct output values corresponding to each column in your 'd1' matrix (each sample), you need to adjust your approach as shown below:
clc
clear
close all
x = pi:0.01:2*pi;
mu = 25;
sigma = 1.5;
n = 10;
num_samples = 20;
d0 = 25;
d1 = normrnd(mu, sigma, [n, num_samples]);
% Initialize an array to store the output values of fx for each sample
fx_values = zeros(1, num_samples);
% Loop over each sample
for sample = 1:num_samples
Fy = eye(2); % Initialize total matrix as identity for layer 2
for i = 1:length(x)
x_value = x(i);
X1 = x_value * d0;
Fx = [sin(X1) 1i; -1i cos(X1)];
% Reset Fy for each x value
Fy = eye(2);
for j = 1:n
X2 = x_value * d1(j, sample);
M = [sin(X2) 1i; -1i cos(X2)];
Fy = Fy * M; % Update Fy for each layer
end
Fxx = (Fx^n * Fy);
fx = Fxx(1,1) + Fxx(2,2);
end
% Store the last computed fx value for this sample
fx_values(sample) = fx;
end
disp(fx_values)
In the above code, 'fx_values' stores the final 'fx' value for each sample. This is necessary because you want 20 distinct outputs corresponding to your 'num_samples'.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!