Solving non-linear equations in MATLAB

13 vues (au cours des 30 derniers jours)
Nurahmed
Nurahmed le 21 Nov 2019
Commenté : Nurahmed le 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 commentaires
Star Strider
Star Strider le 21 Nov 2019
My pleasure.
Plotting the function could provide insight into the optimal initial parameter estimates.
Nurahmed
Nurahmed le 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?

Connectez-vous pour commenter.

Réponses (3)

Walter Roberson
Walter Roberson le 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 commentaires
Nurahmed
Nurahmed le 22 Nov 2019
Oh I see.
Walter Roberson
Walter Roberson le 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

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 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)

Nurahmed
Nurahmed le 29 Nov 2019
For initial values, is it possible to give a range for every solution instead of give specific values?
  2 commentaires
Walter Roberson
Walter Roberson le 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.
Nurahmed
Nurahmed le 30 Nov 2019
Thanks for your help.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by