在 Maxima 中求解分段函数
Solving piecewise functions in Maxima
我试图在 Maxima 中求解分段函数,但返回了 none 个函数解:
piecewiseExample(x) := if (x < 5) then x*2 else x/2;
solve([piecewiseExample(x) = 4], [x]);
//result: [(if x<5 then 2*x else x/2)=4]
Maxima有没有可能求出像这样的方程的解?
一种选择是使用数值方法:
(%i1) f(x) := if (x < 5) then x*2 else x/2;
x
(%o1) f(x) := if x < 5 then x 2 else -
2
(%i9) find_root(f(x)-4,x,-1000,1000);
(%o9) 8.0
在简单的情况下,你可以到处解决每个分支并过滤解决方案:
solve_and_filter(eq, var, p):= block([so: solve(eq, var), prederror: true],
sublist(so, lambda([c], p(rhs(c))))) $
pw_solve(pw, var):= map(lambda([L],
solve_and_filter(first(L), var, second(L))), pw) $
/* represent piecewise equation as a list of equation-predicate pairs
[ [eq1, pred1], [eq2, pred2], ... ] */
pw: [ [x*2 = 4, lambda([x], x< 5)],
[x/2 = 4, lambda([x], x>=5)]] $
/* solve every `eq' and filter solutions using `pred' */
pw_solve(pw, x);
我试图在 Maxima 中求解分段函数,但返回了 none 个函数解:
piecewiseExample(x) := if (x < 5) then x*2 else x/2;
solve([piecewiseExample(x) = 4], [x]);
//result: [(if x<5 then 2*x else x/2)=4]
Maxima有没有可能求出像这样的方程的解?
一种选择是使用数值方法:
(%i1) f(x) := if (x < 5) then x*2 else x/2;
x
(%o1) f(x) := if x < 5 then x 2 else -
2
(%i9) find_root(f(x)-4,x,-1000,1000);
(%o9) 8.0
在简单的情况下,你可以到处解决每个分支并过滤解决方案:
solve_and_filter(eq, var, p):= block([so: solve(eq, var), prederror: true],
sublist(so, lambda([c], p(rhs(c))))) $
pw_solve(pw, var):= map(lambda([L],
solve_and_filter(first(L), var, second(L))), pw) $
/* represent piecewise equation as a list of equation-predicate pairs
[ [eq1, pred1], [eq2, pred2], ... ] */
pw: [ [x*2 = 4, lambda([x], x< 5)],
[x/2 = 4, lambda([x], x>=5)]] $
/* solve every `eq' and filter solutions using `pred' */
pw_solve(pw, x);