Matlab Solutions and Algorithms
Hello everyone, I am starting a discussion about the Matlab software and some nice codes and functions\algorithms.
I will start with a question of my own:
I have 2 small problems for you Professionals:
1) Objective : Having the M matrix below and v = eig(M), L=average of the 4 eigenvalues, compute s,t that will make the sum function minimal (S=ex5(s)).
Problem: when I type fminsearch in the command window I get this:
"Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: -719824113424418960000000000000000000000000.000000
s =
1.0e+041 *
-0.5648 -3.0343
f =
-7.1982e+041"
function S = ex5(s)
% Building: M matrix, v vector containing M eigs.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
v = eig(M);
L=0.25*(v(1) + v(2) + v(3) + v(4));
S=0;
% Calculating the sum:
for i=1:4
S=S+((v(i)-L)^2);
end
end
Please help me understand and show me please how to avoid this warning and to repair the function. Where is the mistake?
2) With the same M from 1, Objective: Compute the sum of squares of abstract minimum values of M eigenvalues. Minimum values - the abstract minimum values:
function S = ex6A(s)
% Building: M matrix, v vector containing M eigs.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
v=eig(M);
% Building the column vector (dynamic allocation of vector (array) that
% will conatain the abs values of the each M eigen value.
temp = zeros(size(v,2),1);
S=0;
for i=1:size(v)
temp(i) = abs(v(i));
end
vsorted = sort(temp);
% Calculating the sum of the squared minimum M eigen values:
S = (vsorted(1))^2+(vsorted(2))^2;
Also I have to compute the sum of squares of minimum coefficient of M characteristic polynomial (mean poly(M)) function. Minimum coefficient - the x^0 and x^1 coefficient:
function Sum = ex6B(s)
% Building: M matrix from 1b.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
Sum = 0;
% Find the CharPol of M:
P=poly(M);
sizep=size(P,2);
% Calculating the sum of the squared minimum P coefficients the -
% x^0 and x^1:
Sum = (P(sizep))^2+(P(sizep-1))^2;
end
The question that I can't answer is: Why those 2 functions need to return the same min values for same s,t (I don't get same values, please help why?)
and to explain if those functions are the same.
With great thanks in advance, Steve.
I will start with a question of my own:
I have 2 small problems for you Professionals:
1) Objective : Having the M matrix below and v = eig(M), L=average of the 4 eigenvalues, compute s,t that will make the sum function minimal (S=ex5(s)).
Problem: when I type fminsearch in the command window I get this:
"Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: -719824113424418960000000000000000000000000.000000
s =
1.0e+041 *
-0.5648 -3.0343
f =
-7.1982e+041"
function S = ex5(s)
% Building: M matrix, v vector containing M eigs.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
v = eig(M);
L=0.25*(v(1) + v(2) + v(3) + v(4));
S=0;
% Calculating the sum:
for i=1:4
S=S+((v(i)-L)^2);
end
end
Please help me understand and show me please how to avoid this warning and to repair the function. Where is the mistake?
2) With the same M from 1, Objective: Compute the sum of squares of abstract minimum values of M eigenvalues. Minimum values - the abstract minimum values:
function S = ex6A(s)
% Building: M matrix, v vector containing M eigs.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
v=eig(M);
% Building the column vector (dynamic allocation of vector (array) that
% will conatain the abs values of the each M eigen value.
temp = zeros(size(v,2),1);
S=0;
for i=1:size(v)
temp(i) = abs(v(i));
end
vsorted = sort(temp);
% Calculating the sum of the squared minimum M eigen values:
S = (vsorted(1))^2+(vsorted(2))^2;
Also I have to compute the sum of squares of minimum coefficient of M characteristic polynomial (mean poly(M)) function. Minimum coefficient - the x^0 and x^1 coefficient:
function Sum = ex6B(s)
% Building: M matrix from 1b.
M = [-2 2 1 8; 2 1 1 0; s(1) s(2) -1 0; 1 0 3 -2];
Sum = 0;
% Find the CharPol of M:
P=poly(M);
sizep=size(P,2);
% Calculating the sum of the squared minimum P coefficients the -
% x^0 and x^1:
Sum = (P(sizep))^2+(P(sizep-1))^2;
end
The question that I can't answer is: Why those 2 functions need to return the same min values for same s,t (I don't get same values, please help why?)
and to explain if those functions are the same.
With great thanks in advance, Steve.
0