필터 지우기
필터 지우기

Use of eig(A) for eigenvectors

조회 수: 24 (최근 30일)
Sergio
Sergio 2024년 7월 10일 15:52
댓글: Mathieu NOE 2024년 7월 10일 16:43
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

채택된 답변

Mathieu NOE
Mathieu NOE 2024년 7월 10일 16:22
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
Sergio
Sergio 2024년 7월 10일 16:34
Thanks @Mathieu NOE!!
Mathieu NOE
Mathieu NOE 2024년 7월 10일 16:43
as always, my pleasure !!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by