• System of Linear Equation in MATLAB
  • Matlab Howtos

Solve the System of Linear Equations Using the solve() Function in MATLAB

Solve the system of linear equations using the linsolve() function in matlab.

System of Linear Equation in MATLAB

This tutorial will discuss solving the system of linear equations using the solve() and linsolve() functions in Matlab.

We can use the Matlab built-in function solve() to solve the system of linear equations in Matlab. First of all, we can define the variables using the syms variable. After that, we can write the equations in Matlab. After that, we need to use the function solve() to solve the equations. For example, let’s define some equations in Matlab and find their solution using the solve() function. See the code below.

As you can see, there are three variables in the equation, and there are three answers. You can also use vapsolve() function instead of the solve() function to get the answer in numeric. To use the vpasolve() function, you need to change the function name solve to vpasolve in the above code. If the equations are in matrix form, you can use the linsolve() function.

The function linsolve() is used instead of the solve() function if you have matrices instead of equations. We can also convert the equations to matrix form using the equationsToMatrix() function. For example, let’s define some equations in Matlab and find their solution using the linsolve() function. See the code below.

The solve() and linsolve() functions come with the symbolic math Toolbox, so make sure you have installed the toolbox to use these functions.

Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

Related Article - MATLAB Equation

  • How to Solve Quadratic Equations in MATLAB
  • Trending Now
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial

Solution of system of linear equation in MATLAB

  • Plot a line along 2 points in MATLAB
  • Find number of solutions of a linear equation of n variables
  • Solve System of Equations in R
  • System of Linear Equations
  • How to Solve a System of Equations using Inverse of Matrices?
  • Number of Solutions to a System of Equations Algebraically
  • Solve the Linear Equation using Substitution Method
  • Python - Solve the Linear Equation of Multiple Variable
  • Solve Linear Equations using eval() in Python
  • Algebraic Methods of Solving Pair of Linear Equations in Two Variables
  • Graphical Methods of Solving Pair of Linear Equations in Two Variables
  • Pair of Linear Equations in Two Variables
  • Linear Equation in Two Variables
  • Linear Equations in One Variable
  • Python | Finding Solutions of a Polynomial Equation
  • Solving Linear Equations Using the Elimination Method
  • Graph of Linear Equations in Two Variables
  • Solve the Linear Equation of Single Variable
  • How to solve a pair of nonlinear equations using Python?

Let us see how to solve a system of linear equations in MATLAB. Here are the various operators that we will be deploying to execute our task :

  • \ operator : A \ B is the matrix division of A into B, which is roughly the same as INV(A) * B . If A is an NXN matrix and B is a column vector with N components or a matrix with several such columns, then X = A \ B is the solution to the equation A * X = B . A warning message is printed if A is badly scaled or nearly singular. A\EYE(SIZE(A)) produces the inverse of A.
  • linsolve operator : X = LINSOLVE(A, B) solves the linear system A * X = B using LU factorization with partial pivoting when A is square, and QR factorization with column pivoting. A warning is given if A is ill conditioned for square matrices and rank deficient for rectangular matrices.

Example 1 : Non-homogeneous System Ax = b, where A is a square and is invertible. In our example we will consider the following equations :

We will convert these equations into matrices A and b :

Now we will create an augmented matrix Ab. We will compare the ranks of Ab and A, if the ranks are equal then a unique solution exists.

Now we can find the solution to this system of equations by using 3 methods:

  • conventional way : inv(A) * b
  • using mid-divide routine : A \ b
  • using linsolve routine : linsolve(A, b)

We can verify the correctness of the solution by finding the error using A * x - b . The error should be 0.

As all the errors are close to 0, we can say that the solution is correct.

Example 2 : Non-homogeneous system Ax = b, where A is a square and it is not invertible. In our example we will consider the following equations :

Example 3 : Non-homogeneous system Ax = b where A is not a square. In our example we will consider the following equations :

Example 4 : Homogeneous system Ax = 0 where A is a square and is invertible. In our example we will consider the following equations :

Example 5 : Homogeneous system Ax = 0 where A is a square and is not invertible. In our example we will consider the following equations :

author

Please Login to comment...

Similar reads.

  • Computer Subject

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Help Center Help Center

  • Help Center
  • Trial Software
  • Product Updates
  • Documentation

Solve System of Algebraic Equations

This topic shows you how to solve a system of equations symbolically using Symbolic Math Toolbox™. This toolbox offers both numeric and symbolic equation solvers. For a comparison of numeric and symbolic solvers, see Select Numeric or Symbolic Solver .

Handle the Output of solve

Suppose you have the system

x 2 y 2 = 0 x - y 2 = α   ,

and you want to solve for x and y . First, create the necessary symbolic objects.

There are several ways to address the output of solve . One way is to use a two-output call. The call returns the following.

( 0 - 2   a )

Modify the first equation to x 2 y 2 = 1 . The new system has more solutions. Four distinct solutions are produced.

( a 2 - a 2 - 2 2 a 2 - a 2 + 2 2 a 2 + a 2 - 2 2 a 2 + a 2 + 2 2 )

( - a - a 2 - 2 - a - a 2 + 2 a 2 - 2 - a a 2 + 2 - a )

Since you did not specify the dependent variables, solve uses symvar to determine the variables.

This way of assigning output from solve is quite successful for “small” systems. For instance, if you have a 10-by-10 system of equations, typing the following is both awkward and time consuming.

To circumvent this difficulty, solve can return a structure whose fields are the solutions. For example, solve the system of equations u^2 - v^2 = a^2 , u + v = 1 , a^2 - 2*a = 3 . The solver returns its results enclosed in a structure.

The solutions for a reside in the “ a -field” of S .

Similar comments apply to the solutions for u and v . The structure S can now be manipulated by the field and index to access a particular portion of the solution. For example, to examine the second solution, you can use the following statement to extract the second component of each field.

The following statement creates the solution matrix M whose rows comprise the distinct solutions of the system.

( - 1 1 0 3 5 - 4 )

Clear solx and soly for further use.

Solve a Linear System of Equations

Linear systems of equations can also be solved using matrix division. For example, solve this system.

( 2   v 3 - 5   u 3 4   u 3 - v 3 )

Thus, sol and z produce the same solution, although the results are assigned to different variables.

Return the Full Solution of a System of Equations

solve does not automatically return all solutions of an equation. To return all solutions along with the parameters in the solution and the conditions on the solution, set the ReturnConditions option to true .

Consider the following system of equations:

sin ( x ) + cos ( y ) = 4 5 sin ( x )   cos ( y ) = 1 10

Visualize the system of equations using fimplicit . To set the x-axis and y-axis values in terms of pi , get the axes handles using axes in a . Create the symbolic array S of the values -2*pi to 2*pi at intervals of pi/2 . To set the ticks to S , use the XTick and YTick properties of a . To set the labels for the x-and y-axes, convert S to character vectors. Use arrayfun to apply char to every element of S to return T . Set the XTickLabel and YTickLabel properties of a to T .

linear equation solve in matlab

The solutions lie at the intersection of the two plots. This shows the system has repeated, periodic solutions. To solve this system of equations for the full solution set, use solve and set the ReturnConditions option to true .

solve returns a structure S with the fields S.x for the solution to x , S.y for the solution to y , S.parameters for the parameters in the solution, and S.conditions for the conditions on the solution. Elements of the same index in S.x , S.y , and S.conditions form a solution. Thus, S.x(1) , S.y(1) , and S.conditions(1) form one solution to the system of equations. The parameters in S.parameters can appear in all solutions.

Index into S to return the solutions, parameters, and conditions.

( z 1 z 1 )

( z + acos ( σ 3 ) 2   π ∈ Z ∨ z - acos ( σ 3 ) 2   π ∈ Z ∧ - π - z 1 + σ 1 2   π ∈ Z ∨ z 1 + σ 1 2   π ∈ Z z 1 - π + asin ( σ 3 ) 2   π ∈ Z ∨ z 1 - asin ( σ 3 ) 2   π ∈ Z ∧ z + σ 2 2   π ∈ Z ∨ z - σ 2 2   π ∈ Z ) where    σ 1 = asin ( 6 10 - 2 5 )    σ 2 = acos ( 2 5 - 6 10 )    σ 3 = 6 10 + 2 5

Solve a System of Equations Under Conditions

To solve the system of equations under conditions, specify the conditions in the input to solve .

Solve the system of equations considered above for x and y in the interval -2*pi to 2*pi . Overlay the solutions on the plot using scatter .

linear equation solve in matlab

Work with Solutions, Parameters, and Conditions Returned by solve

You can use the solutions, parameters, and conditions returned by solve to find solutions within an interval or under additional conditions. This section has the same goal as the previous section, to solve the system of equations within a search range, but with a different approach. Instead of placing conditions directly, it shows how to work with the parameters and conditions returned by solve .

For the full solution S of the system of equations, find values of x and y in the interval -2*pi to 2*pi by solving the solutions S.x and S.y for the parameters S.parameters within that interval under the condition S.conditions .

Before solving for x and y in the interval, assume the conditions in S.conditions using assume so that the solutions returned satisfy the condition. Assume the conditions for the first solution.

Find the parameters in S.x and S.y .

Solve the first solution of x for the parameter paramx .

( π + asin ( 6 10 - 2 5 ) asin ( 6 10 - 2 5 ) - π - asin ( 6 10 - 2 5 ) - 2   π - asin ( 6 10 - 2 5 ) )

Similarly, solve the first solution of y for paramy .

( acos ( 6 10 + 2 5 ) acos ( 6 10 + 2 5 ) - 2   π - acos ( 6 10 + 2 5 ) 2   π - acos ( 6 10 + 2 5 ) )

Clear the assumptions set by S.conditions(1) using assume . Call asumptions to check that the assumptions are cleared.

Assume the conditions for the second solution.

Solve the second solution to x and y for the parameters paramx and paramy .

( π + σ 1 σ 1 - π - σ 1 - 2   π - σ 1 σ 2 π - σ 2 σ 2 - 2   π - π - σ 2 ) where    σ 1 = asin ( 6 10 - 2 5 )    σ 2 = asin ( 6 10 + 2 5 )

( σ 2 σ 2 - 2   π - σ 2 2   π - σ 2 σ 1 σ 1 - 2   π - σ 1 2   π - σ 1 ) where    σ 1 = acos ( 2 5 - 6 10 )    σ 2 = acos ( 6 10 + 2 5 )

The first rows of paramx and paramy form the first solution to the system of equations, and the second rows form the second solution.

To find the values of x and y for these values of paramx and paramy , use subs to substitute for paramx and paramy in S.x and S.y .

Note that solx and soly are the two sets of solutions to x and to y . The full sets of solutions to the system of equations are the two sets of points formed by all possible combinations of the values in solx and soly .

Plot these two sets of points using scatter . Overlay them on the plot of the equations. As expected, the solutions appear at the intersection of the plots of the two equations.

linear equation solve in matlab

Convert Symbolic Results to Numeric Values

Symbolic calculations provide exact accuracy, while numeric calculations are approximations. Despite this loss of accuracy, you might need to convert symbolic results to numeric approximations for use in numeric calculations. For a high-accuracy conversion, use variable-precision arithmetic provided by the vpa function. For standard accuracy and better performance, convert to double precision using double .

Use vpa to convert the symbolic solutions solx and soly to numeric form.

( 2.9859135500977407388300518406219 - 3.2972717570818457380952349259371 0.15567910349205249963259154265761 - 6.1275062036875339772926952239014 0.70095651347102524787213653614929 2.4406361401187679905905068471302 - 5.5822287937085612290531502304097 - 3.8425491670608184863347799194288 )

( 0.86983981332387137135918515549046 - 5.4133454938557151055661016110685 - 0.86983981332387137135918515549046 5.4133454938557151055661016110685 1.4151172233028441195987301489821 - 4.8680680838767423573265566175769 - 1.4151172233028441195987301489821 4.8680680838767423573265566175769 )

Simplify Complicated Results and Improve Performance

If results look complicated, solve is stuck, or if you want to improve performance, see, Troubleshoot Equation Solutions from solve Function .

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

An Adaptive Orthogonal Basis Method for Computing Multiple Solutions of Differential Equations with Polynomial Nonlinearities

  • Published: 27 May 2024
  • Volume 100 , article number  11 , ( 2024 )

Cite this article

linear equation solve in matlab

  • Lin Li 1 , 2 ,
  • Yangyi Ye 1 &
  • Huiyuan Li 3  

73 Accesses

Explore all metrics

This paper presents an innovative approach, the Adaptive Orthogonal Basis Method, tailored for computing multiple solutions to differential equations characterized by polynomial nonlinearities. Departing from conventional practices of predefining candidate basis pools, our novel method adaptively computes bases, considering the equation’s nature and structural characteristics of7 the solution. It further leverages companion matrix techniques to generate initial guesses for subsequent computations. Thus this approach not only yields numerous initial guesses for solving such equations but also adapts orthogonal basis functions to effectively address discretized nonlinear systems. Through a series of numerical experiments, this paper demonstrates the method’s effectiveness and robustness. By reducing computational costs in various applications, this novel approach opens new avenues for uncovering multiple solutions to differential equations with polynomial nonlinearities.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price includes VAT (Russian Federation)

Instant access to the full article PDF.

Rent this article via DeepDyve

Institutional subscriptions

linear equation solve in matlab

Similar content being viewed by others

linear equation solve in matlab

An Efficient Spectral Trust-Region Deflation Method for Multiple Solutions

linear equation solve in matlab

Towards a reliable implementation of least-squares collocation for higher index differential-algebraic equations—Part 2: the discrete least-squares problem

A parameterized multi-step newton method for solving systems of nonlinear equations, data availability.

The data that support the findings of this study are available from the corresponding author upon reasonable request.

Allgower, E.L., Sommese, A.J., Bates, D.J., Wampler, C.W.: Solution of polynomial systems derived from differential equations. Computing 76 , 1–10 (2006)

Article   MathSciNet   Google Scholar  

Allgower, E.L., Cruceanu, S.G., Tavener, S.: Application of numerical continuation to compute all solutions of semilinear elliptic equations. Adv. Geom. 76 , 1–10 (2009)

MathSciNet   Google Scholar  

Alpert, B.K., Rokhlin, V.: A fast algorithm for the evaluation of legendre expansions. SIAM J. Sci. Stat. Comput. 12 , 158–179 (1991)

Chen, C.M., Xie, Z.Q.: Search extension method for multiple solutions of a nonlinear problem. Comput. Math. Appl. 47 , 327–343 (2004)

Choi, Y.S., McKenna, P.J.: A mountain pass method for the numerical solution of semilinear elliptic problems. Nonlinear Anal. 20 , 417–437 (1993)

Davis, H.T.: Introduction to nonlinear differential and integral equations. US Atomic Energy Commission (1960)

Ding, Z.H., Costa, D., Chen, G.: A high-linking algorithm for sign-changing solutions of semilinear elliptic equations. Nonlinear Anal. 38 , 151–172 (1999)

Du, Q., Zhang, L., Zheng, Z.Z.: Optimization-based shrinking dimer method for finding transition states. SIAM J. Sci. Comput. 38 , A528–A544 (2016)

Farrell, P.E., Birkisson, A., Funke, S.W.: Deflation techniques for finding distinct solutions of nonlinear partial differential equations. SIAM J. Sci. Comput. 37 , A2026–A2045 (2015)

Frisch, U., Matarrese, S., Mohayaee, R., Sobolevski, A.: A reconstruction of the initial conditions of the universe by optimal mass transportation. Nature 417 , 260–262 (2002)

Article   Google Scholar  

Gould, N., Sainvitu, C., Toint, P.L.: A filter-trust-region method for unconstrained optimization. SIAM J. Optim. 16 , 341–357 (2006)

Hale, N., Townsend, A.: A fast, simple and stable Chebyshev–Legendre transform using an asymptotic formula. SIAM J. Sci. Comput. 36 , A148–A167 (2014)

Hao, W., Lee, S., Lee, Y.J.: Companion-based multi-level finite element method for computing multiple solutions of nonlinear differential equations. arXiv preprint arXiv:2305.04162 (2023)

Hao, Wenrui, Xue, Chuan: Spatial pattern formation in reaction-diffusion models: a computational approach. J. Math. Biol. 80 , 521–543 (2020)

Hao, W.R., Hauenstein, J.D., Hu, B., Sommese, A.J.: A bootstrapping approach for computing multiple solutions of differential equations. J. Comput. Appl. Math. 258 , 181–190 (2014)

Hao, Wenrui, Hesthaven, Jan, Lin, Guang, Zheng, Bin: A homotopy method with adaptive basis selection for computing multiple solutions of differential equations. J. Sci. Comput. 82 , 1–17 (2020)

Hao, W.R., Zhao, X.Y.E., Chen, L.Q., Zhao, Y.X.: Bifurcation analysis reveals solution structures of phase field models. Commun. App. Math. Comput. 6 , 64–89 (2024)

Li, Y.X., Zhou, J.X.: A minimax method for finding multiple critical points and its applications to semilinear pdes. SIAM J. Sci. Comput. 23 , 840–865 (2001)

Li, Z.X., Yang, Z.H., Zhu, H.I.: Bifurcation method for solving multiple positive solutions to Henon equation. Sci. China Ser. 37 , 1417–1428 (2007)

Google Scholar  

Li, L., Wang, L.L., Li, H.Y.: An efficient spectral trust-region deflation method for multiple solutions. J. Sci. Comput. 32 , 1–23 (2023)

McKenna, P.J., Breuer, B., Plum, M.: Multiple solutions for a semilinear boundary value problem: a computational multiplicity proof. J. Differ. Equ. 195 , 243–269 (2003)

Natarajan, T., Ahmed, N., Rao, K.R.: Discrete cosine transform. IEEE Trans. Comput. 100 , 90–93 (1974)

Nocedal, J., Wright, S.J.: Numerical Optimization, vol. 25. Springer Series in Operations Research (1999)

Shen, J., Tang, T., Wang, L.L.: Spectral Methods: Algorithms, Analysis and Applications, vol. 41. Springer, Berlin (2011)

Steidl, G., Potts, D., Tasche, M.: Fast algorithms for discrete polynomial transforms. Math. Comput. 67 , 1577–1590 (1998)

Sun, W.Y., Yuan, Y.X.: Optimization Theory and Methods: Nonlinear Programming, vol. 1. Springer, Berlin (2006)

Tadmor, E.: A review of numerical methods for nonlinear partial differential equations. Bull. Am. Math. Soc. 49 , 507–554 (2012)

Trefethen, L.N.: Spectral Methods in MATLAB, vol. 41. Tsinghua University Press, Beijing (2011)

Wang, Y., Hao, W., Lin, G.: Two-level spectral methods for nonlinear elliptic equations with multiple solutions. SIAM J. Sci. Comput. 40 , B1180–B1205 (2018)

Xie, Z.Q. Liu, W., Yuan, Y.J.: A constrained gentlest ascent dynamics and its applications to find excited states of Bose–Einstein condensates. https://arxiv.org/pdf/2209.04684v1

Xie, Z.Q., Chen, C.M., Xu, Y.: An improved search-extension method for computing multiple solutions of semilinear PDEs. IMA J. Numer. Anal. 25 , 549–576 (2005)

Yang, Z.H., Li, Z.X., Zhu, H.L.: Bifurcation method for solving multiple positive solutions to boundary value problem of henon equation on unit disk. Comput. Math. Appl. 62 , 3775–3784 (2011)

Yao, X.D., Zhou, J.X.: A minimax method for finding multiple critical points in banach spaces and its application to quasi-linear elliptic PDEs. SIAM J. Sci. Comput. 26 , 1796–1809 (2005)

Yao, X.D., Zhou, J.X.: Numerical methods for computing nonlinear eigenpairs: Part I. Iso-Homogeneous cases. SIAM J. Sci. Comput. 29 , 1355–1374 (2007)

Yao, X.D., Zhou, J.X.: Numerical methods for computing nonlinear eigenpairs: Part II. Non-Iso-Homogeneous cases. SIAM J. Sci. Comput 30 , 937–956 (2008)

Zhang, J.Y., Du, Q.: Shrinking dimer dynamics and its applications to saddle point search. SIAM J. Numer. Anal. 50 , A1899–A1921 (2012)

Zhang, H., Andrew, R., Scheinberg, K.: A derivative-free algorithm for least-squares minimization. SIAM J. Optim. 20 , 3555–3576 (2010)

Zhang, X.P., Zhang, J.T., Yu, B.: Eigenfunction expansion method for multiple solutions of semilinear elliptic equations with polynomial nonlinearity. SIAM J. Numer. Anal. 51 , 2680–2699 (2013)

Zhang, L., Yin, J.Y., Zhang, P.W.: High-index optimization-based shrinking dimer method for finding high-index saddle points. SIAM J. Sci. Comput. 6 , A3576–A3595 (2019)

Zhou, J.X.: Instability analysis of saddle points by a local minimax method. Math. Comput. 74 , 1391–1411 (2004)

Zhou, X.: The gentlest ascent dynamics. Nonlinearity 24 , 1831–1842 (2011)

Download references

This work is partially supported by the the National Natural Science Foundations of China (Nos. 11871455, 12131005).

Author information

Authors and affiliations.

School of Mathematics and Physics, University of South China, Hengyang, 421001, China

Lin Li & Yangyi Ye

Key Laboratory of Computing and Stochastic Mathematics(Ministry of Education), School of Mathematics and Statistics, Hunan Normal University, Changsha, 410081, Hunan, China

State Key Laboratory of Computer Science/Laboratory of Parallel Computing, Institute of Software, Chinese Academy of Sciences, Beijing, China

You can also search for this author in PubMed   Google Scholar

Corresponding author

Correspondence to Lin Li .

Ethics declarations

Conflict of interest.

The authors have no relevant financial interest to disclose.

Additional information

Publisher's note.

Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

We present the detailed process of the trust region method to solve ( 2.6 ). For this purpose, we introduce a region around the current best solution, and approximate the objective function by a quadratic form which boils down to solving a sequence of trust-region subproblems:

where the trust region \({\mathbb B}_{h_k}:=\{\varvec{s}\in {\mathbb R}^n\,:\,\Vert \varvec{s}\Vert \le h_k\}\) . When \(h_{k}\) is given and \({\varvec{s}}_{k}\) is the minimizer of \(q^{(k)}({\varvec{s}})\) in ( 5.1 ), we can update \({\varvec{{a}}}_{k+1} = {\varvec{{a}}}_{k} + {\varvec{s}}_{k}\) . Obviously, it is one of the most critical steps to choose a proper \(h_k\) at each iteration. Based on a good agreement between \(q^{(k)}(s_{k})\) and the objective function value \(Q({\varvec{a}}_{k+1})\) , we should choose \(h_{k}\) as large as possible. To be specific, we define a ratio

The ratio \(r_{k}\) is an indicator for expanding and contracting the trust region. If \(r_{k}\) is negative, the current value of \(Q({\varvec{a}}_{k})\) is less than the new objective value \(Q({\varvec{a}}_{k} + {\varvec{s}}_{k})\) , consequently the step should be rejected. If \(r_{k}\) is close to 1, it means there is a good agreement between the model \(q^{(k)}\) and the objective function Q over this step, we can expand the trust region for the next iteration. If \(r_{k}\) is close to zero, the trust region should be contracted. Otherwise, we do not alter the trust region at the next iteration. Moreover, the process is also summarized in the following algorithm 1 .

figure a

For simplicity, in general we choose \(\epsilon = 10^{-13}, \delta _1 = 0.25, \delta _2 = 0.75, \tau _1 = 0.5,\) and \(\tau _2 = 2\) throughout the paper. Moreover, in the Algorithm 1 (see Line 4), the subproblem ( 5.1 ) needs to be solved. Here the so-called dogleg method (see [ 11 , 26 ]) is used to solve it, and the process is as follows: Let \(s:= {\varvec{a}}_{k} - d_{k}{\varvec{g}}_{k}\) , and substituting it into ( 5.1 ) yields

Based on the exact line search, the step size \(d_{k}\) becomes

Consequently the corresponding step along the steepest descent direction is

On the other hand, the Newtonian step is

If \(\Vert {\varvec{s}}^{C}_{k}\Vert _{2} = \Vert d_{k}{\varvec{g}}_{k}\Vert _{2} \ge h_{k}\) , the solution of ( 5.1 ) can be obtained, i.e.,

which leads to \({\varvec{a}}_{k+1} = {\varvec{a}}_{k} + {\varvec{s}}_{k}\) . If \(\Vert {\varvec{s}}^{C}_{k}\Vert _2 < h_{k}\) and \(\Vert {\varvec{s}}^{N}_{k}\Vert _2 > h_{k}\) , a dogleg path consisting of two line segments is used to approximate \({\varvec{s}}\) in ( 5.1 ), i.e.,

Obviously, when \(\lambda = 0\) , \({\varvec{s}}_{k}(\lambda )\) reduces to the steepest descent direction. While \(\lambda = 1\) , it becomes the Newtonian direction. To exactly obtain \(\lambda \) in ( 5.4 ), we will solve the following equation:

As a result, we have

Otherwise, we choose

In summary, with ( 5.3 ), ( 5.4 ) and ( 5.5 ), the solution \({\varvec{s}}_{k}\) in ( 5.1 ) becomes

figure 18

Exact trajectory and dogleg approximation

Next, we remark the trust region method for solving nonlinear algebraic system ( 2.5 ). As mentioned in [ 26 ], the trust region enjoys the desirable global convergence with a local superlinear rate of convergence as follows.

Theorem 5.1

Assume that

the function \(Q({\varvec{{a}}})\) is bounded below on the level set

and is Lipschitz continuously differentiable in H ; 

the Hessian matrixes \(G({\varvec{{x}}}^{(k)})\) are uniformly bounded in 2-norm, i.e., \(\Vert G({\varvec{{a}}}_{k})\Vert \le \beta \) for any k and some \(\beta >0\) .

If \({\varvec{g}}({\varvec{{a}}}_{k}) \ne {\varvec{0}}\) , then

Moreover, if \({\varvec{g}}({\varvec{{a}}}^{*}) = {\varvec{0}}\) , and \({\varvec{G}}({\varvec{{a}}}^{*})\) is positive definite, then the convergence rate of the trust region method is quadratic.

When k is large enough, the trust region method becomes the Newtonian iteration. As a result, it has the same convergence rate as the Newtonian method. \(\square \)

In practice, the gradient and Hessian matrices might be appropriately approximated by some numerical means. We refer to Zhang et al. [ 37 ] for such derivative-free methods for ( 2.6 ) with \(\varvec{f}\) being twice continuously differentiable, but none of their first-order or second-order derivatives being explicitly available. \(\square \)

Rights and permissions

Springer Nature or its licensor (e.g. a society or other partner) holds exclusive rights to this article under a publishing agreement with the author(s) or other rightsholder(s); author self-archiving of the accepted manuscript version of this article is solely governed by the terms of such publishing agreement and applicable law.

Reprints and permissions

About this article

Li, L., Ye, Y. & Li, H. An Adaptive Orthogonal Basis Method for Computing Multiple Solutions of Differential Equations with Polynomial Nonlinearities. J Sci Comput 100 , 11 (2024). https://doi.org/10.1007/s10915-024-02557-7

Download citation

Received : 06 January 2024

Revised : 06 April 2024

Accepted : 25 April 2024

Published : 27 May 2024

DOI : https://doi.org/10.1007/s10915-024-02557-7

Share this article

Anyone you share the following link with will be able to read this content:

Sorry, a shareable link is not currently available for this article.

Provided by the Springer Nature SharedIt content-sharing initiative

  • Multiple solutions
  • Spectral method
  • Nonlinear differential equations
  • Adaptive orthogonal basis

Mathematics Subject Classification

  • Find a journal
  • Publish with us
  • Track your research

Help Center Help Center

  • Help Center
  • Mises à jour du produit
  • Documentation

Solve linear system of equations

Description

X = linsolve( A , B ) solves the linear system A X = B using one of these methods:

When A is square , linsolve uses LU factorization with partial pivoting.

For all other cases, linsolve uses QR factorization with column pivoting.

linsolve warns if A is ill conditioned (for square matrices) or rank deficient (for rectangular matrices).

X = linsolve( A , B , opts ) uses an appropriate solver as determined by the options structure opts . The fields in opts are logical values describing properties of the matrix A . For example, if A is an upper triangular matrix, you can set opts.UT = true to make linsolve use a solver designed for upper triangular matrices. linsolve does not test to verify that A has the properties specified in opts .

[ X , r ] = linsolve( ___ ) also returns r , which is the reciprocal of the condition number of A (for square matrices) or the rank of A (for rectangular matrices). You can use any of the input argument combinations in previous syntaxes. With this syntax, linsolve does not warn if A is ill conditioned or rank deficient.

collapse all

Solve Linear System

Solve a linear system with both mldivide and linsolve to compare performance.

mldivide is the recommended way to solve most linear systems of equations in MATLAB®. However, the function performs several checks on the input matrix to determine whether it has any special properties. If you know about the properties of the coefficient matrix ahead of time, then you can use linsolve to avoid time-consuming checks for large matrices.

Create a 10000-by-10000 magic square matrix and extract the lower triangular portion. Set the LT field of the opts structure to true to indicate that A is a lower triangular matrix.

Create a vector of ones for the right-hand side of the linear equation Ax = b . The number of rows in A and b must be equal.

Solve the linear system Ax = b using mldivide and time the calculation.

Now, solve the system again using linsolve . Specify the options structure so that linsolve can select an appropriate solver for a lower triangular matrix.

Compare the execution times to see how much faster linsolve is. As with any timing comparison, the results can vary between different computers and releases of MATLAB.

Suppress Matrix Condition Warnings

Solve a linear system using linsolve with two outputs to suppress matrix conditioning warnings.

Create a 20-by-20 Hilbert test matrix. This matrix is nearly singular, with the largest singular value being about 2e18 larger than the smallest.

Solve a linear system involving A with linsolve . Since A is nearly singular, linsolve returns a warning.

Now, solve the same linear system, but specify two outputs to linsolve . MATLAB® suppresses the warning, and the second output r contains the reciprocal condition number of A . You can use this syntax to handle ill-conditioned matrices with special cases in your code, without the code producing a warning.

Input Arguments

A — coefficient matrix matrix.

Coefficient matrix. A appears in the system of linear equations on the left as A X = B . The number of rows in A must equal the number of rows in B .

A cannot be sparse. To solve a linear system involving a sparse matrix, use mldivide or decomposition instead.

Data Types: single | double Complex Number Support: Yes

B — Input array vector | matrix

Input array, specified as a vector or matrix. B appears in the system of linear equations on the right as A X = B . If B is a matrix, then each column in the matrix represents a different vector for the right-hand side.

The number of rows in A must equal the number of rows in B .

opts — Coefficient matrix properties structure

Coefficient matrix properties, specified as a structure. Use this structure to specify properties of A that linsolve uses to select an appropriate solver for the linear system. The fields in the structure contain true / false values to indicate whether A has each property. By default all fields in the structure are assumed to be false . This table lists the possible fields in opts and their corresponding matrix properties.

Example: opts.UT = true specifies that A is upper triangular.

Example: opts.SYM = true, opts.POSDEF = true sets two fields to specify that A is symmetric and positive definite.

Valid Combinations

The rows of this table list all combinations of field values in opts that are valid for linsolve . Empty cells are the default value of false , and a true / false entry indicates that linsolve accepts either value.

Notes on Usage

If A has the properties in opts , then linsolve is faster compared to mldivide , because linsolve invokes the appropriate solver immediately and does not perform any tests to verify that A has the specified properties.

If A does not have the properties that you specify in opts , then linsolve returns incorrect results and does not always return an error message. Therefore, if you are unsure whether A has the specified properties, use mldivide or decomposition instead.

Data Types: struct

Output Arguments

X — linear system solution vector | matrix.

Linear system solution, returned as a vector or matrix that satisfies A X = B (or A T X = B if opts.TRANSA = true ). The size of X depends on whether opts.TRANSA = true :

If A is m -by- n and B is m -by- k , then X is n -by- k and is the solution to A X = B .

If opts.TRANSA = true , then A is m -by- n and B is n -by- k . In this case, X is m -by- k and is the solution to A T X = B .

r — Reciprocal condition number or rank scalar

Reciprocal condition number or rank, returned as a scalar.

If A is a square matrix, then r is the reciprocal condition number of A .

If A is a rectangular matrix, then r is the rank of A .

If opts is specified, then r is the reciprocal of the condition number of A unless RECT is true and both LT and UT are false , in which case, r gives the rank of A .

The speed benefit of linsolve can vary depending on the matrix structure and the relative optimization of the underlying algorithms. In some cases (such as with small matrices) there might not be any speed-up compared to mldivide . The speed benefit with linsolve arises by avoiding costly checks on the properties of large matrices, or by choosing an algorithm that is better suited to the input than the choice that mldivide makes.

Extended Capabilities

C/c++ code generation generate c and c++ code using matlab® coder™..

Usage notes and limitations:

The opts structure must be a constant scalar. Code generation does not support arrays of options structures.

Code generation only optimizes these cases:

UHESS = true (the TRANSA can be either true or false )

SYM = true and POSDEF = true

Other options are equivalent to using mldivide .

Code generation does not support sparse matrix inputs for this function.

GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Thread-based environment run code in the background using matlab® backgroundpool or accelerate code with parallel computing toolbox™ threadpool ..

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .

GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.

The two-output syntax [x,r] = linsolve(___) is not supported.

The MATLAB ® linsolve function prints a warning if A is badly scaled, nearly singular, or rank deficient. The gpuArray linsolve is unable to check for this condition. Take action to avoid this condition.

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox) .

Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox) .

Version History

Introduced before R2006a

mldivide | decomposition | lsqminnorm

  • Systems of Linear Equations

Commande MATLAB

Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :

Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

Help Center Help Center

  • Centro de ayuda
  • Actualizaciones de productos
  • Documentation

Solve System of Linear Equations

This section shows you how to solve a system of linear equations using the Symbolic Math Toolbox™.

Solve System of Linear Equations Using linsolve

Solve system of linear equations using solve.

A system of linear equations

a 11 x 1 + a 12 x 2 + … + a 1 n x n = b 1 a 21 x 1 + a 22 x 2 + … + a 2 n x n = b 2 ⋯ a m 1 x 1 + a m 2 x 2 + … + a m n x n = b m

can be represented as the matrix equation A ⋅ x → = b → , where A is the coefficient matrix,

A = ( a 11 … a 1 n ⋮ ⋱ ⋮ a m 1 ⋯ a m n )

and b → is the vector containing the right sides of equations,

b → = ( b 1 ⋮ b m )

If you do not have the system of linear equations in the form AX = B , use equationsToMatrix to convert the equations into this form. Consider the following system.

2 x + y + z = 2 − x + y − z = 3 x + 2 y + 3 z = − 10

Declare the system of equations.

Use equationsToMatrix to convert the equations into the form AX = B . The second input to equationsToMatrix specifies the independent variables in the equations.

Use linsolve to solve AX = B for the vector of unknowns X .

From X , x  = 3 , y  = 1 and z  = -5 .

Use solve instead of linsolve if you have the equations in the form of expressions and not a matrix of coefficients. Consider the same system of linear equations.

Solve the system of equations using solve . The inputs to solve are a vector of equations, and a vector of variables to solve the equations for.

solve returns the solutions in a structure array. To access the solutions, index into the array.

Related Topics

  • Solve Algebraic Equations
  • Solve System of Algebraic Equations

Comando de MATLAB

Ha hecho clic en un enlace que corresponde a este comando de MATLAB:

Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

Help Center Help Center

  • Help Center
  • Trial Software
  • Product Updates
  • Documentation

Solve Differential Equation

Solve a differential equation analytically by using the dsolve function, with or without initial conditions. To solve a system of differential equations, see Solve a System of Differential Equations .

First-Order Linear ODE

Solve differential equation with condition, nonlinear differential equation with initial condition, second-order ode with initial conditions, third-order ode with initial conditions, more ode examples.

Solve this differential equation.

d y d t = t y .

First, represent y by using syms to create the symbolic function y(t) .

Define the equation using == and represent differentiation using the diff function.

Solve the equation using dsolve .

In the previous solution, the constant C1 appears because no condition was specified. Solve the equation with the initial condition y(0) == 2 . The dsolve function finds a value of C1 that satisfies the condition.

If dsolve cannot solve your equation, then try solving the equation numerically. See Solve a Second-Order Differential Equation Numerically .

Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.

( d y d t + y ) 2 = 1 , y ( 0 ) = 0.

Solve this second-order differential equation with two initial conditions.

d 2 y d x 2 = cos ( 2 x ) − y , y ( 0 ) = 1 , y ' ( 0 ) = 0.

Define the equation and conditions. The second initial condition involves the first derivative of y . Represent the derivative by creating the symbolic function Dy = diff(y) and then define the condition using Dy(0)==0 .

Solve ode for y . Simplify the solution using the simplify function.

Solve this third-order differential equation with three initial conditions.

d 3 u d x 3 = u , u ( 0 ) = 1 ,   u ′ ( 0 ) = − 1 ,   u ′ ′ ( 0 ) = π .

Because the initial conditions contain the first- and second-order derivatives, create two symbolic functions, Du = diff(u,x) and D2u = diff(u,x,2) , to specify the initial conditions.

Create the equation and initial conditions, and solve it.

This table shows examples of differential equations and their Symbolic Math Toolbox™ syntax.

dsolve | odeFunction | odeToVectorField | reduceDifferentialOrder | daeFunction

Related Topics

  • Solve a System of Differential Equations
  • Solve a Second-Order Differential Equation Numerically
  • Solve Differential Algebraic Equations (DAEs)

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

COMMENTS

  1. Solve System of Linear Equations

    Solve System of Linear Equations Using solve. Use solve instead of linsolve if you have the equations in the form of expressions and not a matrix of coefficients. Consider the same system of linear equations. 2 x + y + z = 2 − x + y − z = 3 x + 2 y + 3 z = − 10. Declare the system of equations. syms x y z.

  2. System of Linear Equation in MATLAB

    We can use the Matlab built-in function solve() to solve the system of linear equations in Matlab. First of all, we can define the variables using the syms variable. After that, we can write the equations in Matlab. After that, we need to use the function solve() to solve the equations. For example, let's define some equations in Matlab and ...

  3. Matlab Tutorial

    Get more lessons like this at http://www.MathTutorDVD.comLearn how to solve systems of equations in matlab, specifically linear algebraic systems.

  4. Solution of system of linear equation in MATLAB

    Let us see how to solve a system of linear equations in MATLAB. Here are the various operators that we will be deploying to execute our task : \ operator : A \ B is the matrix division of A into B, which is roughly the same as INV(A) * B.If A is an NXN matrix and B is a column vector with N components or a matrix with several such columns, then X = A \ B is the solution to the equation A * X = B.

  5. How to solve linear equation in matlab

    Solve linear equation in matlab or solve system of linear equation in matlab using matlab symbolic variable is presented here in this video. The Systems of l...

  6. Solve System of Algebraic Equations

    Suppose you have the system. x 2 y 2 = 0 x - y 2 = α , and you want to solve for x and y. First, create the necessary symbolic objects. syms x y a. There are several ways to address the output of solve. One way is to use a two-output call. The call returns the following. [solx,soly] = solve(x^2*y^2 == 0, x-y/2 == a)

  7. solve system of linear equations in matlab

    You can use multiple calls of solve to get solutions for x1 and x2. In this problem you can solve the first equation for x1, and then plug that into the second equation to get x2 in terms of x3, x4, and x5.You can then substitute the new value of x2 back into your solution of x1.. The subs function is used to substitute the solved values back into the original equation.

  8. An Adaptive Orthogonal Basis Method for Computing Multiple ...

    This paper presents an innovative approach, the Adaptive Orthogonal Basis Method, tailored for computing multiple solutions to differential equations characterized by polynomial nonlinearities. Departing from conventional practices of predefining candidate basis pools, our novel method adaptively computes bases, considering the equation's nature and structural characteristics of7 the ...

  9. Solve linear system of equations

    Solve a linear system with both mldivide and linsolve to compare performance. mldivide is the recommended way to solve most linear systems of equations in MATLAB®. However, the function performs several checks on the input matrix to determine whether it has any special properties.

  10. Solve System of Linear Equations

    Solve System of Linear Equations Using solve. Use solve instead of linsolve if you have the equations in the form of expressions and not a matrix of coefficients. Consider the same system of linear equations. 2 x + y + z = 2 − x + y − z = 3 x + 2 y + 3 z = − 10. Declare the system of equations. syms x y z.

  11. Linear Algebra

    Linear Algebra. Linear algebra functions in MATLAB ® provide fast, numerically robust matrix calculations. Capabilities include a variety of matrix factorizations, linear equation solving, computation of eigenvalues or singular values, and more. For an introduction, see Matrices in the MATLAB Environment.

  12. Equation Solving

    For solving linear equations, use linsolve. These solver functions have the flexibility to handle complicated problems. ... Convert system of differential algebraic equations to MATLAB function handle suitable for ode15i: decic: Find consistent initial conditions for first-order implicit ODE system with algebraic constraints:

  13. Solve Differential Equation

    Solve Differential Equation. Solve a differential equation analytically by using the dsolve function, with or without initial conditions. To solve a system of differential equations, see Solve a System of Differential Equations. First-Order Linear ODE