How to solve an equation vectorwise? - matlab

Is there any vectorwise equation solver in matlab?
For example if I have a vector a = [1 8 27] then solving x.^3-a=0 will give us [1 2 3]
Thanks!

Try fminsearch:
>> x = fminsearch( #(x) sum( ( x.^3 - a ).^2 ), [0 0 0] )
x =
1.0000 2.0000 3.0000

This can be done with the solve command.
The good news is that it is quite easy to use and rather powerfull.
Unfortunately you do need the symbolic toolbox to use this.
Here is an example:
syms a b c x
solve(a*x^2 + b*x + c == 0)
If you don't have the symbolic toolbox you wont be able to run this.

You can combine arrayfun and fzero to achieve this:
>> arrayfun(#(a) fzero(#(x) x^3-a,0), [1 8 27])
ans =
1.0000 2.0000 3.0000

Related

Power Method in MATLAB

I would like to implement the Power Method for determining the dominant eigenvalue and eigenvector of a matrix in MATLAB.
Here's what I wrote so far:
%function to implement power method to compute dominant
%eigenvalue/eigenevctor
function [m,y_final]=power_method(A,x);
m=0;
n=length(x);
y_final=zeros(n,1);
y_final=x;
tol=1e-3;
while(1)
mold=m;
y_final=A*y_final;
m=max(y_final);
y_final=y_final/m;
if (m-mold)<tol
break;
end
end
end
With the above code, here is a numerical example:
A=[1 1 -2;-1 2 1; 0 1 -1]
A =
1 1 -2
-1 2 1
0 1 -1
>> x=[1 1 1];
>> x=x';
>> [m,y_final]=power_method(A,x);
>> A*x
ans =
0
2
0
When comparing with the eigenvalues and eigenvectors of the above matrix in MATLAB, I did:
[V,D]=eig(A)
V =
0.3015 -0.8018 0.7071
0.9045 -0.5345 0.0000
0.3015 -0.2673 0.7071
D =
2.0000 0 0
0 1.0000 0
0 0 -1.0000
The eigenvalue coincides, but the eigenvector should be approaching [1/3 1 1/3]. Here, I get:
y_final
y_final =
0.5000
1.0000
0.5000
Is this acceptable to see this inaccuracy, or am I making some mistake?
You have the correct implementation, but you're not checking both the eigenvector and eigenvalue for convergence. You're only checking the eigenvalue for convergence. The power method estimates both the prominent eigenvector and eigenvalue, so it's probably a good idea to check to see if both converged. When I did that, I managed to get [1/3 1 1/3]. Here is how I modified your code to facilitate this:
function [m,y_final]=power_method(A,x)
m=0;
n=length(x);
y_final=x;
tol=1e-10; %// Change - make tolerance more small to ensure convergence
while(1)
mold = m;
y_old=y_final; %// Change - Save old eigenvector
y_final=A*y_final;
m=max(y_final);
y_final=y_final/m;
if abs(m-mold) < tol && norm(y_final-y_old,2) < tol %// Change - Check for both
break;
end
end
end
When I run the above code with your example input, I get:
>> [m,y_final]=power_method(A,x)
m =
2
y_final =
0.3333
1.0000
0.3333
On a side note with regards to eig, MATLAB most likely scaled that eigenvector using another norm. Remember that eigenvectors are not unique and are accurate up to scale. If you want to be sure, simply take the first column of V, which coincides with the dominant eigenvector, and divide by the largest value so that we can get one component to be normalized with the value of 1, just like the Power Method:
>> [V,D] = eig(A);
>> V(:,1) / max(abs(V(:,1)))
ans =
0.3333
1.0000
0.3333
This agrees with what you have observed.

Checking which rows switched given an original and an altered matrix in Matlab

I've been trying to wrap my head around this for awhile and was hoping to get some insight.
Suppose you have matrix A, then you switched rows until you ended up with matrix B;
A = [1 3 1;
3 2 1;
2 3 1;];
B = [3 2 1;
1 3 1;
2 3 1;];
invA =
0.0000 -1.0000 1.0000
-1.0000 -1.0000 2.0000
3.0000 5.0000 -7.0000
invB =
-1.0000 0.0000 1.0000
-1.0000 -1.0000 2.0000
5.0000 3.0000 -7.0000
How would I document these row switches?. I'm ultimately trying to alter the inverse of B to match with the inverse of A. My conclusion was that given 2 rows switched (aka between rows 1 and 2), the end result of the inverse would be identical except for switching the columns of (1 and 2) of the inverse B.
This is quite a basic algebra question.
You can write your matrix B as a product of a permutation matrix P and A:
B = PA;
(in your example: P = [0 1 0;1 0 0;0 0 1];).
Now you can invert B:
inv( B ) = inv( PA )
The inverse of a product is
= inv(A) * inv(P)
Since matrix P is a permutation matrix: inv(P) = P.'. Thus
= inv(A) * P.'
That is, inv(B) = inv(A) * P.' which means that you apply the permutation P to the columns of inv(A).
Note that a permutation P can represent more than a single switch between rows, moreover, permutations can be multiplies to account for repeated switching of rows.
An important comment: I use inv in this answer to denote the inverse of a matrix. However, when running Matlab and numerically inverting matrices it is un-recommended to use inv function explicitly.

Wrong answer for lyap() function in matlab

I'm getting a weird answer from matlab using the lyap() function for generating a stable controller
my code is
m=1;c=2;k=1;
A=[0 1;-k/m -c/m]
B=[0 1/m]'
C=[1 0;0 1];
D=[0 0]';
u=2;
Q=eye(2);
ro=60;
k=0.99*ro;
P=lyap(A,Q)
What I'm getting is
P =
1.5000 -0.5000
-0.5000 0.5000
which is giving me an unstable controller,
while when solving it alone I get
p1 =
1.5000 0.5000
0.5000 0.5000
which is a stable controller.
Any ideas?
Thanks
from Mathworks Documentation
Limitations:
The continuous Lyapunov equation has a unique solution if the eigenvalues a1,a2,...,an of A and b1,b2,...,bn of B satisfy
ai+bj ~= 0 for all i,j
and from your values
eig(A)
ans =
-1
-1
eig(Q)
ans =
1
1
we can see these add to zero, thus there is not unique solution for these inputs
However I have no idea why the error message isn't generated, possibly time to report a bug

Create vectors associated with each entry of an array and save them in a new matrix

Say i have a matrix like A = [1 2; 3 4], and that i need to create 4, vectors each one associated to one entrance of the matrix, such that the first one goes from -1..1, and second from -2..2, and so forth. Wath i try was
for j=1:2
for k=1:2
W=linspace(-A(j,k),A(j,k),4)
end
end
the problem with that line is that it not save the data.
Also i need that to create a new matrix, such that every row be one of the vectors that i mentioned.
I know that on octave i can do
W=linspace(-A,A,4)
but in MATLAB it doesn't work
If you want 4 values evenly distributed between A(k) and A(k), then you can use an anonymous function in combination with linspace this way:
fun = #(x) linspace(-A(x), A(x), 4)
b = fun(1:numel(A))
b =
-1.00000 -0.33333 0.33333 1.00000
-3.00000 -1.00000 1.00000 3.00000
-2.00000 -0.66667 0.66667 2.00000
-4.00000 -1.33333 1.33333 4.00000
Assuming you want [-1 0 1], [-2 -1 0 1 2] etc, then I suggest using arrayfun like this:
A = [1 2;3 4];
b = arrayfun(#(n) -A(n):A(n), 1:numel(A), 'UniformOutput',0)
b =
{
[1,1] =
-1 0 1
[1,2] =
-3 -2 -1 0 1 2 3
[1,3] =
-2 -1 0 1 2
[1,4] =
-4 -3 -2 -1 0 1 2 3 4
Your approach didn't work because you're overwriting W everytime you loop. The following works:
V = zeros(numel(A),4);
for k=1:numel(A)
W(k,:) = linspace(-A(k),A(k),4);
end
The reason why I only use one index for A is because you may use linear indexing in MATLAB. Remember to allocate memory before you assign values to a matrix inside a loop. "Growing" matrices are very slow.
You can do it like that
W = zeros(4,4);
a = reshape(A, 1, 4);
for i=1:4
W(i,:) = linspace(-a(i), a(i), 4);
end
and you obtain
W =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000
If you want to generate a fixed number of values (say 4) for each entry of A, you can achieve it in one line:
>> bsxfun(#times, linspace(-1,1,4), A(:))
ans =
-1.0000 -0.3333 0.3333 1.0000
-3.0000 -1.0000 1.0000 3.0000
-2.0000 -0.6667 0.6667 2.0000
-4.0000 -1.3333 1.3333 4.0000

Vectorising a function array in Matlab

My generic problem is illustrated by the following example:
f=#(x,y) cos(x.*y);
Yvalues = linspace(0,1,50);
W = #(x) f(x,Yvalues);
which works fine if I only want to evaluate W at one point at a time. For instance:
norm(W(pi/3)-f(pi/3,Yvalues))
ans =
0
But how do I go about evaluating W at any number of points?
Thanks in advance.
If you change
f=#(x,y) cos(x.*y);
to
f=#(x,y) cos(x'*y);
you can execute
W([1 2 3])
For example,
>> f = #(x,y) cos(x'*y);
>> yv = linspace(0,1,5);
>> W = #(x) f(x,yv);
>> W(1)
ans =
1.0000 0.9689 0.8776 0.7317 0.5403
>> W(2)
ans =
1.0000 0.8776 0.5403 0.0707 -0.4161
>> W(3)
ans =
1.0000 0.7317 0.0707 -0.6282 -0.9900
>> W([1 2 3])
ans =
1.0000 0.9689 0.8776 0.7317 0.5403
1.0000 0.8776 0.5403 0.0707 -0.4161
1.0000 0.7317 0.0707 -0.6282 -0.9900