I want to find the five unknown of a system of five nonlinear equations. Thank you !
Show older comments
Here the code:
clc;
clear;
close all;
clearvars;
syms ET i1 i2 i3 i4 i5 x1 x2 x3 x4 x5 % constants defined below
syms k1 k2 k3 k4 k7 % unknown variables
ET=1
i1=0.2
i2=0.32
i3=0.43
i4=0.3
i5=0.15
x1=100
x2=200
x3=400
x4=600
x5=800
eq1 = 0==-i1+k7*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1))*(ET-(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1)))/(1+2*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1)));
eq2 = 0==-i2+k7*k3/(k4+k7)*(-(k2+k1*x2-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x2-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x2+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x2))*(ET-(-(k2+k1*x2-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x2-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x2+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x2)))/(1+2*k3/(k4+k7)*(-(k2+k1*x2-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x2-k7*k3/(k4+k7)*ET)^2+4*k2*ET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((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5))*(ET-(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)))/(1+2*k3/(k4+k7)*(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)));
eq1=subs(eq1); eq2=subs(eq2); eq3=subs(eq3); eq4=subs(eq4); eq5=subs(eq5);
[k1,k2,k3,k4,k7]=fsolve( [eq1, eq2, eq3, eq4, eq5], [k1,k2,k3,k4,k7])
Answers (1)
function main
x0 = rand(5,1);
x = fsolve(@fun,x0)
end
function res = fun(x)
k1 = x(1);
k2 = x(2);
k3 = x(3);
k4 = x(4);
k7 = x(5);
ET=1;
i1=0.2;
i2=0.32;
i3=0.43;
i4=0.3;
i5=0.15;
x1=100;
x2=200;
x3=400;
x4=600;
x5=800;
res(1) = -i1+k7*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3 /(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1))*(ET-(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1)))/(1+2*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k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*x4-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x4-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x4+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x4))*(ET-(-(k2+k1*x4-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x4-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x4+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x4)))/(1+2*k3/(k4+k7)*(-(k2+k1*x4-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x4-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x4+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x4)));
res(5) = -i5+k7*k3/(k4+k7)*(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5))*(ET-(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)))/(1+2*k3/(k4+k7)*(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)));
end
10 Comments
Thierry Rebetez
on 13 Jan 2022
Torsten
on 13 Jan 2022
What do you get when you include
res = fun(x)
after
x = fsolve(@fun,x0)
?
If all elements of res are small, then fsolve succeeded to find a solution.
Take a look here:
Thierry Rebetez
on 14 Jan 2022
Since you take sqrt's in your expressions, it's not guaranteed that the solution remains real-valued.
If it remains real-valued, you can get positive values by using this code:
function main
x0 = rand(5,1);
x = fsolve(@fun,x0);
k1 = x(1)^2
k2 = x(2)^2
k3 = x(3)^2
k4 = x(4)^2
k7 = x(5)^2
end
function res = fun(x)
k1 = x(1)^2;
k2 = x(2)^2;
k3 = x(3)^2;
k4 = x(4)^2;
k7 = x(5)^2;
ET=1;
i1=0.2;
i2=0.32;
i3=0.43;
i4=0.3;
i5=0.15;
x1=100;
x2=200;
x3=400;
x4=600;
x5=800;
res(1) = -i1+k7*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3 /(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1))*(ET-(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1)))/(1+2*k3/(k4+k7)*(-(k2+k1*x1-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x1-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x1+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x1)));
res(2) = -i2+k7*k3/(k4+k7)*(-(k2+k1*x2-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x2-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x2+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x2))*(ET-(-(k2+k1*x2-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x2-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x2+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x2)))/(1+2*k3/(k4+k7)*(-(k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
res(5) = -i5+k7*k3/(k4+k7)*(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5))*(ET-(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)))/(1+2*k3/(k4+k7)*(-(k2+k1*x5-k7*k3/(k4+k7)*ET)+sqrt((k2+k1*x5-k7*k3/(k4+k7)*ET)^2+4*k2*ET*(2*k1*k3/(k4+k7)*x5+k7*k3/(k4+k7))))/(2*k3/(k4+k7)*(k7+2*k1*x5)));
end
fsolve tries to adjust k1,k2,k3,k4 and k7 such that res(1),res(2),res(3),res(4) and res(5) become 0.
So res(i) = ... this is the correct syntax for fsolve.
Maybe you should read the fsolve-documentation, at least what is written under
Thierry Rebetez
on 14 Jan 2022
Now I tried to put back the constants k's solved by Matlab in the equations, but the result is wrong. Why?
It's nearly impossible that the initial values are solutions to the problem. So it is not surprising that the x values don't solve your equations. What do you get back with the command res = fun(x) ? Is the res-vector small ?
Another question: why, by each new resolution of the k's, Matlab get another solution ? Should it not be only one solution ?
A polynomial of degree n can have n solutions. Can you exclude such thing for the case you are working on ?
Thierry Rebetez
on 15 Jan 2022
Torsten
on 15 Jan 2022
Whatever value for x0 I choose, my solver gives the warning that the Jacobian matrix in the starting point is singular. Are you sure your equations are correct ? Where do they stem from ?
Thierry Rebetez
on 15 Jan 2022
Thierry Rebetez
on 16 Jan 2022
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!