How can I create a function which outputs two variables for calculating the eigenvalue * eigenvector
1 次查看(过去 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 个评论
Dyuman Joshi
2022-5-9
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?
回答(1 个)
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!