How can I create a function which outputs two variables for calculating the eigenvalue * eigenvector
1 view (last 30 days)
Show older comments
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 Comment
Dyuman Joshi
on 9 May 2022
Use isequal() for the 1st output
Use if else for the 2nd output. But what is the value of 2nd output if they are equal?
Answers (1)
Nithin
on 10 Oct 2023
Edited: Nithin
on 10 Oct 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 Comment
Dyuman Joshi
on 10 Oct 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.
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!