4 non linear simultaneous equations

1 view (last 30 days)
Misha Patel
Misha Patel on 16 Sep 2017
Commented: John BG on 17 Sep 2017
I have the following 4 questions and want to solve them using fsolve
F(1)=x(1)+x(3)-12.54;
F(2)=x(2)+x(4)-12.245;
F(3)= 2*x(1)+n(2)+n(3)-(1493/60);
F(4) =(x(3)*x(2))/(x(1)*x(4))-1000;

Accepted Answer

John BG
John BG on 17 Sep 2017
Edited: John BG on 17 Sep 2017
Hi Misha
1.
define the function to solve with in for instance a separate file
function F=fun1(x,n)
F(1)=x(1)+x(3)-12.54;
F(2)=x(2)+x(4)-12.245;
F(3)= 2*x(1)+n(2)+n(3)-(1493/60);
F(4) =(x(3)*x(2))/(x(1)*x(4))-1000;
2.
if choosing a single lucky start point, then for instance
f1=@fun1
x0=[1 1 1 1];
n=[1 2 1 2];
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
f2=@(x) f1(x,n)
Y=fsolve(f2,x0)
Y =
2.9951 4.5133 1.6388 0.0025
3.
Not all start points allow fsolve to start, for instance starting x0 all nulls fsolve returns error
x0=[0 0 0 0];
Y=fsolve(f2,x0)
Error using trustnleqn (line 28)
Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 388)
trustnleqn(funfcn,x,verbosity,gradflag,options,defaultopt,f,JAC,...
4.
A way to understand that there are multiple real roots is changing fsolve options to
.
problem.options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
problem.objective = f2;
problem.x0 = [1 1 1 1];
problem.solver = 'fsolve';
Y=fsolve(problem)
grid on
.
.
Misha, if you find this answer useful would you please consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  2 Comments
Misha Patel
Misha Patel on 17 Sep 2017
the output:
Y = 2.9951 4.5133 1.6388 0.0025
is not correct for my equations though
John BG
John BG on 17 Sep 2017
this is because the start point is really close to x1 x2 x3 x4, and with x1 x4 small, the 4th equation has 1/(x1-x4)
What are the intervals for x?
The roots also change depending upon values of n.
Note I chose first thought of n=[1 2 1 2].
What n values do you use?
Another way:
clear all;clc
syms x1 x2 x3 x4
n2=1;n3=1;
eqs=[x1+x3-12.54, 2+x4-12.245, 2*x1+n2+n3-(1493/60), (x3*x2)/(x1*x4)-1000];
vars=[x1 x2 x3 x4];
S=solve(eqs,vars)
S.x1
=
1373/120
S.x2
=
70331925/659
S.x3
=
659/600
S.x4
=
2049/200
or
clear all;clc
syms x1 x2 x3 x4 y1 y2 y3 y4
n2=1;n3=1;
eqs=[x1+x3-12.54==y1; 2+x4-12.245==y2; 2*x1+n2+n3-(1493/60)==y3; (x3*x2)/(x1*x4)-1000]==y4;
% vars=[x1 x2 x3 x4];
S=solve(eqs,[x1 x2 x3 x4 y1 y2 y3 y4])
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
> In solve (line 304)
S.x1
S.x2
S.x3
S.x4
S.y1
S.y2
S.y3
S.y4
=
-24.422375373098379314336862527351
=
86.546107815420532907149633277211
=
-42.498755958595581747330328897972
=
46.647494915323305014544840322395
=
-79.461131331693961061667191425346
=
36.402494915323305014544840322395
=
-71.728084079530091962007058388033
=
-996.77144989573753535055182388018
note the warning; cannot solve symbolically
With
S = solve(eqs,[x1 x2 x3 x4 y1 y2 y3 y4] , 'IgnoreAnalyticConstraints', true)
S.x1
S.x2
S.x3
S.x4
S.y1
S.y2
S.y3
S.y4
=
1373/120
=
70331925/659
=
659/600
=
2049/200
=
0
=
0
=
0
=
0
Perhaps you will find useful the output constraints that setting parameter ReturnConditions = true obtains
S=solve(eqs,[x1 x2 x3 x4 y1 y2 y3 y4],'ReturnConditions',true)
S =
struct with fields:
x1: [1×1 sym]
x2: [1×1 sym]
x3: [1×1 sym]
x4: [1×1 sym]
y1: [1×1 sym]
y2: [1×1 sym]
y3: [1×1 sym]
y4: [1×1 sym]
parameters: [1×4 sym]
conditions: [1×1 sym]
S.parameters
ans =
[ z, z1, z2, z3]
S.conditions
ans =
300*z2 ~= 600*z1 + 659 & z2 ~= -1373/60 & z ~= -2049/200
S.x1
> S.x1
S.x2
S.x3
S.x4
S.y1
S.y2
S.y3
S.y4
ans =
z2/2 + 1373/120
ans =
((68650*z)/3 + 10245*z2 + (937759*z3)/4000 + 1000*z*z2 + (1373*z*z3)/60 + (2049*z2*z3)/200 + z*z2*z3 + 937759/4)/(2*z1 - z2 + 659/300)
ans =
z1 - z2/2 + 659/600
ans =
z + 2049/200
ans =
z1
ans =
z
ans =
z2
ans =
z3

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Sep 2017
n = rand(1,3); %to have some data to test with
F = @(x) [x(1)+x(3)-12.54;
x(2)+x(4)-12.245;
2*x(1)+n(2)+n(3)-(1493/60);
(x(3)*x(2))/(x(1)*x(4))-1000];
fsolve(F, rand(1,4))

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!