how to use a piecewise function with fsolve?

4 Ansichten (letzte 30 Tage)
Ekin Ceyda Cetin
Ekin Ceyda Cetin am 28 Jan. 2017
Kommentiert: Walter Roberson am 10 Nov. 2018
I want to use piecewise objective function with fsolve. If i define the objective function using an if statement I get 'Conversion to logical from sym is not possible.'. When I use piecewise built-in function it returns an error when I try convert the objective function to a matlabFunction.
Actually, the objective function consists of 3 functions, for a region i need fsolve to consider only two of the functions. I was thinking of doing this by defining one of the functions as NaN in a certain region. Is this possible?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Jan. 2017
It is not possible to use piecewise with fsolve. It is possible to use piecewise with vpasolve.
Also I discovered earlier today that if you have a symbolic expression that contains piecewise, that if you use matlabFunction() with the 'file' option then the code written will use 'if' to decide the case and do the appropriate computation. Unfortunately the generated code is not vectorized (which would require logical indexing rather than if), but provided you used the matlabFunction 'vars' option to put all of variables into a single argument, you would be able to use it with fsolve.
  7 Kommentare
Cagdas Ozgenc
Cagdas Ozgenc am 10 Nov. 2018
Has this bug been fixed? Thanks.
Walter Roberson
Walter Roberson am 10 Nov. 2018
Unfortunately no, if you use matlabFunction with 'file' and piecewise() then although it generates appropriate if to handle the piecewise, it still does so assuming the input is a scalar. For example
F = [a, a^2, piecewise(a<0, -(-a)^(1/3), a^(1/3))]
is currently still turned into code that has
if (a < 0.0)
t0 = -(-a).^(1.0./3.0);
else
t0 = a.^(1.0./3.0);
end
F = [a,a.^2,t0];
instead of generating logical indexing or a loop.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by