How can I create this function?
Afficher commentaires plus anciens
I need help writing a function that will carry out the following:
y(x) = Bj*Cj(x)*aj
where Cj = diag[exp(im*k*x),exp(-im*k*x)]
Bj and aj are not dependent on x
How can I write code for this in MATLAB?
Réponse acceptée
Plus de réponses (1)
CARLOS RIASCOS
le 5 Avr 2018
This code should return in the variables: B_mtrx_a, C_mtrx_a, a_; a "history" of calculations of matrix B, C and vector a. Observe the command window to show the calculations for each iteration of the for loop.
clear all
im=1i; %Imaginary part
% You should modify these variables:
Z = [1;2];
k = [1;2];
X = [1;2];
V = ones(2);
T = ones(2);
% Definition of the functions:
B = @(x)([1 1; im*Z(x,1) -im*Z(x,1)]);
C = @(x)([exp(im*k(x,1)*X(x,1)) 0; 0 exp(-im*k(x,1)*X(x,1))]);
layers = 2;
%pre-allocation, to save the history of calculations.
B_mtrx_a = [];
C_mtrx_a = [];
a_a = [];
for j = 1:layers
B_mtrx = B(j); C_mtrx = C(j);
disp('B:')
disp(B_mtrx)
disp('C:')
disp(C_mtrx)
B_mtrx_a = [B_mtrx_a B(j)];
C_mtrx_a = [C_mtrx_a C(j)];
if j == 1
a = inv(B_mtrx)*V(:,1);
a_a = [a_a a];
alfa = B_mtrx; beta = a; %You save the specific
%values in auxiliary variables.
else
a = inv(C_mtrx)*inv(B_mtrx)*T*alfa*beta;
a_a = [a_a a];
end
disp('a:')
disp(a)
end
1 commentaire
Amanda Lococo
le 6 Avr 2018
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!