5 views (last 30 days)

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!

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.

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
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
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)

Sign in to comment.

Nurahmed
on 29 Nov 2019

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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769913

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769913

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769936

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769936

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769940

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_769940

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_770021

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/492425-solving-non-linear-equations-in-matlab#comment_770021

Sign in to comment.