Solving non-linear equations in MATLAB

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 Commenti

There are an infinity of solutions to problems involving trigonometric equaations. The solution you get is very much dependent on the initial parameter estimates, so try different values for ‘x0’.
Thanks, I was suspecious to my itinial values too, let me try different values.
My pleasure.
Plotting the function could provide insight into the optimal initial parameter estimates.
Thanks.
But plotting the function I need initial value, is't it? Without plotting the function how can I set the optimal initial values?

Accedi per commentare.

Risposte (3)

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 Commenti

Thanks for reply. I try hard to understand your explanation, but still couldn't understand...
If you restrict your x values to the range 0 to 2*pi then there are 48 solutions.
Oh I see.
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

Accedi per commentare.

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 il 29 Nov 2019
For initial values, is it possible to give a range for every solution instead of give specific values?

2 Commenti

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.
Thanks for your help.

Accedi per commentare.

Prodotti

Release

R2018b

Richiesto:

il 21 Nov 2019

Commentato:

il 30 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by