Solve constrained equation with 2 variables - matlab

EDIT:
I think what I need is to apply fmincon, but I don't know how. I want to do this:
Use fmincon to solve:
Minimize a, subject to the equality constraint
f(q,z) = 0
by varying the unknowns (q,z).
How do I use fmincon to implement this? Thanks!
Here is the entire function:
f(q,z) = 60*q^9*z^4 + 120*q^7*z^3 + 80*q^(17/2)*z^3 - 60*q^8*z^3 - 60*q^6*z^3 + 40*q^(15/2)*z^3 + 20*q^(11/2)*z^3 - 20*q^9*z^3 + 175*q^(13/2)*z^2 - 90*q^(15/2)*z^2 + 75*q^5*z^2 - 60*q^6*z^2 - 45*q^(11/2)*z^2 + 40*q^2*z^2 + 30*q^8*z^2 - 30*q^(3/2)*z^2 - 10*q^(17/2)*z^2 + 15*q^(9/2)*z^2 - 20*q^4*z^2 - 10*q^(7/2)*z^2 - 90*q^(11/2)*z + 86*q^(9/2)*z + 75*q^6*z - 45*q^(7/2)*z + 40*q^(3/2)*z - 30*q^7*z - 20*q^(13/2)*z - 24*q^(1/2)*z + 15*q^4*z + 15*q^3*z - 9*q^(5/2)*z - 9*q^5*z + 45*q^4 - 30*q^5 + 30*q^(1/2) - 29*q^3 + 21*q^(5/2) - 10*q^(9/2) - 9*q^2 - 24.
----- original post -------
I have a function of 2 variables q and z like
f(q,z) = 60*q^9*z^4 + 120*q^7*z^3 + 80*q^(17/2)*z^3 - 60*q^8*z^3 - 60*q^6*z^3 + ...
I need to find
f(q,z) = 0
with respect to z, but this is not possible analytically (resulting in RootOf). So instead of finding all points, I want to numerically find the smallest number z such that the value pair (q,z) fulfills f(q,z) = 0. Any ideas how to do this?

I'm afraid there is no minimum z, as f(q,z) = 0: q↓0 ⇒ z↓-∞.
Your original equation can be written as:
Writing all the terms following like powers of z as A, B, C, D and E, you basically have
This is a quartic equation in z, which can definitely be solved analytically. Granted, it's a bit awkward, but possible nonetheless.
Note that the fact that A = f(q), B = g(q), C = h(q), D = k(q) and E = m(q) does not matter, as your goal is to express z = F(q).
Here's one way to find the minimum z for which f(q,z)=0 holds:
function find_minimum_z
solution = fminsearch(#F, 3)
end
function R = F(q)
R = roots([
60.*q.^9
120.*q.^7 + 80.*q.^(17/2) - 60.*q.^8 - 60.*q.^6 + 40.*q.^(15/2) + 20.*q.^(11/2) - 20.*q.^9
175.*q.^(13/2) - 90.*q.^(15/2) + 75.*q.^5 - 60.*q.^6 - 45.*q.^(11/2) + 40.*q.^2 + 30.*q.^8 - 30.*q.^(3/2) - 10.*q.^(17/2) + 15.*q.^(9/2) - 20.*q.^4 - 10.*q.^(7/2)
-90.*q.^(11/2) + 86.*q.^(9/2) + 75.*q.^6 - 45.*q.^(7/2) + 40.*q.^(3/2) - 30.*q.^7 - 20.*q.^(13/2) - 24.*q.^(1/2) + 15.*q.^4 + 15.*q.^3 - 9.*q.^(5/2) - 9.*q.^5
45.*q.^4 - 30.*q.^5 + 30.*q.^(1/2) - 29.*q.^3 + 21.*q.^(5/2) - 10.*q.^(9/2) - 9.*q.^2 - 24
]);
R = min(R(imag(R)==0));
if isempty(R)
R = NaN; end
end
My trials indicate there is an asymptote at q=0 for which the minimum, real z that solves the quartic tends to -∞.
Indeed this is what you can see when you make a plot of F(q):

If all terms after the first term have a z³, then it's really quite simple:
f(q,z) = 60q⁹z⁴ + 120q⁷z³ + 80q¹⁷/²z³ - 60q⁸z³ - 60q⁶z³ + ...
= z⁴·(60q⁹) + z³·(120q⁷ + 80q¹⁷/² - 60q⁸ - 60q⁶ + ... )
f(q,z) = 0
⇒ z⁴ · (60q⁹) = -z³ · (120q⁷ + 80q¹⁷/² - 60q⁸ - 60q⁶ + ...)
⇒ -z · (60q⁹) = 120q⁷ + 80q¹⁷/² - 60q⁸ - 60q⁶ + ...
⇒ z = -(120q⁷ + 80q¹⁷/² - 60q⁸ - 60q⁶ + ...) / 60q⁹
= -(2q⁷ + 1⅓q¹⁷/² - q⁸ - q⁶ + ...) / q⁹

Related

Derive state space matrix from ODEs in Matlab

I would like to create Matlab codes that automatically convert ODEs to state space matrix. All I need is the A matrix and B matrix.
I have partially finished it and this is what it looks like. After I run this code, I copy and paste the result V that is displayed on the command window into a spreadsheet and manually find the A and B matrices. Does anyone know how I can create a code so it automatically displays the A and B matrices on the command window? I have 14 states, 4 inputs, 4 disturbance inputs by the way.
Thanks.
syms x1(t) x2(t) x3(t) x4(t) x5(t) x6(t) x7(t) x8(t) x9(t) x10(t) x11(t) x12(t) x13(t) x14(t) t;
syms Ms Ir Ip m_1 m_2 m_3 m_4 b_1 b_2 b_3 b_4 ks1 ks2 ks3 ks4 a_dis b_dis Tf Tr ku1 ku2 ku3 ku4 zs1 zs2 zs3 zs4;
syms u1(t) u2(t) u3(t) u4(t) w1(t) w2(t) w3(t) w4(t);
zs1=-Tf*x1-a_dis*x2+x3;
zs2=+Tf*x1-a_dis*x2+x3;
zs3=-Tr*x1+b_dis*x2+x3;
zs4=+Tr*x1+b_dis*x2+x3;
Fd1=b_1*(diff(zs1)-x11);
Fd2=b_2*(diff(zs2)-x12);
Fd3=b_3*(diff(zs3)-x13);
Fd4=b_4*(diff(zs4)-x14);
Fs1=ks1*(zs1-x4);
Fs2=ks2*(zs2-x5);
Fs3=ks3*(zs3-x6);
Fs4=ks4*(zs4-x7);
Fu1= ku1*(x4-w1);
Fu2= ku2*(x5-w2);
Fu3= ku3*(x6-w3);
Fu4= ku4*(x7-w4);
DEq1 = diff(x1)== x8;
DEq2 = diff(x2)== x9;
DEq3 = diff(x3)== x10;
DEq4 = diff(x4)== x11;
DEq5 = diff(x5)== x12;
DEq6 = diff(x6)== x13;
DEq7 = diff(x7)== x14;
DEq8 = Ir*diff(x8)== -Fd1*Tf+Fd2*Tf-Fd3*Tr+Fd4*Tr-Fs1*Tf+Fs2*Tf-Fs3*Tr+Fs4*Tr+u1*Tf-u2*Tf+u3*Tr-u4*Tr;
DEq9 = Ip*diff(x9)== -Fd1*a_dis-Fd2*a_dis+Fd3*b_dis+Fd4*b_dis-Fs1*a_dis-Fs2*a_dis+Fs3*b_dis+Fs4*b_dis+u1*a_dis+u2*a_dis-u3*b_dis-u4*b_dis;
DEq10 = Ms*diff(x10)== -Fd1-Fd2-Fd3-Fd4-Fs1-Fs2-Fs3-Fs4+u1+u2+u3+u4;
DEq11 = m_1*diff(x11)== Fd1+Fs1-Fu1-u1;
DEq12 = m_2*diff(x12)== Fd2+Fs2-Fu2-u2;
DEq13 = m_3*diff(x13)== Fd3+Fs3-Fu3-u3;
DEq14 = m_4*diff(x14)== Fd4+Fs4-Fu4-u4;
[V,S] = odeToVectorField(DEq1,DEq2,DEq3,DEq4,DEq5,DEq6,DEq7,DEq8,DEq9,DEq10,DEq11,DEq12,DEq13,DEq14)
What gets displayed on the command window after running the above code is this.
V =
Y[8]
Y[9]
Y[10]
Y[11]
Y[12]
Y[13]
Y[14]
(Tf*u1(t) - Tf*u2(t) + Tr*u3(t) - Tr*u4(t) + Tf^2*b_1*Y[8] + Tf^2*b_2*Y[8] + Tr^2*b_3*Y[8] + Tr^2*b_4*Y[8] + Tf^2*ks1*Y[1] + Tf^2*ks2*Y[1] + Tr^2*ks3*Y[1] + Tr^2*ks4*Y[1] - Tf*b_1*Y[10] + Tf*b_1*Y[11] + Tf*b_2*Y[10] - Tf*b_2*Y[12] - Tr*b_3*Y[10] + Tr*b_4*Y[10] + Tr*b_3*Y[13] - Tr*b_4*Y[14] - Tf*ks1*Y[3] + Tf*ks1*Y[4] + Tf*ks2*Y[3] - Tf*ks2*Y[5] - Tr*ks3*Y[3] + Tr*ks4*Y[3] + Tr*ks3*Y[6] - Tr*ks4*Y[7] + Tf*a_dis*b_1*Y[9] - Tf*a_dis*b_2*Y[9] - Tr*b_3*b_dis*Y[9] + Tr*b_4*b_dis*Y[9] + Tf*a_dis*ks1*Y[2] - Tf*a_dis*ks2*Y[2] - Tr*b_dis*ks3*Y[2] + Tr*b_dis*ks4*Y[2])/Ir
(a_dis*u1(t) + a_dis*u2(t) - b_dis*u3(t) - b_dis*u4(t) + a_dis^2*b_1*Y[9] + a_dis^2*b_2*Y[9] + b_3*b_dis^2*Y[9] + b_4*b_dis^2*Y[9] + a_dis^2*ks1*Y[2] + a_dis^2*ks2*Y[2] + b_dis^2*ks3*Y[2] + b_dis^2*ks4*Y[2] - a_dis*b_1*Y[10] + a_dis*b_1*Y[11] - a_dis*b_2*Y[10] + a_dis*b_2*Y[12] + b_3*b_dis*Y[10] + b_4*b_dis*Y[10] - b_3*b_dis*Y[13] - b_4*b_dis*Y[14] - a_dis*ks1*Y[3] + a_dis*ks1*Y[4] - a_dis*ks2*Y[3] + a_dis*ks2*Y[5] + b_dis*ks3*Y[3] + b_dis*ks4*Y[3] - b_dis*ks3*Y[6] - b_dis*ks4*Y[7] + Tf*a_dis*b_1*Y[8] - Tf*a_dis*b_2*Y[8] - Tr*b_3*b_dis*Y[8] + Tr*b_4*b_dis*Y[8] + Tf*a_dis*ks1*Y[1] - Tf*a_dis*ks2*Y[1] - Tr*b_dis*ks3*Y[1] + Tr*b_dis*ks4*Y[1])/Ip
(u1(t) + u2(t) + u3(t) + u4(t) - b_1*Y[10] + b_1*Y[11] - b_2*Y[10] - b_3*Y[10] + b_2*Y[12] - b_4*Y[10] + b_3*Y[13] + b_4*Y[14] - ks1*Y[3] + ks1*Y[4] - ks2*Y[3] - ks3*Y[3] + ks2*Y[5] - ks4*Y[3] + ks3*Y[6] + ks4*Y[7] + Tf*b_1*Y[8] - Tf*b_2*Y[8] + Tr*b_3*Y[8] - Tr*b_4*Y[8] + Tf*ks1*Y[1] - Tf*ks2*Y[1] + Tr*ks3*Y[1] - Tr*ks4*Y[1] + a_dis*b_1*Y[9] + a_dis*b_2*Y[9] - b_3*b_dis*Y[9] - b_4*b_dis*Y[9] + a_dis*ks1*Y[2] + a_dis*ks2*Y[2] - b_dis*ks3*Y[2] - b_dis*ks4*Y[2])/Ms
-(u1(t) - b_1*Y[10] + b_1*Y[11] - ks1*Y[3] + ks1*Y[4] + ku1*Y[4] - ku1*w1(t) + Tf*b_1*Y[8] + Tf*ks1*Y[1] + a_dis*b_1*Y[9] + a_dis*ks1*Y[2])/m_1
-(u2(t) - b_2*Y[10] + b_2*Y[12] - ks2*Y[3] + ks2*Y[5] + ku2*Y[5] - ku2*w2(t) - Tf*b_2*Y[8] - Tf*ks2*Y[1] + a_dis*b_2*Y[9] + a_dis*ks2*Y[2])/m_2
-(u3(t) - b_3*Y[10] + b_3*Y[13] - ks3*Y[3] + ks3*Y[6] + ku3*Y[6] - ku3*w3(t) + Tr*b_3*Y[8] + Tr*ks3*Y[1] - b_3*b_dis*Y[9] - b_dis*ks3*Y[2])/m_3
(b_4*Y[10] - u4(t) - b_4*Y[14] + ks4*Y[3] - ks4*Y[7] - ku4*Y[7] + ku4*w4(t) + Tr*b_4*Y[8] + Tr*ks4*Y[1] + b_4*b_dis*Y[9] + b_dis*ks4*Y[2])/m_4
Using Matlab's subs function is the less ugly way I have found to do it. You'll get a 14x14 sym matrix:
A = repmat(V,1,14);
for j=1:14
y = double((1:14)==j);
A(:,j) = subs(A(:,j), 'Y[1]', y(1));
A(:,j) = subs(A(:,j), 'Y[2]', y(2));
A(:,j) = subs(A(:,j), 'Y[3]', y(3));
A(:,j) = subs(A(:,j), 'Y[4]', y(4));
A(:,j) = subs(A(:,j), 'Y[5]', y(5));
A(:,j) = subs(A(:,j), 'Y[6]', y(6));
A(:,j) = subs(A(:,j), 'Y[7]', y(7));
A(:,j) = subs(A(:,j), 'Y[8]', y(8));
A(:,j) = subs(A(:,j), 'Y[9]', y(9));
A(:,j) = subs(A(:,j), 'Y[10]', y(10));
A(:,j) = subs(A(:,j), 'Y[11]', y(11));
A(:,j) = subs(A(:,j), 'Y[12]', y(12));
A(:,j) = subs(A(:,j), 'Y[13]', y(13));
A(:,j) = subs(A(:,j), 'Y[14]', y(14));
end
You can get rid of all the lines by using eval function:
A = repmat(V,1,14);
for j=1:14
y = double((1:14)==j);
for k=1:14
A(:,j) = eval(sprintf('subs(A(:,j), ''Y[%d]'', y(%d));',k,k));
end
end
In the column j, we are replacing Y[i] by 1 if i==j else 0.
Test:
>> size(A)
ans =
14 14
>> A(:,10)
ans =
0
0
1
0
0
0
0
-(Tf*b_1 - Tf*b_2 + Tr*b_3 - Tr*b_4 - Tf*u1(t) + Tf*u2(t) - Tr*u3(t) + Tr*u4(t))/Ir
-(a_dis*b_1 + a_dis*b_2 - b_3*b_dis - b_4*b_dis - a_dis*u1(t) - a_dis*u2(t) + b_dis*u3(t) + b_dis*u4(t))/Ip
-(b_1 + b_2 + b_3 + b_4 - u1(t) - u2(t) - u3(t) - u4(t))/Ms
(b_1 - u1(t) + ku1*w1(t))/m_1
(b_2 - u2(t) + ku2*w2(t))/m_2
(b_3 - u3(t) + ku3*w3(t))/m_3
(b_4 - u4(t) + ku4*w4(t))/m_4

Matrix with symbolic math gives a symbolic answer, not a numeric one

Consider the following matrix
Ja(t1, t2, t3, t4, t5, t6) =
[ (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - sin(t1)/100 - (219*sin(t1)*sin(t2))/1000 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (219*cos(t1)*cos(t2))/1000 + (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (19*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, -(cos(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5, 0]
[ cos(t1)/100 + (219*cos(t1)*sin(t2))/1000 + (29*cos(t1)*sin(t3))/1000 - (21*cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (21*sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 + (sin(t5)*(cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (219*cos(t2)*sin(t1))/1000 - (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 + (21*cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (29*cos(t3)*sin(t1))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (19*sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (cos(t5)*(cos(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) + sin(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, 0]
[ 0, 0, (21*cos(t3)*cos(t4))/1000 - (29*cos(t3))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, (21*cos(t3)*cos(t4))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5, -(cos(t5)*(cos(t3)*sin(t4) + cos(t4)*sin(t3)))/5, 0]
The problem is that when I put my arguments, MATLAB doesn't calculate the matrix numerically, rather it leaves it symbolically.
This is the result:
Ja(q(1),q(2),q(3),q(4),q(5),q(6)) =
[ sin(63/100)/100 + (219*sin(528276371951843/1125899906842624)*sin(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, (219*cos(528276371951843/1125899906842624)*cos(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, - (19*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, -(cos(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5, 0]
[ cos(63/100)/100 + (219*sin(528276371951843/1125899906842624)*cos(63/100))/1000 + (29*cos(63/100)*sin(157/125))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (21*cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000 - (sin(59/125)*(cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))) + sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))))/5 - (21*sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000, (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (219*cos(528276371951843/1125899906842624)*sin(63/100))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, (19*sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 - (29*cos(157/125)*sin(63/100))/1000 + (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, -(cos(59/125)*(cos(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) + sin(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, 0]
[ 0, 0, (21*cos(157/125)*cos(157/250))/1000 - (29*cos(157/125))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, (21*cos(157/125)*cos(157/250))/1000 - (21*sin(157/125)*sin(157/250))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250) - sin(157/125)*sin(157/250)))/5, -(cos(59/125)*(cos(157/125)*sin(157/250) + cos(157/250)*sin(157/125)))/5, 0]
Is there a way I can get real numbers ?
Short answer: evaluate your symbolic expression numerically using eval or convert it to a specific type using one of these options, f.e. double or vpa.
Note that eval may be twice as slow as using double, but is sometimes slightly faster too
Explanation
The problem is that MATLAB does not evaluate your symbolic expression numerically, it only simplifies your expression mathematically.
Example:
syms x
my_function(x) = cos(x)
% exact algebraic solution is known:
my_function(0) % returns 1
my_function(pi) % returns -1
my_function(pi/2) % returns 0
my_function(pi/6) % returns 3^(1/2)/2
% result can only be numerically approximated:
my_function(3.1415) % returns cos(6283/2000)
my_function(1) % returns cos(1)
So, MATLAB is able to simplify the cos expression when the result is exactly known. In general, the result of cos can only be numerically evaluated, therefore MATLAB displays cos in it answer.
If you want a numerical result you can use one of the following options:
eval: evaluates your matrix numerically
double: converts to double precision
single: converts to single precision
int8 : converts to 8 bit integers (alternatives int16, int32, int64)
vpa: converts to variable-precision arithmetic, i.e. it allows you to specify the desired accuracy (number of significant digits) of the result
See Conversion Between Symbolic and Numeric for more information
Is using eval a good option?
As pointed out by Sardar Usama, using eval (to evaluate a string) is often bad practice:
Evading eval.
Alternatives to the eval Function
But, is this the same eval?
No, I don't think so. help sym/eval returns (in contrast to help eval):
eval Evaluate a symbolic expression.
eval(S) evaluates the character representation of the
symbolic expression S in the caller's workspace.
Also using the MATLAB debugger points out that it is a different function. However, the full explanation mentions that it evaluates the character representation of the expression, which can also be seen in the source code:
s = evalin('caller',vectorize(map2mat(char(x))));
So, it uses internally evalin, which is similar to eval, to evaluate a string. This may not be very efficient.
So, we should avoid sym/eval too?
Maybe not, also double uses eval internally to evaluate a string:
Xstr = mupadmex('symobj::double', S.s, 0);
X = eval(Xstr);
The difference is that sym/eval uses eval (evalin) for the original character representation, i.e. the whole expression, whereas double uses it to parse the final result, i.e. the numerically evaluated value.
Conclusion: for your example double seems to be the appropriate method as it is twice as fast as using eval. However, for the following example eval is somewhat faster (~15%):
my_function(x) = cos(x);
for i=2:100
my_function(x) = my_function(x) + cos(i*x);
end

Solving non linear equations in Matlab

consider the following equations :
eqn1 =
sin(t6)*(cos(t4)*sin(t1) + sin(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3))) + cos(t6)*(cos(t5)*(sin(t1)*sin(t4) - cos(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3))) - sin(t5)*(cos(t1)*cos(t2)*sin(t3) + cos(t1)*cos(t3)*sin(t2))) == 1
eqn2 =
cos(t6)*(cos(t4)*sin(t1) + sin(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3))) - sin(t6)*(cos(t5)*(sin(t1)*sin(t4) - cos(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3))) - sin(t5)*(cos(t1)*cos(t2)*sin(t3) + cos(t1)*cos(t3)*sin(t2))) == 0
eqn3 =
- sin(t5)*(sin(t1)*sin(t4) - cos(t4)*(cos(t1)*sin(t2)*sin(t3) - cos(t1)*cos(t2)*cos(t3))) - cos(t5)*(cos(t1)*cos(t2)*sin(t3) + cos(t1)*cos(t3)*sin(t2)) == 0
eqn4 =
cos(t1)/100 + (19*cos(t1)*cos(t2))/100 - (21*cos(t1)*sin(t2)*sin(t3))/1000 + (21*cos(t1)*cos(t2)*cos(t3))/1000 - (219*cos(t1)*cos(t2)*sin(t3))/1000 - (219*cos(t1)*cos(t3)*sin(t2))/1000 == 619/1000
eqn5 =
- sin(t6)*(cos(t1)*cos(t4) - sin(t4)*(sin(t1)*sin(t2)*sin(t3) - cos(t2)*cos(t3)*sin(t1))) - cos(t6)*(cos(t5)*(cos(t1)*sin(t4) + cos(t4)*(sin(t1)*sin(t2)*sin(t3) - cos(t2)*cos(t3)*sin(t1))) + sin(t5)*(cos(t2)*sin(t1)*sin(t3) + cos(t3)*sin(t1)*sin(t2))) == 0
eqn6 =
sin(t6)*(cos(t5)*(cos(t1)*sin(t4) + cos(t4)*(sin(t1)*sin(t2)*sin(t3) - cos(t2)*cos(t3)*sin(t1))) + sin(t5)*(cos(t2)*sin(t1)*sin(t3) + cos(t3)*sin(t1)*sin(t2))) - cos(t6)*(cos(t1)*cos(t4) - sin(t4)*(sin(t1)*sin(t2)*sin(t3) - cos(t2)*cos(t3)*sin(t1))) == 1
eqn7 =
sin(t5)*(cos(t1)*sin(t4) + cos(t4)*(sin(t1)*sin(t2)*sin(t3) - cos(t2)*cos(t3)*sin(t1))) - cos(t5)*(cos(t2)*sin(t1)*sin(t3) + cos(t3)*sin(t1)*sin(t2)) == 0
eqn8 =
sin(t1)/100 + (19*cos(t2)*sin(t1))/100 - (219*cos(t2)*sin(t1)*sin(t3))/1000 - (219*cos(t3)*sin(t1)*sin(t2))/1000 - (21*sin(t1)*sin(t2)*sin(t3))/1000 + (21*cos(t2)*cos(t3)*sin(t1))/1000 == 0
eqn9 =
sin(t4)*sin(t6)*(cos(t2)*sin(t3) + cos(t3)*sin(t2)) - cos(t6)*(sin(t5)*(cos(t2)*cos(t3) - sin(t2)*sin(t3)) + cos(t4)*cos(t5)*(cos(t2)*sin(t3) + cos(t3)*sin(t2))) == 0
eqn10 =
sin(t6)*(sin(t5)*(cos(t2)*cos(t3) - sin(t2)*sin(t3)) + cos(t4)*cos(t5)*(cos(t2)*sin(t3) + cos(t3)*sin(t2))) + cos(t6)*sin(t4)*(cos(t2)*sin(t3) + cos(t3)*sin(t2)) == 0
eqn11 =
cos(t4)*sin(t5)*(cos(t2)*sin(t3) + cos(t3)*sin(t2)) - cos(t5)*(cos(t2)*cos(t3) - sin(t2)*sin(t3)) == 1
eqn12 =
(219*sin(t2)*sin(t3))/1000 - (219*cos(t2)*cos(t3))/1000 - (21*cos(t2)*sin(t3))/1000 - (21*cos(t3)*sin(t2))/1000 - (19*sin(t2))/100 == 261/1000
is there a function that can solve these equations ?
fminsearch should do the trick.
https://de.mathworks.com/help/matlab/ref/fminsearch.html
Notice that solving non-linear equations may lead to local optima instead of global optima if the problem isn't convex. You can't really be sure to be globally optimal in many practical cases.
You would need to reformulate this to make it a minimization problem though.
f(x)=1 for example could be written as minimize abs(f(x)-1)

Bug in 2D Fourier Transform implementation

I am trying to implement a 2D DFT using a combination of 1D DFT in Matlab. When comparing my results against Matlab's inbuilt function (fft2) I realized that I have the following issues:
The sign of the imaginary part is being inverted. i.e + changed to - and vice versa.
The rows are being ordered in descending order except for the first one.
This image shows the comparison between the two results. The red numbers on the side are there to indicate the reordering issue.
My code is as follows:
x = imread('img.png');
x = double(x);
x = x(1:12,1:5)
% FFT
Xw = complex(zeros(size(x)));
for row = 1:size(x,1)
Xw(row,:) = fft(x(row,:));
end
for col = 1:size(x,2)
Xw(:,col) = fft(Xw(:,col)');
end
Could someone kindly indicate where my problem is please? Thanks
The ' operator is for the complex transpose, which means that the matrix is transposed and the conjugate of the values is taken. This means that the signs of the complex values are reversed. You don't notice this with real numbers because technically, the imaginary component is zero.
You want to use .' instead to preserve the sign of the imaginary component as performing the intermediate FFT per row will result in complex values. Using just ' will change the imaginary components of the intermediate results, which will give you the wrong results:
for col = 1:size(x,2)
Xw(:,col) = fft(Xw(:,col).');
end
BTW, as a minor note, there is no need to transpose the intermediate columns of your result at all. If you provide a single vector, fft will operate along the first non-singleton dimension, and so using a transpose is superfluous. It would actually help if you didn't transpose your result at all:
for col = 1:size(x,2)
Xw(:,col) = fft(Xw(:,col));
end
Here's an example. I've generated a random 10 x 10 matrix:
rng(123);
x = rand(10);
Using your code with correcting the transpose (and without the indexing at the beginning), we get:
>> Xw
Xw =
Columns 1 through 5
50.1429 + 0.0000i -0.4266 - 0.2624i 0.8803 + 0.9311i -0.0526 + 1.7067i 0.7187 + 0.7161i
0.5066 - 2.4421i 2.7551 - 1.7421i -1.9994 + 0.6052i 0.2891 + 3.4182i 0.5300 + 2.4417i
0.1956 + 0.1790i 4.1461 + 1.9648i -1.3781 - 1.0303i -0.6872 - 1.0103i -1.2184 - 0.5783i
-0.3645 - 1.6193i -1.8470 - 1.3445i 4.1555 + 0.7432i 2.3707 + 3.8265i -1.9526 + 1.9464i
-3.1136 - 0.3704i 2.4132 - 1.0795i -0.2255 + 1.3062i 0.8436 - 0.5157i -0.3493 - 0.9994i
-1.5962 + 0.0000i -0.3780 - 1.4055i 1.6242 - 0.4842i 0.4457 + 0.4718i -0.1794 - 2.0014i
-3.1136 + 0.3704i 0.0134 + 0.1267i 1.0630 + 1.4563i -0.8864 - 0.3174i -0.5720 + 1.3041i
-0.3645 + 1.6193i 0.7028 + 0.2797i 0.1064 + 2.0705i 2.1644 + 0.1685i 0.3095 + 0.7426i
0.1956 - 0.1790i 2.3511 + 2.1440i 0.7301 - 0.8264i -1.1974 - 0.3794i -2.4981 + 1.2363i
0.5066 + 2.4421i -3.5897 + 0.7444i 1.2191 - 3.6386i -2.9659 - 1.6626i -2.0339 + 0.0880i
Columns 6 through 10
2.0373 + 0.0000i 0.7187 - 0.7161i -0.0526 - 1.7067i 0.8803 - 0.9311i -0.4266 + 0.2624i
-1.8782 - 0.9047i -2.0339 - 0.0880i -2.9659 + 1.6626i 1.2191 + 3.6386i -3.5897 - 0.7444i
2.3752 + 1.8811i -2.4981 - 1.2363i -1.1974 + 0.3794i 0.7301 + 0.8264i 2.3511 - 2.1440i
4.5213 + 0.9237i 0.3095 - 0.7426i 2.1644 - 0.1685i 0.1064 - 2.0705i 0.7028 - 0.2797i
-1.2259 + 2.1690i -0.5720 - 1.3041i -0.8864 + 0.3174i 1.0630 - 1.4563i 0.0134 - 0.1267i
6.2411 + 0.0000i -0.1794 + 2.0014i 0.4457 - 0.4718i 1.6242 + 0.4842i -0.3780 + 1.4055i
-1.2259 - 2.1690i -0.3493 + 0.9994i 0.8436 + 0.5157i -0.2255 - 1.3062i 2.4132 + 1.0795i
4.5213 - 0.9237i -1.9526 - 1.9464i 2.3707 - 3.8265i 4.1555 - 0.7432i -1.8470 + 1.3445i
2.3752 - 1.8811i -1.2184 + 0.5783i -0.6872 + 1.0103i -1.3781 + 1.0303i 4.1461 - 1.9648i
-1.8782 + 0.9047i 0.5300 - 2.4417i 0.2891 - 3.4182i -1.9994 - 0.6052i 2.7551 + 1.7421i
I've also verified that this works without transposing. You'll get the same thing as if you were to perform .'. Now, using fft2, we get:
>> Xw2 = fft2(x)
Xw2 =
Columns 1 through 5
50.1429 + 0.0000i -0.4266 - 0.2624i 0.8803 + 0.9311i -0.0526 + 1.7067i 0.7187 + 0.7161i
0.5066 - 2.4421i 2.7551 - 1.7421i -1.9994 + 0.6052i 0.2891 + 3.4182i 0.5300 + 2.4417i
0.1956 + 0.1790i 4.1461 + 1.9648i -1.3781 - 1.0303i -0.6872 - 1.0103i -1.2184 - 0.5783i
-0.3645 - 1.6193i -1.8470 - 1.3445i 4.1555 + 0.7432i 2.3707 + 3.8265i -1.9526 + 1.9464i
-3.1136 - 0.3704i 2.4132 - 1.0795i -0.2255 + 1.3062i 0.8436 - 0.5157i -0.3493 - 0.9994i
-1.5962 + 0.0000i -0.3780 - 1.4055i 1.6242 - 0.4842i 0.4457 + 0.4718i -0.1794 - 2.0014i
-3.1136 + 0.3704i 0.0134 + 0.1267i 1.0630 + 1.4563i -0.8864 - 0.3174i -0.5720 + 1.3041i
-0.3645 + 1.6193i 0.7028 + 0.2797i 0.1064 + 2.0705i 2.1644 + 0.1685i 0.3095 + 0.7426i
0.1956 - 0.1790i 2.3511 + 2.1440i 0.7301 - 0.8264i -1.1974 - 0.3794i -2.4981 + 1.2363i
0.5066 + 2.4421i -3.5897 + 0.7444i 1.2191 - 3.6386i -2.9659 - 1.6626i -2.0339 + 0.0880i
Columns 6 through 10
2.0373 + 0.0000i 0.7187 - 0.7161i -0.0526 - 1.7067i 0.8803 - 0.9311i -0.4266 + 0.2624i
-1.8782 - 0.9047i -2.0339 - 0.0880i -2.9659 + 1.6626i 1.2191 + 3.6386i -3.5897 - 0.7444i
2.3752 + 1.8811i -2.4981 - 1.2363i -1.1974 + 0.3794i 0.7301 + 0.8264i 2.3511 - 2.1440i
4.5213 + 0.9237i 0.3095 - 0.7426i 2.1644 - 0.1685i 0.1064 - 2.0705i 0.7028 - 0.2797i
-1.2259 + 2.1690i -0.5720 - 1.3041i -0.8864 + 0.3174i 1.0630 - 1.4563i 0.0134 - 0.1267i
6.2411 + 0.0000i -0.1794 + 2.0014i 0.4457 - 0.4718i 1.6242 + 0.4842i -0.3780 + 1.4055i
-1.2259 - 2.1690i -0.3493 + 0.9994i 0.8436 + 0.5157i -0.2255 - 1.3062i 2.4132 + 1.0795i
4.5213 - 0.9237i -1.9526 - 1.9464i 2.3707 - 3.8265i 4.1555 - 0.7432i -1.8470 + 1.3445i
2.3752 - 1.8811i -1.2184 + 0.5783i -0.6872 + 1.0103i -1.3781 + 1.0303i 4.1461 - 1.9648i
-1.8782 + 0.9047i 0.5300 - 2.4417i 0.2891 - 3.4182i -1.9994 - 0.6052i 2.7551 + 1.7421i
We can show these two are equal by ensuring that all of the element-wise differences between the two matrices is less than a small threshold... say 1e-10:
>> all(abs(Xw(:)-Xw2(:)) <= 1e-10)
ans =
1
Take away message
Never use ' for the transpose.... ever. Luis Mendo is a strong advocate of this very fact.

How to figure out eigenvalues of a matrix in matlab when all entries of matrix are variables?

I have a matrix with a bunch of unknown constants such as the one below:
a*b -c -d 0
-c e -a -b-d
-d -a d -e
0 -b-d -e a
As you may realize it is symmetric about the diagonal and therefore, the diagonal values are all positive. All constants are greater than 0.
I would like to solve this for the eigenvalues in matlab. How would I go about doing this? I do not know the values a,b,c,d, and e. I would like to do something like this:
d = eig(#getMatrix)
but the eig function does not accept function handles.
No problem in MATLAB.
>> syms a b c d e
>> M = [a*b -c -d 0
-c e -a -b-d
-d -a d -e
0 -b-d -e a];
>> eig(M)
ans =
a/4 + d/4 + e/4 + (a*b)/4 - ((51*a*d^3)/16 - (117*a^4*b)/16 + (27*a^3*d)/16 + (27*a*e^3)/16 + (57*b*d^3)/2 + (27*a^3*e)/16 + (27*d*e^3)/16 + (51*d^3*e)/16 + 6*((4*(2*b*d - (a*e)/4 - (a*d)/4 - (d*e)/4 - (a^2*b)/4 + (11*a^2)/8 + b^2 + c^2 + (19*d^2)/8 + (11*e^2)/8 + (3*a^2*b^2)/8 - (a*b*d)/4 - (a*b*e)/4)*((17*a*d^3)/64 - (39*a^4*b)/64 + (9*a^3*d)/64 + (9*a*e^3)/64 + (19*b*d^3)/8 + (9*a^3*e)/64 + (9*d*e^3)/64 + (17*d^3*e)/64 + (45*a^4)/256 + (285*d^4)/256 + (45*e^4)/256 - (a^2*b^2)/16 + (a^2*b^3)/8 + (3*a^2*b^4)/16 + (31*a^4*b^2)/128 + (a^4*b^3)/64 - (3*a^4*b^4)/256 + (3*a^2*c^2)/16 + (15*a^2*d^2)/128 - (9*a^2*e^2)/128 + (19*b^2*d^2)/16 - (b^2*e^2)/16 + (3*c^2*d^2)/16 + (15*c^2*e^2)/16 +
...
(a*b*c^2*e)/8 + (3*a*b*d*e^2)/64 + (11*a*b*d^2*e)/64 + (a*b^2*d*e)/4 - (33*a^2*b*d*e)/32 - (5*a^2*b^2*d*e)/64 + (a*b*d*e)/4 + (a*c*d*e)/2 - 2*b*c*d*e) - 256*((17*a*d^3)/64 - (39*a^4*b)/64 + (9*a^3*d)/64 + (9*a*e^3)/64 + (19*b*d^3)/8 + (9*a^3*e)/64 + (9*d*e^3)/64 + (17*d^3*e)/64 + (45*a^4)/256 + (285*d^4)/256 + (45*e^4)/256 - (a^2*b^2)/16 + (a^2*b^3)/8 + (3*a^2*b^4)/16 + (31*a^4*b^2)/128 + (a^4*b^3)/64 - (3*a^4*b^4)/256 + (3*a^2*c^2)/16 + (15*a^2*d^2)/128 - (9*a^2*e^2)/128 + (19*b^2*d^2)/16 - (b^2*e^2)/16 + (3*c^2*d^2)/16 + (15*c^2*e^2)/16 + (15*d^2*e^2)/1...
Output truncated. Text exceeds maximum line length of 25,000 characters for Command Window display.
I deleted a lot there. Admittedly, its rather messy and lengthy, but can you really expect better?
Edit: I should comment that such a long extended formula may be dangerous in terms of computational accuracy. I've seen people blindly use such a mess of an expression, evaluating it in Fortran or MATLAB. They think that because it is "symbolic" that it is also exact. This is a total fallacy when numerical computations are done.
There may well be immense subtractive cancellation in those terms, with huge positive and negative terms almost canceling each other out, leaving a tiny result that is essentially worthless because of the limited dynamic range of floating point computations. BEWARE. At the very least, compare single and double precision computations done with the same expression. If they differ by any significant amount, try an extended precision version to verify there is not a problem for the doubles. If you have not tested such an expression and verified it extensively, don't trust it.