How to use function like `min` in symfun?(I got an error)

6 ビュー (過去 30 日間)
Liming Fang
Liming Fang 2020 年 4 月 26 日
コメント済み: Walter Roberson 2020 年 4 月 26 日
For example, i want to create a symbolic function: f(x,y) = min(x-1,y+1);
when f(x,y) = x+y, everything is ok, but when it comes to min, error occured.
syms x y
f = symfun(min([x-1 y+1])),[x y])
I got error like this:
"Unable to convert expression into double array."
Help...

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 4 月 26 日
You can't use min or max in symbolic variables in matlab. A work around can be this one:
syms x1 x2 real;
xlt = symfun(1/2*(x1+x2-abs(x1-x2)),[x1,x2]); %min
xgt = symfun(1/2*(x1+x2+abs(x1-x2)),[x1,x2]); %max
  4 件のコメント
Liming Fang
Liming Fang 2020 年 4 月 26 日
編集済み: Walter Roberson 2020 年 4 月 26 日
ok i got it.
Does this mean that functions involved condition won't work?
And the answer below menthons "piecewise", i wonder whether it is used to solve functions involved condions.
Walter Roberson
Walter Roberson 2020 年 4 月 26 日
It depends upon how the condition is constructed.
Functions you should avoid for symbolic expressions that include unresolved symbolic variables include min(), max(), and mod() . Most other functions are willing to postpone evaluation when they are passed symbolic variables, but those three do odd or undesirable things.
And the answer below menthons "piecewise", i wonder whether it is used to solve functions involved condions.
Yes, it is. You can even differentiate piecewise -- but the boundary conditions might not get the right value, as piecewise() expressions are often discontinuous on the boundaries and differentiating piecewise() does not return a special "undefined" result for the boundary.
However, solve() sometimes has difficulty with piecewise, especially something of the form piecewise(condition, expression, condition, expression...) == value . Sometimes you need to solve() against each of the expressions and then test to see whether the result meets the conditions

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 4 月 26 日
syms x y
f = symfun(piecewise(x-1<=y+1, x-1, y+1),[x y])

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by