Use of eig(A) for eigenvectors

5 vues (au cours des 30 derniers jours)
Sergio
Sergio le 10 Juil 2024
Commenté : Mathieu NOE le 10 Juil 2024
Hi, I try to call the eigenvectors of the respective eigenvalue, but that part in the code gives an error:
"Unrecognize function or variable 'eigvecs_theta'
I tried eig and eigvec instead of eigvecs_theta, but nothing works. What did I miss here? Thanks
% Discretization parameters
theta_min = 0;
theta_max = pi;
M = 100; % Number of grid points
theta = linspace(theta_min, theta_max, M);
dtheta = theta(2) - theta(1);
% Initialize the Theta(theta) vector
Theta = zeros(M, 1);
% Set up the finite difference matrix
B = zeros(M, M);
for j = 3:M-2
B(j, j-2) = -1 / (2 * dtheta^3);
B(j, j-1) = 2 / (dtheta^3);
B(j, j) = -2 / (dtheta^3) + l * (l + 1);
B(j, j+1) = 2 / (dtheta^3);
B(j, j+2) = -1 / (2 * dtheta^3);
end
% Apply boundary conditions (example: Theta(0) = 0 and Theta(pi) = 0)
B(1,1) = 1;
B(M,M) = 1;
% Solve the eigenvalue problem
[~, D_theta] = eig(B);
% The eigenvalues are the diagonal elements of D_theta
eigenvalues_theta = diag(D_theta);
% The solution Theta(theta) corresponds to the eigenvector with the desired eigenvalue
Theta = eig(:, idx);
% Plot the solution
plot(theta, Theta);
xlabel('theta');
ylabel('Theta(theta)');
title('Angular Wavefunction');
end

Réponse acceptée

Mathieu NOE
Mathieu NOE le 10 Juil 2024
hello
let's fix it
1/ had to remove the trailing end - there's simply no reason to have an "end" at the end of your code - probably a copy paste mistake.
2/ add the init of missing variable l (copy pasted from your other post Script has no errors, but no plot is given - MATLAB Answers - MATLAB Central (mathworks.com) ) - please put the correct value and definition if I'm wrong here
3/ corrected Theta computation - I believe there is no idx indexing involved in this code - or it's not defined on your side - i simply assumed you wanted to plot the entire array of eigenvalues vs theta.
% I added following lines
l = 1; % angular momentum quantum number
% Discretization parameters
theta_min = 0;
theta_max = pi;
M = 100; % Number of grid points
theta = linspace(theta_min, theta_max, M);
dtheta = theta(2) - theta(1);
% Initialize the Theta(theta) vector
Theta = zeros(M, 1);
% Set up the finite difference matrix
B = zeros(M, M);
for j = 3:M-2
B(j, j-2) = -1 / (2 * dtheta^3);
B(j, j-1) = 2 / (dtheta^3);
B(j, j) = -2 / (dtheta^3) + l * (l + 1);
B(j, j+1) = 2 / (dtheta^3);
B(j, j+2) = -1 / (2 * dtheta^3);
end
% Apply boundary conditions (example: Theta(0) = 0 and Theta(pi) = 0)
B(1,1) = 1;
B(M,M) = 1;
% Solve the eigenvalue problem
[~, D_theta] = eig(B);
% The eigenvalues are the diagonal elements of D_theta
eigenvalues_theta = diag(D_theta);
% The solution Theta(theta) corresponds to the eigenvector with the desired eigenvalue
%Theta = eig(:, idx); % NO !! eig is a function not a variable
% Theta = eigenvalues_theta(:, idx); % maybe but where is defined idx ??
Theta = eigenvalues_theta; % my 2 cents , just to make the code work here
% Plot the solution
plot(theta, Theta);
xlabel('theta');
ylabel('Theta(theta)');
title('Angular Wavefunction');
  2 commentaires
Sergio
Sergio le 10 Juil 2024
Thanks @Mathieu NOE!!
Mathieu NOE
Mathieu NOE le 10 Juil 2024
as always, my pleasure !!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by