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

3 次查看(过去 30 天)
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

回答(1 个)

Nithin
Nithin 2023-10-10
编辑:Nithin 2023-10-10
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-10-10
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by