Could fsolve cause trouble when using it to solve linear equations?
6 次查看(过去 30 天)
显示 更早的评论
Hello,
I have used fsolve optimisation algorithm to solve linear integral equations using integral command. But I know fsolve works for non-linear equations. So can I use it to solve linear equation system? Thanks
0 个评论
采纳的回答
Mohammad Abouali
2015-4-2
编辑:Mohammad Abouali
2015-4-2
Yes, you can, but if you have linear system of equations you have much better and optimized function for system of linear equations. So, why do you want to use fsolve and not use those?
By the way, fsolve is not for optimization. Solving a system for zero is different than optimizing an objective function. In some cases they work just the same but generally one shouldn't be used for the other.
6 个评论
Matt J
2015-4-4
编辑:Matt J
2015-4-4
I read the post by MathWorks Reps that you mentioned and he is saying the same thing that I am saying here.
No, I don't think you're saying the same thing as the MathWorks reps. Unless I've misuderstood, you are saying that none of the algorithms used by fsolve are optimization theoretic, and that is not what Shashank claimed in that thread.
You and I do agree that root-finding and optimization are not the same thing and that the function F(x) that you feed to fsolve is not something that is being minimized directly. However, it is not just a hypothetical possibility that the root-finding problem can be converted to an optimization problem. That is in fact what fsolve is doing internally and it is using known optimization principles and algorithms to extract a solution from this formulation. After all, there has to be some reason why fsolve is part of the Optimization Toolbox!
The exception to this is fsolve's default trust-region dogleg algorithm. Shashank claimed that this algorithm is not motivated by an optimization problem. Accordingly, for nonlinear equations, there is no apparent guarantee that a zero-gradient point of norm(F(x))^2 will be found. However, I think that in the special case where F(x) is linear, it is equivalent to a least algorithm, and my example appears to confirm that as well.
Mohammad Abouali
2015-4-4
编辑:Mohammad Abouali
2015-4-4
I did not say that none of the internal algorithm within fsolve is not optimization algorithm. I said, "By the way, fsolve is not for optimization." or "FSOLVE IS NOT for optimizing a function but finding its roots". These are copy paste from my replies and comments. I am saying fsolve shouldn't be used to optimize a function.
On the contrary, I actually said in my reply to you that fsolve and root finding algorithms turn the problem into an optimization and they solve it. (second paragraph of my reply to you). But they turn it into a problem that makes sure that the optimum point is where the function output is zero, i.e. the root.
again, if you have function F() and you want to optimize it; passing it to fsolve(F,...) does not necessarily provide you with the correct answer and my example above also clearly shows that. The only time that it works is that if the roots of the function are the minimum point of the function, as in your example.
Well I think we are saying the same thing anyway. "You and I do agree that root-finding and optimization are not the same thing and that the function F(x) that you feed to fsolve is not something that is being minimized directly." That's all I am trying to say, if you are looking for the minimum/optimum point of F() don't pass it to fsolve, but if you are looking for the roots or you already know that the minimum/optimum is the zero of the function then fsolve can be used; although if the goal is solely finding the root of the system of linear equations; then in this case, you have other options too such as:
- cgs (Conjugate gradients squared method),
- GMRES(Generalized minimum residual method (with restarts)),
- bicgstab (biconjugate gradients stabilized method)
- and my favorite one, AGMG (AGgregation-based algebraic MultiGrid) available on http://homepages.ulb.ac.be/~ynotay/AGMG/
These are for finding the root or solving a system of linear equations. There exist many more of these algorithms specific for solving a system of linear equations, and again, many of these algorithms do minimize the sum of squares error again (GMRES even has the "minimum residual" as part of its name), but their goal is not optimization.
By the way, if you look at Meva's profile it says "fluids". If (s)he is dealing with a system of linear equations arising from solving Navier-Stokes equation then above algorithms are much better suited for the job than fsolve. Actually AGMG (or other implementation of multigrid) is way more popular (and efficient) in solving the system of linear equations resulted from Poisson's equation for pressure than any other method. Used with preconditioning, they can do magic.
更多回答(1 个)
John D'Errico
2015-4-4
Think of it like this. Fsolve CAN solve a linear system of equations, but do you want to use the wrong tool for the job? In general, many tools can solve problems they are not really targeted to solve. But for a linear system, use a tool designed to solve that particular problem VERY well and efficiently.
A nonlinear solver will assume that you have a nonlinear problem. That means it will need starting values. It will need convergence tolerances. Fsolve will first need to compute the derivatives of what it thinks are nonlinear equations. (Or you will need to supply them.) So all of this makes fsolve less efficient for the problem than it need be.
After all, while I can probably drive in a nail by banging it with a screwdriver, a hammer will do so much better. And I suppose I could find a way to drive a screw with a hammer, but a screwdriver will be the right tool for the job.
So instead, just use backslash (or linsolve, or pinv, etc.) to solve linear problems. These tools are designed to be quite efficient and accurate on linear problems.
3 个评论
gautam vatsa
2021-12-10
Hi I have a similar difficulty. I totally admit your points - why at all should we use fsolve for solving linear problems. The issue in my case is that the my linear equations arrive after discretizing my partial differential equations (which are although linear but very complicated - coupled and 4th order). So for me it is much easier to form a function handle than to create the matrix vector form. Can you suggest some linear equation solver that admits function handle (consisting of set of linear equations in variables) just like fsolve does.
The function equationToMatrix requires me to represent my variables in symbolic variables, however I have too many variables and therefore, using symbolic variables makes my codes run too slow. Please suggest something.
Torsten
2021-12-10
If your equations are linear in the vector x of unknowns, use the function handle to calculate the Jacobian of your system (with Jacobianest, e.g.) (Result is A) and evaluate your function handle with all variables set to 0 (Result is b).
Then your linear system to solve reads
A*x = -b
such that
x = A\(-b).
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!