How can I create a function which outputs two variables for calculating the eigenvalue * eigenvector

1 visualización (últimos 30 días)
m1 = [7 3; 3 -1] % Matrix
[Vectors, DiagonalWithValues] = eig(m1)
ListEvalues = diag(DiagonalWithValues)
How can I implement a function which takes these 3 inputs (1. initial matrix, 2. eigenvectors, 3.eigenvalues) and returns two variables
1. a boolean variable, which indicates if both are equal
2. result of the operation for example if they are not equal it should return -1

Respuestas (1)

Nithin
Nithin el 10 de Oct. de 2023
Editada: Nithin el 10 de Oct. de 2023
Hi Jonas,
I understand that you want to create a function which outputs two variables on calculating the product of eigen value and eigen vector and then write a condition to generate the function output based on the match or mismatch of the variables.
To implement this function, kindly refer to the following code snippet -
M1 = [7 3; 3 -1] %given matrix
[eigenvectors, eigenvalues] = eig(M1);
function [isEqual, result] = checkEigenMatch(M1, eigenvalues, eigenvectors)
% Calculate the product of M1 * eigenvectors
product = M1 * eigenvectors;
% Calculate the product of eigenvalues * eigenvectors
productEigen = eigenvalues * eigenvectors;
% Check if the two products are equal within a small tolerance
tolerance = 1e-6;
isEqual = isequal(product, productEigen, tolerance);
% Set result based on the comparison
if isEqual
result = true;
else
result = -1;
end
end
To know more information regarding the calculation of eigen vectors and eigen values, kindly refer to the following documentation:
I hope this answer resolves your query.
Regards,
Nithin Kumar.
  1 comentario
Dyuman Joshi
Dyuman Joshi el 10 de Oct. de 2023
@Nithin, Please don't do obvious homework questions on MATLAB Answers, when no effort has been shown. This does not help the student, who learns nothing more than to post all of their homwork problems on the site, hoping they will find someone willing to do their thinking and work for them. Worse that that, it convinces the next student who comes along to do the same.
If you want to help, then find a way to push the student in the right direction. But posting a complete solution does far more harm than good.

Iniciar sesión para comentar.

Categorías

Más información sobre Linear Algebra en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by