Write a wellcommented MATLAB function program mypm that inpu
Write a well-commented MATLAB function program ‘mypm’ that inputs a matrix and a tolerance, applies the power method until the change in the vector is less than the tolerance, and outputs the number of steps used and the largest estimated eigenvalue. Test your program on the matrix B
-2 0 1 B=11-2 0 -2Solution
Main_Prog:
clc;
 clear all;
 close all;
 A = input(\'Enter the Matrix:\'); %Reading matrix from command window
 tolerance = input(\'Enter the Tolerance:\');%Reading tolerance value from command window
 [vec,val,j] = mypm(A,tolerance);% Calling \"mypm\" function by providing input matrix and tolerance
 disp(\'The Eigen Value is:\');
 disp(val); % Displaying the Dominant Eigen Value in command window
 disp(\'The no. of iterations are:\');
 disp(j); % Displaying the No. of iterations in command window
Function Program \'mypm\':
%Power method for computing eigenvalues
 function [vector,value,i]=mypm(B,tolerance) % Creatinf function program \'mypm\'
 x = [1;1;1]; % Start matrix for initial approximation
 dd=1;% Maximum tolerance allowed
 n=10;% Initial assumption of dominant eigen vector
 i=0;% Initialization for calculating no. of iterations
 while dd > tolerance % Checking the condition for change in the vector is less than the tolerance
 y=B*x
 dd=abs(norm(x)-n);
 n=norm(x)
 x=y/n
 i = i+1; % Final total No. of iterations
 end
 vector=x;% final dominant eigen vector
 value=n;% final dominant eigen vector
Output in command window:
Enter the Matrix:[-2 1 0;1 -2 1;0 1 -2]
 Enter the Tolerance:0.1
y =
-1
 0
 -1
 n =
1.7321
 x =
-0.5774
 0
 -0.5774
 y =
1.1547
 -1.1547
 1.1547
 n =
0.8165
 x =
1.4142
 -1.4142
 1.4142
 y =
-4.2426
 5.6569
 -4.2426
 n =
2.4495
 x =
-1.7321
 2.3094
 -1.7321
 y =
5.7735
 -8.0829
 5.7735
 n =
3.3665
 x =
1.7150
 -2.4010
 1.7150
 y =
-5.8310
 8.2319
 -5.8310
 n =
3.4128
 x =
-1.7086
 2.4121
 -1.7086
The Eigen Value is:
 3.4128
The no. of iterations are:
 5



