Numerically solving piecewise system of equations

I am using vpasolve to numerically solve systems of equations. I tried to solve a problem that had a piecewise function in it and it did not work. I made the simplest problem I could that replicated the issue below to troubleshoot the issue. If I set the initial parameters to the exact answer ([0.5,0.25]), MATLAB confirms the solution. If I set the initial parameters slightly away from the solution (for example [0.5,0.24999]), the function fails to find a solution.
function [Out] = piecewise_test
a=-0.25;
% a=1;
syms x y
eqn1 = y == x+a;
% if abs(x)>=1
% eqn2 = y == abs(x);
% else
% eqn2 = y == x^2;
% end
eqn2 = y == piecewise(abs(x)>=1,abs(x),x^2);
sol=vpasolve([eqn1, eqn2],[x, y],[0.5,0.25])
Out=[sol.x, sol.y];
end
In the full problem, the solution can be predicted decently within a range, so using initial parameters is fine as long as it can find the answer within 1 order of magnitude of the initial parameter.
I am looking to calculate a numerical solution and I am ignoring any complex or negative solutions. I want to keep solve time down if possible as the function runs at least 200 times in my code.

Réponses (0)

Catégories

En savoir plus sur Mathematics and Optimization dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by