# solution of five equations in five unknowns

Discussion in 'MATLAB' started by Mostafa M Naseri, Aug 9, 2011.

1. ### Mostafa M NaseriGuest

I use fsolve to solve system of five nonlinear equations, F, for five
unknowns, x,
and my unknown are positive (and in one paper there is the answer: x(1)=15.2864,x(2)=98.1268,x(3)=1.1625,x(4)=.1578,x(5)=1.0148
but i can get these answers)
how can i solve my problem?
the code:
function F=myfunsol(x)
w=1;
fim=.44*pi;w=1;
A=20;
B=20;
wt=10;
ws=.01;
h=.001;
f=20*log10((abs((11*(x(1) +x(2)/(1i*w)^x(4) + x(3)*(1i*w)^x(5)))/(20*(62*1i*w + 1)))))

ceq1=angle((11*(x(1)+x(2)/(1i*w)^x(4) + x(3)*(1i*w)^x(5)))/(20*(62*1i*w + 1)))+pi-fim

ceq2=(angle((11*(x(1)+x(2)/(1i*(w+h))^x(4) +x(3)*(1i*(w+h))^x(5)))/(20*(62*1i*(w+h) +1)))-angle((11*(x(1)+x(2)/(1i*w)^x(4) + x(3)*(1i*w)^x(5)))/(20*(62*1i*w + 1))))/h

ceq3=20*log10((11*((-2*sin(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*sin(1.570796327*x(5) *1)+2*cos(1.570796327*x(4) *1)*x(1)*(wt^2)^(.5000000000*x(4))*x(2)+x(3)^2*(wt^2)^(x(5)+x(4))+2.*x(1)*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+x(1)^2*(wt^2)^x(4)+2*cos(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*cos(1.570796327*x(5) *1)+x(2)^2)/(121*x(2)^2+242*cos(1.570796327*x(4) *1)*x(1)*(wt^2)^(.5000000000*x(4))*x(2)-27280*sin(1.570796327*x(4) *1)*(wt^2)^(.5000000000*x(4))*wt*x(2)+440*(wt^2)^x(4)*x(1)+27280*(wt^2)^(x(4)+.5000000000*x(5))*wt*x(3)*sin(1.570796327*x(5) *1)+400*(wt^2)^x(4)+440*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+440*x(2)*(wt^2)^(.5000000000*x(4))*cos(1.570796327*x(4) *1)-242*sin(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*sin(1.570796327*x(5)
*1)+242*cos(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*cos(1.570796327*x(5) *1)+121*x(3)^2*(wt^2)^(x(5)+x(4))+242*x(1)*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+1537600*(wt^2)^x(4)*wt^2+121*x(1)^2*(wt^2)^x(4)))^(1/2)))+A

ceq4=20*log10(( 20*(1+3844*ws^2)^(1/2)/(121*(ws^2)^(-1*x(4))*x(2)^2+242*(ws^2)^(-.5*x(4))*cos(1.570796327*x(4) )*x(1)*x(2)-27280*(ws^2)^(- 0.5*x(4))*sin(1.570796327*x(4) )*ws*x(2)+440*x(1)+27280*(ws^2)^( 0.5*x(5))*ws*x(3)*sin(1.570796327*x(5) )+400+440*(ws^2)^( 0.5*x(5))*x(3)*cos(1.570796327*x(5) )+440*(ws^2)^(- 0.5*x(4))*x(2)*cos(1.570796327*x(4) )-242*(ws^2)^(- 0.5*x(4)+ 0.5*x(5))*sin(1.570796327*x(4) )*x(2)*x(3)*sin(1.570796327*x(5) )+242*(ws^2)^(- 0.5*x(4)+ 0.5*x(5))*cos(1.570796327*x(4) )*x(2)*x(3)*cos(1.570796327*x(5) )+121*(ws^2)^x(5)*x(3)^2+242*(ws^2)^( 0.5*x(5))*x(1)*x(3)*cos(1.570796327*x(5) )+1537600*ws^2+121*x(1)^2)^(1/2)))+B

F=[f ceq1 ceq2 ceq3 ceq4];

clear
clc
x0=[16 ;90 ;1 ;.1; 2];
options = optimset('Display','iter');
[x,fval] = fsolve(@myfunsol,x0,options)

is there another way to obtain desired x?

Mostafa M Naseri, Aug 9, 2011

2. ### Mostafa M NaseriGuest

I forget to say that my equations have complex number so i cant use fmincon

Mostafa M Naseri, Aug 9, 2011

3. ### Bruno LuongGuest

what about separate the real and image parts?

Bruno

Bruno Luong, Aug 9, 2011
4. ### Mostafa M NaseriGuest

It is not poosible i have fractional order for i ( for exampe i^1.4) and it is more complicated .

Mostafa M Naseri, Aug 9, 2011
5. ### Mostafa M NaseriGuest

And

i substitue x=x^2 so i have positive solution but it was not my desired solution that mentioned in the paper .i need minimum of solution of them i think but fmincon can not sove complex numbers
how can i do with my 5 equations 5 unknown and desired solution?

Mostafa M Naseri, Aug 9, 2011
6. ### Bruno LuongGuest

Huh? It has nothing to do with fractional order or not. Just compute in complex then separate real and imag at the end.

Bruno

Bruno Luong, Aug 9, 2011
7. ### Mostafa M NaseriGuest

How can i do that(compute in complex then separate real and imag at the end.) for

20*log10((11*((-2*sin(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*sin(1.570796327*x(5) *1)+2*cos(1.570796327*x(4) *1)*x(1)*(wt^2)^(.5000000000*x(4))*x(2)+x(3)^2*(wt^2)^(x(5)+x(4))+2.*x(1)*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+x(1)^2*(wt^2)^x(4)+2*cos(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*cos(1.570796327*x(5) *1)+x(2)^2)/(121*x(2)^2+242*cos(1.570796327*x(4) *1)*x(1)*(wt^2)^(.5000000000*x(4))*x(2)-27280*sin(1.570796327*x(4) *1)*(wt^2)^(.5000000000*x(4))*wt*x(2)+440*(wt^2)^x(4)*x(1)+27280*(wt^2)^(x(4)+.5000000000*x(5))*wt*x(3)*sin(1.570796327*x(5) *1)+400*(wt^2)^x(4)+440*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+440*x(2)*(wt^2)^(.5000000000*x(4))*cos(1.570796327*x(4) *1)-242*sin(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*sin(1.570796327*x(5)
*1)+242*cos(1.570796327*x(4) *1)*x(2)*x(3)*(wt^2)^(.5000000000*x(5)+.5000000000*x(4))*cos(1.570796327*x(5) *1)+121*x(3)^2*(wt^2)^(x(5)+x(4))+242*x(1)*(wt^2)^(x(4)+.5000000000*x(5))*x(3)*cos(1.570796327*x(5) *1)+1537600*(wt^2)^x(4)*wt^2+121*x(1)^2*(wt^2)^x(4)))^(1/2)))+A

or

20*log10((abs((11*(x(1) +x(2)/(1i*w)^x(4) + x(3)*(1i*w)^x(5)))/(20*(62*1i*w + 1)))))

Mostafa M Naseri, Aug 9, 2011
8. ### Bruno LuongGuest

help real
help imag

Note that your function looks ugly, and surely not easy to maintain. You should improve the coding.

Bruno

Bruno Luong, Aug 9, 2011
9. ### Mostafa M NaseriGuest

Ok I separated in image and real but how can it help me to find my desired solution(my unknown are positive (and in one paper there is the answer: x(1)=15.2864,x(2)=98.1268,x(3)=1.1625,x(4)=.1578,x(5)=1.0148) with fsolve?(fsolve didnt give me these answer)
And
fmincon dont work with complex so if i separated my functions and ceq 's ,i have complex yet!

Mostafa M Naseri, Aug 9, 2011
10. ### Steven_LordGuest

To solve the system of N equations:

z(x) = 0

where z(x) is a complex-valued function, you can solve the system of 2*N
equations:

real(z(x)) = 0
imag(z(x)) = 0
Reading back into your equations, I see you're making use of plenty of trig
functions.

If FSOLVE were to give you the answer x = pi for a solution of sin(x) = 0
instead of your expected x = 0, would you consider that wrong? I wouldn't,
because in MATLAB sin(pi) is extremely close to 0. [It's not exactly 0
because PI is not exactly the mathematical pi.]

If your system has multiple solutions, FSOLVE is not guaranteed to give the
one you have in mind. As long as z(x) returns something "small" as the
residual, FSOLVE has returned A correct result.
See above, though you'd need to combine the 2*N results into one value,
perhaps by taking the sum of squares.

Steven_Lord, Aug 9, 2011