如果找不到解决方案,Matlab fsolve 停止

Matlab fsolve stop if No solution found

我在 for 循环中使用 fsolve。在某个时候,它找不到解决方案并给出以下输出。

如果 fsolve 找不到解决方案,是否可以阻止 matlab 执行进一步的代码?

当然可以。只需使用额外的 fsolve 输出:

[x, fval, exitflag, output] = fsolve(...)
if exitflag <= 0
    return
end

您可以根据 documentation

中列出的标志值相应地调整条件

有一个 fzero returns 的退出标志。如果它不是 1,则遇到了一些问题:

https://www.mathworks.com/help/matlab/ref/fzero.html

例如:

func                            = @(x) x^2 +1  
[x, fval, exitflag, output]     = fzero(func, 0)

if exitflag ~= 1
    disp('no solution was found, terminating further execution');
    return
end