这篇文章在Solve finds wrong solution?
中更新了我的问题给出x中的“简单”函数:
f = (x + 3/10)^(1/2) - (9*(x + 3/10)^5)/5 - (x + 3/10)^6 + (x - 1)/(2*(x + 3/10)^(1/2));
通过调用找到零
solve(f,x)
这产生3个零:
ans =
0.42846617518653978966562924618638
0.15249587894102346284238111155954
0.12068186494007759990714181154349
简单地看一下情节表明第三个根是无意义的:
我有一个严重的问题,因为我需要从上面的向量中检索最小的零。调用min(ans)返回错误的零。我该怎么做才能解决?
答案 0 :(得分:8)
这是一个非多项式方程,它可能是fallback to a numeric solver(非符号)。所以可能存在数字错误,或者数字算法可能会卡住并报告错误的解决方案,我不确定......
您可以做的是将解决方案替换回等式,并拒绝高于某个指定阈值的解决方案:
% define function
syms x real
syms f(x)
xx = x+3/10;
f(x) = sqrt(xx) - 9/5*xx^5 - xx^6 + (x - 1)/(2*sqrt(xx));
pretty(f)
% find roots
sol = solve(f==0, x, 'Real',true)
% filter bad solutions
err = subs(f, x, sol)
sol = sol(abs(err) < 1e-9); % this test removes the 2nd solution
% plot
h = ezplot(f, [0.1 0.5]);
line(xlim(), [0 0], 'Color','r', 'LineStyle',':')
xlabel('x'), ylabel('f(x)')
% programmatically insert data tooltips
xd = get(h, 'XData'); yd = get(h ,'YData');
[~,idx] = min(abs(bsxfun(@minus, xd, double(sol))), [], 2);
dcm = datacursormode(gcf);
pos = [xd(idx) ; yd(idx)].';
for i=1:numel(idx)
dtip = createDatatip(dcm, h);
set(get(dtip,'DataCursor'), 'DataIndex',idx(i), 'TargetPoint',pos(i,:))
set(dtip, 'Position',pos(i,:))
end
我们只留下两个理想的解决方案(一个被我们的测试拒绝):
/ 3 \5
9 | x + -- |
/ 3 \ \ 10 / / 3 \6 x - 1
sqrt| x + -- | - ------------- - | x + -- | + ----------------
\ 10 / 5 \ 10 / / 3 \
2 sqrt| x + -- |
\ 10 /
sol =
0.42846617518653978966562924618638
0.12068186494007759990714181154349 % <== this one is dropped
0.15249587894102346284238111155954
err(x) =
-9.1835496157991211560057541970488e-41
-0.058517436737550288309001512815475 % <==
1.8367099231598242312011508394098e-40
我也尝试过使用MATLAB的数值求解器,它们能够找到合理出发点的两个解决方案:
fzero
函数(无衍生root-finding algorithm)fsolve
函数(来自Optimization Toolbox)fcn = matlabFunction(f); % convert symbolic f to a regular function handle
opts = optimset('Display','off', 'TolFun',1e-9, 'TolX',1e-6);
% FZERO
sol2(1) = fzero(fcn, 0.1, opts);
sol2(2) = fzero(fcn, 0.5, opts);
disp(sol2) % [0.1525, 0.4285]
% FSOLVE
sol3(1) = fsolve(fcn, 0.0, opts);
sol3(2) = fsolve(fcn, 1.0, opts);
disp(sol3) % [0.1525, 0.4285]
为了比较,我尝试将方程式直接解释为MuPAD以及Mathematica。
当然,我们总是directly call MuPAD from inside MATLAB:
% f is the same symbolic function we've built above
>> sol = evalin(symengine, ['numeric::solve(' char(f) ' = 0, x, AllRealRoots)'])
sol =
[ 0.15249587894102346284238111155954, 0.42846617518653978966562924618638]
上述调用相当于搜索整个范围x = -infinity .. infinity
(可能很慢!)。我们应该尽可能提供特定的搜索范围来协助numeric::solve
:
>> sol = feval(symengine, 'numeric::solve', f==0, 'x = 0 .. 1', 'AllRealRoots')
sol =
[ 0.15249587894102346284238111155954, 0.42846617518653978966562924618638]
答案 1 :(得分:0)
一种解决方法可能是采用min(ans),然后检查f(min(ans))== 0。如果没有,则使用下一个最小的值。