Hey guys I have multiple problems with using function 'roots'.
I Have to find zeros of 's^1000 + 1'.
I made Y = zeros(1,1000) then manually changed the 1000th matrice to '1'. but then 'root' function does not work with it !
Another problem is that I am having trouble with matrix multiplication. The question is finding zeros(roots) of (s^6 + 6*s^5 + 15*s^4 + 20*s^3 + 15*s^2 + 6*s +1)*(s^6 + 6s^5 + 15*s^4 +15*s^2 +6*s +1) so i did:
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
roots(y)
but this gives me
-27.9355 + 0.0000i
-8.2158 + 0.0000i
0.1544 + 0.9880i
0.1544 - 0.9880i
-0.1217 + 0.0000i
-0.0358 + 0.0000i
where I calculate the original equation with wolfram then I have made matrix as :
p = [1 12 66 200 375 492 524 492 375 200 66 12 1]
roots(p)
and this gives me :
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051 + 0.0000i
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9975 + 0.0044i
-0.9975 - 0.0044i
-0.9949 + 0.0000i
-0.1943 + 0.1539i
-0.1943 - 0.1539i
and I think the second solution is right (that is what wolfram alpha gave me)
How would you answer these two questions through matlab guys?
To multiply polynomials, you convolve their coefficients:
>> roots(conv(a,b))
ans =
-3.1629 + 2.5046i
-3.1629 - 2.5046i
0.3572 + 0.9340i
0.3572 - 0.9340i
-1.0051
-1.0025 + 0.0044i
-1.0025 - 0.0044i
-0.9974 + 0.0044i
-0.9974 - 0.0044i
-0.9950
-0.1943 + 0.1539i
-0.1943 - 0.1539i
Q1
Using roots to find the roots of s1000 + 1 is a bit of an overkill. The solution is given by this code snippet (corrected the first version; may be deduced using De Moivre's formula):
n = 1000;
k = 0:n-1
u = (2*k + 1)*pi / n;
s = cos(u) + 1i*sin(u)
Also, this method is approx. 100000 times faster.
Q2
Multiplying two polynomials to find the roots of their product is a bit of an overkill. :-) The union of the two polynomials' root sets is the root set of the product polynomial:
s = [roots(a);roots(b)]
Also, this method is more accurate.
1) The vector describing s^1000 + 1 should end with a 1 as well.
2)
a = [1 6 15 20 15 6 1]
b = [1 6 15 0 15 6 1]
y = a.*b;
This is a DOT product, multiplication of polynomials do not multiply element-wise.
Question 1
You need to include the coefficient of x^0 in the vector of coefficients, so there are 1001 entries with the first and last being 1
coeffs=zeros(1001,1);
coeffs([1,1001])=1;
roots(coeffs)
Question 2
To multiply the coefficients of polynomials you need to use convolution:
roots(conv(a,b))
Related
I have to factorize a polynomial e.g.
over the field of Z5 using Matlab or Mupad.
And i tried everything read a lot of Matlab and Mupad documentation and still can't find it, so i am guessing it is the math i don't know that's going to help me factor it.
Don't kill a mosquito with a cannon!
You only need to find a root between 0, 1, 2, -2, -1.
Also, given that x5 = x, the problem reduces to finding x such that
2x + 2x^4 + x^3 + 2x^2 - 3 = 0
and since x ≠ 0, x^4 = 1 hence
2x + x^3 + 2x^2 - 1 = 0
Well, let's try!
1: 2 + 1 + 2 - 1 -> -1
2: -1 + 3 - 2 - 1 -> -1
-2: 1 - 3 + 3 - 1 -> 0 -> root!
Then the polynomial is divisible by (x - 3), and you can repeat the procedure with the quotient until there are no roots left.
Addendum
After dividing by (x - 3) we get
x4 + x2 + 1
which we can expressed as
(x2 + 1)2 - x2
or
((x2 + 1) - x)((x2 + 1) + x)
To find the factors of degree 2 programmatically, just try with x2 + ax + b for a and b between 0 and 4.
I found a mupad command to do what i needed.
Still thanks for exaplaining the math behind it.
This is matrix B
B = [1 2 0 ; 2 4 6 ; 0 6 5]
The result of eig(B) is:
{-2.2240, 1.5109, 10.7131}
and the characteristic polynomial of B by this link is
syms x
polyB = charpoly(B,x)
x^3 - 10*x^2 - 11*x + 36
but the answer of solve(polyB) is
133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) + ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3) + 10/3
(3^(1/2)*(133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3))*i)/2 - 133/(18*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3)/2 + 10/3
10/3 - 133/(18*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3)/2 - (3^(1/2)*(133/(9*(3^(1/2)*5492^(1/2)*(i/3) + 1009/27)^(1/3)) - ((3^(1/2)*5492^(1/2)*i)/3 + 1009/27)^(1/3))*i)/2
which I don't know what it is while I expect it to be the eigenvalues of B. What is the problem?
I do not understand why you add x and symbolic maths, they are not required for your task.
B = [1 2 0 ; 2 4 6 ; 0 6 5]
cp=charpoly(B)
eig2=roots(cp)
returns:
eig2 =
10.7131
-2.2240
1.5109
However, if for some reason you insist in using symbolic (which you should not for a numerical task), you can do
double(solve(polyB))
ans =
10.7131 + 0.0000i
-2.2240 - 0.0000i
1.5109 - 0.0000i
(note imaginary parts is zero)
Since I do not have MATLAB in this machine, I will use SymPy instead:
>>> from sympy import *
>>> B = Matrix([[1, 2, 0],
[2, 4, 6],
[0, 6, 5]])
Computing the characteristic polynomial and its roots:
>>> s = Symbol('s')
>>> p = (s*eye(3) - B).det()
>>> p
s**3 - 10*s**2 - 11*s + 36
>>> roots = solve(p,s)
Computing floating-point approximations of the three roots:
>>> [ r.evalf() for r in roots ]
[1.51092975992931 - 0.e-22*I, -2.22404024437578 + 0.e-22*I, 10.7131104844465 - 0.e-20*I]
Since B is symmetric, its eigenvalues must be real. Note that the imaginary parts of the floating-point approximations of the roots are indeed equal to zero.
Printing in LaTeX, the exact values of the roots are:
Note that some roots are "longer" than others, i.e., they require more symbols. However, they are exact. Using floating-point arithmetic, all roots have the same "size", but they are approximations.
I'm completely new to Matlab and I've been having some problems with my code.
I need to use 'fsolve' to locate minimizers to a nonlinear optimization problem, but I can't get it to work.
I've been using the 'Solution Process of Nonlinear System' part from mathworks.
My code is the following:
function F = myfun(x)
F = [4*x(4) + 2*x(1) - x(3)*(2*x(1) + 4) + 4*x(1)*(x(1).^2 + x(2) - 11) + 2*x(2).^2 - 14;
x(3) - 10*x(4) + 2*x(2) + 4*x(2)*(x(2).^2 + x(1) - 7) + 2*x(1).^2 - 22;
x(2) - (x(1) + 2).^2;
4*x(1) - 10*x(2);];
x0 = [-5;-5];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(#myfun,x0,options);
end
Where it says that; the value assigned to x appears to be unused as well as; not enough input arguments. But accoring to the mathworks page, I've done the exact same thing as them, so I'm sort of lost now.
There are several things that you are not doing correctly:
The function myfun must only contain equations (as mentioned in Florian's comment).
fsolve, x0 and options must be called from a separate script or from the Command Window.
The initial point array x0 must have at least as many elements as variables (x(4), x(3), x(2), x(1)) present in the equations in myfun.
Edit your function myfun, make sure that it only contains equations and save it in your working directory.
function F = myfun(x)
F = [4*x(4) + 2*x(1) - x(3)*(2*x(1) + 4) + 4*x(1)*(x(1)^2 + x(2) - 11) + 2*x(2)^2 - 14;
x(3) - 10*x(4) + 2*x(2) + 4*x(2)*(x(2)^2 + x(1) - 7) + 2*x(1)^2 - 22;
x(2) - (x(1) + 2)^2;
4*x(1) - 10*x(2)];
end
Note that you don't need to use the element-wise power operator .^. Using the ^ operator will be enough to define powers in your equations.
Now in the Command Window enter the following instructions:
x0 = [-5;-5;-5;-5];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(#myfun,x0,options);
Note that I modified x0 to have at least 4 elements in order to match the number of equations that you defined in myfun. These are just sample values, so you should modify x0 with the values of your problem.
This is a fragment of the output:
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 5 81521 4.27e+04 1
1 10 12608.5 1 1.15e+04 1
2 15 966.243 2.5 1.71e+03 2.5
3 20 408.322 6.25 685 6.25
4 21 408.322 15.625 685 15.6
5 26 263.815 3.90625 244 3.91
6 27 263.815 9.76563 244 9.77
7 32 205.88 2.44141 272 2.44
8 37 138.11 6.10352 206 6.1
9 42 93.4561 6.10352 105 6.1
10 47 64.0129 6.10352 42.3 6.1
...
For example, if I have this function: g = t^3 - 5*t^2 + 2
And g = [3 4 6 2 9 10 17 1]
I would like to solve the equation for each g[i] and obtain the resulting t vector.
This might guide you:
>> syms t g %// define symbolic variables
>> y = t^3 - 5*t^2 + 2 - g; %// define y so that equation is: y=0
>> g_data = [3 4 6 2 9 10 17 1]; %// define g values
>> n = 1; %// choose first value. Or use a loop: for n = 1:numel(g_data)
>> s = solve(subs(y, g, g_data(n))) %// substitute g value and solve equation y=0
s =
25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3) + 5/3
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
5/3 - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)/2 - 25/(18*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) + (3^(1/2)*(25/(9*((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3)) - ((108^(1/2)*527^(1/2))/108 + 277/54)^(1/3))*i)/2
>> double(s) %// show solutions as floating point values
ans =
5.039377328113847
-0.019688664056924 + 0.445027607060817i
-0.019688664056924 - 0.445027607060817i
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I have a 3x3 matrix, for example A=[3,2,4;2,0,2;4,2,3], and I'm trying to solve the following linear system in the form A*[a;b;c] = [8*a;8*b;8*c]:
3 2 4 a 8a
2 0 2 * b = 8b
4 2 3 c 8c
Ok, so I have:
3a + 2b + 4c = 8a
2a + 2c = 8b
4a + 2b + 3c = 8c
It's underdetermined, and an answer would be [2;1;2] In fact, if I use an online linear solver like this one, it will give me an answer like this:
{ a = r1, b = r1/2, c = r1 }
Still, I can't find a way to do that in Matlab. If I define B as B=[8*a;8*b;8*c] and try A\B, i get:
2*b - 4*a + 4*c
2*a - 7*b + 2*c
4*a + 2*b - 4*c
And if I define B as [8;8;8], I get:
2
-3
2
While I expected:
2
1
2
Or something like the answer from the online solver form above. What am I doing wrong? Thanks in advance!
Oh well, even though this is NOT a programming problem, I'll answer it.
The question is to solve the linear system
Ax = 8x
where the 3x3 matrix A is given, and x is a 3x1 unknown vector.
A = [3 2 4;2 0 2;4 2 3];
[v,d] = eig(A)
v =
-0.4941 -0.5580 0.6667
-0.4720 0.8161 0.3333
0.7301 0.1500 0.6667
d =
-1.0000 0 0
0 -1.0000 0
0 0 8.0000
Here we see that the third eigenvalue is 8, so there is indeed a non-degenerate solution to the problem. It is of the form
k*v(:,3)
since v(:,3) is the corresponding eigenvector.
format rat
v(:,3)
ans =
2/3
1/3
2/3
Clearly this results in the solution given by the asker.
I'll note that this all works ONLY because the problem was posed in the form A*x=lambda*x, so a classical eigenvalue problem. Again, if you appreciate the mathematics behind the solution, then we can solve the problem using null:
null(A - 8*eye(3))
ans =
2/3
1/3
2/3
Of course, we could have used the symbolic toolbox.
sol = solve('3*a + 2*b + 4*c = 8*a','2*a + 2*c = 8*b','4*a + 2*b + 3*c = 8*c');
sol.a
ans =
z
sol.b
ans =
z/2
sol.c
ans =
z
Suppose instead that the problem was a completely general one? Thus, still a homogeneous linear system, but not an obvious eigenvalue problem? As an example, I'll try to solve the arbitrary linear problem
A*[a;b;c] = [a;2*b;3*c]
See that this is NOT written in the form of an eigenvalue problem. There are actually several ways we might decide to solve it. The unknowns are on BOTH sides of the equality. So just move them all to the left hand side. Semi-mathematically, we might do this as
B = A - diag([1 2 3])
B =
2 2 4
2 -2 2
4 2 0
We now try to solve the linear system
Bx = B*[a;b;c] = [0;0;0]
Do solutions to this exist? This time, they do not exist, beyond the trivial, degenerate solution, because B has full rank.
rank(B)
ans =
3
A full rank, homogeneous linear system has only the degenerate (zero) solution. Null tells us this too.
null(B)
ans =
Empty matrix: 3-by-0
The symbolic toolbox solution reflects that fact.
sol = solve('3*a + 2*b + 4*c = a','2*a + 2*c = 2*b','4*a + 2*b + 3*c = 3*c')
sol.a
ans =
0
sol.b
ans =
0
sol.c
ans =
0
Until you appreciate the mathematics of the linear systems involved, this really is NOT a programming problem, and then it is essentially one command to solve the problem, so still not really much of a programming problem.