Derive state space matrix from ODEs in Matlab - 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

Related

How to solve an equation for a variable in MATLAB

I want to solve this equation in MATLAB. I know via numerical solver that some of the roots are imaginary and others real. Using solve, MATLAB spits out a list of conditions under which the solution is valid.
I just want MATLAB to solve this with an analytical solution (not numerically, I can do this in Mathematica). Is there a better solution, or any other package that I can use?
syms f2 ep1 ep2 a1 a2 fp1 fp2 h e1 m1 mp R f r t1 f1
eqn = (((ep2*fp1 - ep1*fp2)*mp*(e1*m1*(ep2*f2*fp1*(fp2 - a2*mp) +...
ep1*fp2*(-(f1*fp2) + a2*f2*mp))*(h + R) + a2*f2*(-(ep2*f*f2*fp1*R)+...
ep1*f*f1*fp2*R - ...
ep1*ep2*(f1 - f2)*mp*r*(h + R)))* (a1*ep1*ep2*f1*(f1 - f2)...
*mp*(ep2*f*f2*fp1*R - ep1*f*f1*fp2*R + ep1*ep2*(f1 - f2)*mp*r*(h + R)) +...
e1*(ep2^2*f*f2^2*fp1^2*R*t1 + ...
ep1^2*f1*fp2*(f*f1*fp2*R*t1 - ep2*(f1 - f2)*mp*(h + R)*(-(fp1*m1) + ...
a1*m1*mp + r*t1)) + ...
ep1*ep2*fp1*(-2*f*f1*f2*fp2*R*t1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fp2*h^2*m1^2*mp + 2*a1*e1^2*ep2^2*f1*f2*h^2*m1*mp*r*t1 + 4*a1*e1*ep1*ep2^2*f1^2*f2*h^2*m1*mp*r - 4*a1*e1*ep1*ep2^2*f1*f2^2*h^2*m1*mp*r + e1^2*ep1^2*f1^2*fp2^2*h^2*m1^2 - 2*e1^2*ep1*ep2*f1*f2*fp2*h^2*m1*r*t1 + e1^2*ep2^2*f2^2*h^2*r^2*t1^2)^(1/2) - R*e1*ep1^2*f1^2*fp2*m1*mp - e1*ep1^2*f1^2*fp2*h*m1*mp + e1*ep1*ep2*f2^2*h*mp*r*t1 - R*a1*e1*ep1*ep2*f1^2*m1*mp^2 - a1*e1*ep1*ep2*f1^2*h*m1*mp^2 + 2*R*e1*ep1*f*f1*f2*fp2*t1 + R*a1*ep1*ep2*f*f1*f2^2*mp - R*a1*ep1*ep2*f*f1^2*f2*mp + R*e1\\\r\n*ep1^2*f1*f2*fp2*m1*mp + R*e1*ep1*ep2*f2^2*mp*r*t1 + e1*ep1^2*f1*f2*fp2*h*m1*mp - R*e1*ep1*ep2*f1*f2*mp*r*t1 - e1*ep1*ep2*f1*f2*h*mp*r*t1 + R*a1*e1*ep1*ep2*f1*f2*m1*mp^2 + a1*e1*ep1*ep2*f1*f2*h*m1*mp^2)/(2*e1*ep2*f2*(R*f*f2*t1 - R*ep1*f1*m1*mp + R*ep1*f2*m1*mp - ep1*f1*h*m1*mp + ep1*f2*h*m1*mp))

Why do Octave and Matlab produce a new set of eigenvectors every time?

I have observed that both Octave and Matlab return different eigenvectors when invoking eigs(X,2) several times. The eigenvalues stay the same but the eigenvectors change despite there being no change in X. I suspect this is because of random initialization somewhere inside the code for eigs(). Any clues where the randomness comes from?
Is there a way to remove the randomness (for example user specified seed for all random number generation)?
I have manually ported some code from Matlab to Armadillo C++ library. The ported code has a bug somewhere but I am finding it impossible to hunt it down because of the eigenvectors changing from invocation to invocation in Matlab/Octave.
For example when X is:
[ 1.00000 + 0.00000i, -0.64125 + 0.00018i, 0.15282 + 0.04350i, 0.01890 - 0.07992i ...
-0.01980 + 0.02993i, -0.01754 + 0.02204i, -0.01306 + 0.03970i, 0.06067 - 0.08188i ...
-0.04754 - 0.00056i, 0.03681 + 0.02123i;
-0.64125 - 0.00018i, 1.00000 + 0.00000i, -0.67099 - 0.00014i, 0.14904 - 0.00153i ...
-0.01074 + 0.01916i, 0.13448 - 0.06437i, -0.16825 + 0.06454i, 0.06730 - 0.03552i ...
0.04261 + 0.02680i, -0.07457 - 0.01962i;
0.15282 - 0.04350i, -0.67099 + 0.00014i, 1.00000 + 0.00000i, -0.65949 - 0.00159i ...
0.20526 - 0.00742i, -0.17609 + 0.06834i, 0.30492 - 0.08507i, -0.18454 + 0.01918i ...
-0.00090 + 0.01579i, 0.01740 + 0.04236i;
0.01890 + 0.07992i, 0.14904 + 0.00153i, -0.65949 + 0.00159i, 1.00000 + 0.00000i ...
-0.70059 + 0.00211i, 0.25861 - 0.02341i, -0.17782 + 0.01540i, 0.15505 - 0.01596i ...
-0.03466 + 0.04638i, 0.02013 - 0.05730i;
-0.01980 - 0.02993i, -0.01074 - 0.01916i, 0.20526 + 0.00742i, -0.70059 - 0.00211i ...
1.00000 + 0.00000i, -0.66423 - 0.00025i, 0.14354 + 0.00726i, -0.03537 + 0.05243i ...
0.05091 - 0.09231i, -0.06596 + 0.01689i;
-0.01754 - 0.02204i, 0.13448 + 0.06437i, -0.17609 - 0.06834i, 0.25861 + 0.02341i ...
-0.66423 + 0.00025i, 1.00000 + 0.00000i, -0.62010 + 0.00556i, 0.17786 - 0.03333i ...
-0.08309 + 0.02039i, 0.13213 + 0.04940i;
-0.01306 - 0.03970i, -0.16825 - 0.06454i, 0.30492 + 0.08507i, -0.17782 - 0.01540i ...
0.14354 - 0.00726i, -0.62010 - 0.00556i, 1.00000 + 0.00000i, -0.69907 + 0.00052i ...
0.21059 + 0.01909i, -0.04506 - 0.03867i;
0.06067 + 0.08188i, 0.06730 + 0.03552i, -0.18454 - 0.01918i, 0.15505 + 0.01596i ...
-0.03537 - 0.05243i, 0.17786 + 0.03333i, -0.69907 - 0.00052i, 1.00000 + 0.00000i ...
-0.64091 - 0.00089i, 0.11982 - 0.00182i;
-0.04754 + 0.00056i, 0.04261 - 0.02680i, -0.00090 - 0.01579i, -0.03466 - 0.04638i ...
0.05091 + 0.09231i, -0.08309 - 0.02039i, 0.21059 - 0.01909i, -0.64091 + 0.00089i ...
1.00000 + 0.00000i, -0.66278 + 0.00015i;
0.03681 - 0.02123i, -0.07457 + 0.01962i, 0.01740 - 0.04236i, 0.02013 + 0.05730i ...
-0.06596 - 0.01689i, 0.13213 - 0.04940i, -0.04506 + 0.03867i, 0.11982 + 0.00182i ...
-0.66278 - 0.00015i, 1.00000 + 0.00000i ];
Two successive calls to eigs(X,2) returns the following results:
e =
-0.338703 + 0.107398i 0.084395 - 0.026833i
0.396747 - 0.137376i -0.186949 + 0.174112i
-0.293466 + 0.143536i 0.295215 - 0.247898i
0.164756 - 0.083375i -0.307448 + 0.238361i
-0.071050 + 0.009354i 0.289069 - 0.204641i
-0.034409 - 0.001546i -0.346065 + 0.187859i
0.170741 + 0.026855i 0.365437 - 0.175989i
-0.357124 + 0.014893i -0.308248 + 0.132656i
0.483759 - 0.103465i 0.215436 - 0.066429i
-0.366097 + 0.118043i -0.129626 + 0.041190i
d =
Diagonal Matrix
2.0926 - 0.0000i 0
0 3.0116 + 0.0000i
e =
-0.273138 - 0.227266i -0.059841 + 0.065281i
0.329691 + 0.259970i 0.075477 - 0.244066i
-0.279165 - 0.169681i -0.132650 + 0.361952i
0.159079 + 0.093755i 0.148006 - 0.359770i
-0.046203 - 0.054780i -0.148851 + 0.321376i
-0.017259 - 0.029808i 0.206637 - 0.335191i
0.069489 + 0.158256i -0.229348 + 0.334538i
-0.205182 - 0.292677i 0.201316 - 0.268489i
0.348073 + 0.351529i -0.153785 + 0.164851i
-0.296879 - 0.244590i 0.091923 - 0.100248i
d =
Diagonal Matrix
2.0926 - 0.0000i 0
0 3.0116 - 0.0000i
Edit: I have added example data and changed the question too. The Matlab function in question is eigs() not eig() as I had earlier used incorrectly.

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.

Solve constrained equation with 2 variables

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⁹