Could fsolve cause trouble when using it to solve linear equations?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
John D'Errico
am 4 Apr. 2015
2 Stimmen
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 Kommentare
Mohammad Abouali
am 4 Apr. 2015
agree. People do all sort of tricks to keep the model to a linear system as long as it doesn't invalidate the results and it is possible. Here, we do have a linear system to begin with, why should we use fsolve which is for non-linear systems anyway!?
gautam vatsa
am 10 Dez. 2021
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
am 10 Dez. 2021
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).
Kategorien
Mehr zu Systems of Nonlinear Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!