MATLAB Answers

Solving non-linear equations in MATLAB

5 views (last 30 days)
Nurahmed
Nurahmed on 21 Nov 2019
Commented: Nurahmed on 30 Nov 2019
Hi, I have this equation set
function F = myfun(x)
F = [cos(x(1))-cos(x(2))+cos(x(3))-0.85*pi/4;
cos(3*x(1))-cos(3*x(2))+cos(3*x(3));
cos(5*x(1))-cos(5*x(2))+cos(5*x(3))];
Try to solve this
fun = @myfun;
x0 = [0,0.1,0.2];
x = fsolve(fun,x0);
I don't much understand the initial value x0, so I just set it randomly.
The results are below, which is not I expected. I was expecting x with the increment values, and not negative values.
-1.17089247233300 -0.947379689174785 0.531453934932589
I don't know if I made a mistake or this is the just results that I have to accept.
I really appreciate if you can help me. Thanks!

  4 Comments

Show 1 older comment
Nurahmed
Nurahmed on 21 Nov 2019
Thanks, I was suspecious to my itinial values too, let me try different values.
Star Strider
Star Strider on 21 Nov 2019
My pleasure.
Plotting the function could provide insight into the optimal initial parameter estimates.
Nurahmed
Nurahmed on 21 Nov 2019
Thanks.
But plotting the function I need initial value, is't it? Without plotting the function how can I set the optimal initial values?

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 22 Nov 2019
x = sym('x',[1 3]);
F = [cos(x(1))-cos(x(2))+cos(x(3))-0.85*pi/4;
cos(3*x(1))-cos(3*x(2))+cos(3*x(3));
cos(5*x(1))-cos(5*x(2))+cos(5*x(3))];
solF = solve(F, 'returnconditions', true);
Now if you examine solF.parameters and solF.conditions you will see that there are 16 sets of solutions for each variable, with the sets being defined in terms of three arbitrary integer variables, and each of the sets is defined in terms of a value being any of the three roots of a cubic equation. 16 basic forms, 3 solutions each = 48 basic solutions, each with an infinite number of solutions because of the arbitrary integers that are present. The conditions turn out to be the same for each of the solutions, by the way.
x(1) and x(2) each have four basic forms, and x(3) has two basic forms; the forms are used in combinations, leading to 16 basic solution combinations.

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 22 Nov 2019
If you restrict your x values to the range 0 to 2*pi then there are 48 solutions.
Walter Roberson
Walter Roberson on 23 Nov 2019
With cross-checking, I find the following solutions in the first period:
-1.1708924719169 5.33580562033257 -0.531453932745131
-1.1708924719169 5.33580562033257 0.531453932745131
-0.531453932745131 0.947379686847012 1.1708924719169
2.19421296674278 -2.61013872084466 1.1708924719169
2.19421296674278 2.61013872084466 -1.1708924719169
-0.531453932745131 0.947379686847012 -1.1708924719169
2.19421296674278 -2.61013872084466 -1.1708924719169
2.19421296674278 2.61013872084466 1.1708924719169

Sign in to comment.


Steven Lord
Steven Lord on 22 Nov 2019
You could try changing your initial guess. But for this set of equations, since the elements in x always appear inside a call to cos you could take advantage of the trig identity and add 2*pi (or a multiple of 2*pi) to any or all of the components of x to get another solution.
fun = @(x) [cos(x(1))-cos(x(2))+cos(x(3))-0.85*pi/4;
cos(3*x(1))-cos(3*x(2))+cos(3*x(3));
cos(5*x(1))-cos(5*x(2))+cos(5*x(3))];
x0 = [0,0.1,0.2];
x = fsolve(fun,x0)
% Check
shouldBeCloseToZero = fun(x)
% Create another solution
secondSolution = x+2*pi
shouldAlsoBeCloseToZero = fun(secondSolution)
% A third solution
x3 = x + [0, 2*pi, 4*pi]
shouldAlsoBeCloseToZero3 = fun(x3)

  0 Comments

Sign in to comment.


Nurahmed
Nurahmed on 29 Nov 2019
For initial values, is it possible to give a range for every solution instead of give specific values?

  2 Comments

Walter Roberson
Walter Roberson on 30 Nov 2019
If you use vpasolve(), then Yes, you can give a range for each value.
Note that vpasolve() will give only one solution. The initial point it will use will depend upon the range you supply for the parameters, unless you pass the option to use a random starting point. When you give a range for parameters with vpasolve() there is no way to directly provide a specific initial position.

Sign in to comment.

Sign in to answer this question.

Products


Release

R2018b