Hi Michele,
In this situation, the code seems to be interacting with physical systems, generally [0,0,0] is good initial guess. However, a multi-start approach can be applied to the "fsolve" function to get more solutions which can be excluded analytically.
To obtain a feasible solution, the "initial guess" should be a good starting point. However, there is no numerical way to get such starting points.
Further illustrating my point, consider the simple nonlinear equation,
, using "fsolve" on this equation leads to the following results with initial guesses, [0, 1, 5]. options = optimoptions('fsolve','Display','off');
for idx = 1:numel(multiStart)
sol = fsolve(@func, multiStart(idx), options);
fprintf("The solution for initial guess %d is: %.5f\n", multiStart(idx), sol)
end
The solution for initial guess 0 is: 0.99219
The solution for initial guess 1 is: 1.00000
The solution for initial guess 5 is: 1.00586
If the dimensions of the same function are increased, the impact of the initial guess increases too, as observed in the case of ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1732993/image.png)
function f = funcHigher(x)
f = x^20 - 20*x^19 + 190*x^18 - 1140*x^17 + 4845*x^16 - 15504*x^15 + 38760*x^14 ...
- 77520*x^13 + 125970*x^12 - 167960*x^11 + 184756*x^10 - 167960*x^9 + 125970*x^8 ...
- 77520*x^7 + 38760*x^6 - 15504*x^5 + 4845*x^4 - 1140*x^3 + 190*x^2 - 20*x + 1;
for idx = 1:numel(multiStart)
sol = fsolve(@funcHigher, multiStart(idx), options);
fprintf("The solution for initial guess %d is: %.5f\n", multiStart(idx), sol)
end
The solution for initial guess 0 is: 0.36974
The solution for initial guess 1 is: 1.00000
The solution for initial guess 5 is: 1.75944
Upon running this code, the significantly off garbage floating point values can be observed on a seemingly "wrong" initial guess. The initial guess for the "right" answer was determined analytically since its evident that 1 is a root of
. The point stands, there is no right or wrong initial guess if the "fsolve" converges. There are merely more correct initial guesses which are determined by eliminating solutions or guessing values analytically.