Solve Linear Equations with Python

Поділитися
Вставка
  • Опубліковано 3 січ 2025

КОМЕНТАРІ • 86

  • @9130cris
    @9130cris 5 років тому +9

    you dont know how much i love you

    • @apm
      @apm  5 років тому +2

      I'm glad it helped.

  • @snow24121
    @snow24121 3 роки тому +3

    Awesome stuff! Thank you very much. I had one of those for an assignment and solved it by hand in like 10 seconds, but I had no Idea how to do it with the proper syntax with NumPy. Thanks a lot!

  • @rvilleg95
    @rvilleg95 8 років тому +2

    how does that b matrix works. When we introduce the arguments for the array using the np.array([ ]) the inner [ ] works as a column? but when we are introducing the coefficients using np.array([ [ ], [ ] ]) each inner [ ] works as a row but the outer as...a column as well? Or does Python knows how to handle that when we use the linalg.solve command?

    • @apm
      @apm  8 років тому +3

      Rafael, you bring up a good point. Technically "b" should be a column vector. Numpy developers probably knew that some programmers would like to give the function either a row or column vector and have it figure it out. Try the following Python code:
      import numpy as np
      A = np.array([[3,-9],[2,4]])
      print('row vector b1')
      b1 = np.array([-42,2])
      z1 = np.linalg.solve(A,b1)
      print(z1)
      print('column vector b2')
      b2 = np.array([[-42],[2]])
      z2 = np.linalg.solve(A,b2)
      print(z2)
      This produces the output:
      row vector b1
      [-5. 3.]
      column vector b2
      [[-5.]
      [ 3.]]
      You'll notice that np.linalg.solve returns the same type of vector that was given to it for the b vector.

    • @rvilleg95
      @rvilleg95 8 років тому

      Yes, I see that works. There is also the matrix function for numpy and works as well.
      But as you say, the developers probably made it that flexible.
      great videos.

  • @Misseldine
    @Misseldine 3 роки тому +2

    What screen capturing software did you use to make this video?

    • @apm
      @apm  3 роки тому

      Here are some suggestions: ua-cam.com/video/S1ayo1n7AsM/v-deo.html I also like OBS Studio: obsproject.com/

  • @festeration8005
    @festeration8005 5 років тому +4

    please, if we have 3 equations, and we know that for exemple z=0, how can we force him to solve the problem with z=0 thx

    • @apm
      @apm  5 років тому +1

      You can add another row to your linear equations with the equation z=0. If you need optimization then look here: apmonitor.com/che263/index.php/Main/PythonOptimization

  • @basticorrea
    @basticorrea 3 роки тому +1

    which way is efficienly better, this method or using Gauss-Jordan or LU?

    • @apm
      @apm  3 роки тому +1

      Here is something that may help: stackoverflow.com/questions/34951048/gauss-jordan-elimination-versus-lu-decomposition#:~:text=2%20Answers&text=The%20advantages%20of%20using%20an,reused%20to%20compute%20multiple%20solutions.&text=The%20reason%20this%20is%20faster,O(n%5E2).

  • @Alex-sv5li
    @Alex-sv5li 5 років тому +2

    can you help when the input and has to be given in a input.txt file and output printed to output.txt file?

    • @apm
      @apm  5 років тому

      Check out these other tutorials on importing and exporting data or text files: apmonitor.com/che263/index.php/Main/PythonDataAnalysis

  • @kennyPAGC
    @kennyPAGC 7 років тому +3

    is there a video where I can see the actual implementation of the equation solver?

    • @apm
      @apm  7 років тому +2

      There is a reference in the documentation: docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html Please see:
      G. Strang, Linear Algebra and Its Applications, 2nd Ed., Orlando, FL, Academic Press, Inc., 1980, pg. 22.

  • @irreproachableincorrigable1378
    @irreproachableincorrigable1378 7 років тому

    Hi,
    you are supposed to use inverse of A which is obtained by 'np.linalg.inv(A)' . But rather that this, u directly multiplied A and b to find z with the method 'np.linalg.solve(A,b)'

    • @apm
      @apm  7 років тому +1

      That is correct. The np.linalg.solve function produces a solution to A*x=b that is x = A^(-1) * b. You can also get an equivalent result by using np.dot(np.linalg.inv(A),b) that is multiplying the inverse of A with the vector b.

  • @kennyPAGC
    @kennyPAGC 7 років тому +3

    I don't understand why b = np.array([-42, 2]) instead of np.array([[-42], [2]]). Basically I'm asking why is b a 1x2 vector rather than a 2x1 one?

    • @apm
      @apm  7 років тому +2

      I think you can feed either one to the linear solver - it analyzes the dimensions and transforms it to a 2x1 automatically.

    • @kd7799
      @kd7799 5 років тому +2

      @@apm no the outputs are different, I agree with kenny that this should be ([[-42],[2]])

  • @abdelrhmandameen2215
    @abdelrhmandameen2215 4 роки тому +2

    Very satisfying thanks.

  • @HeenaSammi
    @HeenaSammi 11 місяців тому +1

    Sir why we got dot with numbers in the answer in numpy? Hope for getting the answer.

    • @apm
      @apm  11 місяців тому

      The dots are there as a placeholder because it can’t display all of the numbers.

  • @Yxlin1002
    @Yxlin1002 Рік тому +1

    Thank you so much! It really solved my problem. I have a small question for the answer array [6,1,1], it is a 3 rows one column, why it is not [[6],[1],[1]]?

    • @apm
      @apm  Рік тому

      The package is able to accept a row vector. I understands that it needs to be transposed and does it automatically in the package.

  • @weiminli2010
    @weiminli2010 5 років тому +2

    Why it complains that raise LinAlgError("Singular matrix")

    • @apm
      @apm  5 років тому

      There is no unique solution to your system of equations because the A matrix is not invertible.

  • @truclam2195
    @truclam2195 3 роки тому +1

    How can I solve 5 equations but only 4 unknowns?

    • @apm
      @apm  3 роки тому

      This is possible if there is a redundant equation or else if there are inequality constraints. Here is additional information on solving linear equations apmonitor.com/che263/index.php/Main/PythonSolveEquations and github.com/APMonitor/data_science/blob/master/10.%20Solve_Equations.ipynb and apmonitor.com/pdc/index.php/Main/LinearProgramming

  • @milkhunter467
    @milkhunter467 4 роки тому +1

    Thanks Jim Halpert!

    • @apm
      @apm  4 роки тому +1

      Thanks! Someone else just made that comparison this past week on another video. Maybe I could be his voice stunt double.

  • @mccaffeina
    @mccaffeina 8 років тому +1

    Hello, how can I find all roots of 2 equations in Python without using modules. I am using Python 3.5.1 and modules doesnt work in my python. Thank

    • @apm
      @apm  8 років тому

      +Edvardas Valiauga please go through this video to help you install the Python modules. Once you have the modules, it is an easy task: ua-cam.com/video/FKwicZF7xNE/v-deo.html

    • @gramps2225
      @gramps2225 8 років тому

      from math import sqrt
      sqrt(equation)

  •  8 років тому +1

    Nice, solved my problem! Thanks.

    • @apm
      @apm  8 років тому +2

      I'm glad that it helped. Thanks for the feedback.

  • @azeemmustafa7103
    @azeemmustafa7103 5 років тому +1

    I got an error as
    NameError Traceback (most recent call last)
    in
    ----> 1 A = np.array
    NameError: name 'np' is not defined
    Please tell why I get this error.

    • @apm
      @apm  5 років тому +1

      You need to import numpy before you use the array function. Try "import numpy as np" at the top of your script.

  • @usmanfawad304
    @usmanfawad304 5 років тому +3

    What if the linear system has infinitely many solutions?

    • @apm
      @apm  5 років тому +2

      A numerical solution will only return one solution but they may be different based on the starting guess value if you use a solver. If you use the matrix inversion method then the solver will typically report that the "A" matrix is rank deficient because it lacks sufficient independent equations (rows).

  • @esti445
    @esti445 3 роки тому +1

    Awesome!
    Why don't you write b as a column? I see you wrote b as a row

    • @apm
      @apm  3 роки тому +1

      The package accepts either. It understands that a row vector would give an error so it automatically treats it as a column vector.

    • @esti445
      @esti445 3 роки тому

      @@apm great thanks!

  • @Road_Warrior81
    @Road_Warrior81 5 років тому +1

    How to check if this solution is correct or not?

    • @apm
      @apm  5 років тому

      If it is a linear set of equations with A x = b then you can evaluate A x - b and it should be a zero vector.

    • @Road_Warrior81
      @Road_Warrior81 5 років тому

      APMonitor.com Yeah I know that but like how about writing a test piece of code to check it?

  • @akambarsao8458
    @akambarsao8458 3 роки тому +1

    Plz suggest how can i take user input

    • @apm
      @apm  3 роки тому

      Something like x = float(input('What is a number? ')) is a way to input a user value.

  • @Tmac2016
    @Tmac2016 7 років тому

    i have a problem, but it kept saying "LinAlgerror: 1-dimensional array give. Array must be atleast two-dimensional". please help me fix this

    • @apm
      @apm  7 років тому +1

      don't forget the second set of square brackets when defining your A matrix.

  • @wawankusuma3911
    @wawankusuma3911 8 років тому

    what do i do ?? if i have 3 variable in linier equations mr?? like simplex model ??

    • @apm
      @apm  8 років тому

      If you have more variables than equations (and an objective function) then this would certainly be an optimization problem that can be solved with methods such as Simplex or Interior Point methods. I have a course on optimization methods here: apmonitor.com/me575/ or a simple optimization problem here: apmonitor.com/che263/index.php/Main/PythonOptimization

    • @wawankusuma3911
      @wawankusuma3911 8 років тому

      Can i use python to solve simplex metode to solve problem?

    • @wawankusuma3911
      @wawankusuma3911 8 років тому

      Maybe do you have a tutorial that has colum visual

    • @apm
      @apm  8 років тому

      I don't have a tutorial specifically on the simplex method but I do have a few on Interior Point (apmonitor.com/me575/index.php/Main/InteriorPointMethod) and Quasi-Newton methods (apmonitor.com/me575/index.php/Main/QuasiNewton). They both use the KKT conditions to find an optimal solution, even for nonlinear problems (apmonitor.com/me575/index.php/Main/KuhnTucker).

    • @wawankusuma3911
      @wawankusuma3911 8 років тому

      Okey mr, thanks you for you help

  • @AJ-et3vf
    @AJ-et3vf 3 роки тому +1

    Very nice! Thank you!

  • @araSalieri
    @araSalieri 6 років тому +5

    Thanks :)

  • @federicoalbesa3748
    @federicoalbesa3748 4 роки тому +1

    Geniooo gracias!!!

  • @oshanegray2834
    @oshanegray2834 7 років тому +3

    Thank you so much!

  • @ibrogamingman8591
    @ibrogamingman8591 7 років тому +2

    Hi can you make a video on how to make a full-fledged math solving app

    • @apm
      @apm  7 років тому +2

      IBRO GAMING MAN, Numpy and Scipy are probably the most full-featured options. An online version of Python is available through repl.it or try.jupyter.org. Lots of other online applications already exist. Have you also tried Wolfram alpha?

  • @anot1259
    @anot1259 5 років тому +1

    hi how l input from the user
    Ask the user to input nxn numbers separated by comma. This will be the z matrix.
    Enter matrix z: 6, 3, 9, -2, 3, 8, -1, -4, 5

    • @apm
      @apm  5 років тому

      input_string = input("Enter a list element separated by space ")

    • @apm
      @apm  5 років тому

      list = input_string.split()

    • @apm
      @apm  5 років тому

      If you need nxn numbers then I'd recommend that you read in one row at a time with a loop.

    • @anot1259
      @anot1259 5 років тому

      @@apm thank you so much

  • @Raisan-II
    @Raisan-II 5 років тому +2

    Thank you

  • @riviera443
    @riviera443 7 років тому

    Thank You, i need help on solving distance of shortest path for the traveler where the diagram is given..

    • @apm
      @apm  7 років тому

      +Saptarshi Dutta, you need a branch and bound algorithm or another one that will solve the TSP: en.m.wikipedia.org/wiki/Travelling_salesman_problem

    • @riviera443
      @riviera443 7 років тому

      Thank You, i shall try and if i stumble, i shall again disturb you:-)

  • @chandancv6819
    @chandancv6819 5 років тому +1

    how to solve 3x - 9 = 0 in python??

    • @apm
      @apm  5 років тому

      Please see source code for problem #2 here: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization

    • @tejak4996
      @tejak4996 4 роки тому

      Bro did u get out put for this program PLZZ reply me

  • @chonylau6981
    @chonylau6981 5 років тому +2

    Thanks!!!

  • @tejak4996
    @tejak4996 4 роки тому +1

    20+x=40 for this u want program PLZZ reply me sir plzzz

    • @apm
      @apm  4 роки тому

      Here is example code that you can use to create the program: apmonitor.com/che263/index.php/Main/PythonSolveEquations

    • @tejak4996
      @tejak4996 4 роки тому +1

      @@apm I don't understand.... First I want to change the value into x=40-20=20 like that or else .... Plzzz give me any idea about this

    • @apm
      @apm  4 роки тому

      @@tejak4996 see example #2 at apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization You can leave the equation as x+20=40 and solve it.

  • @Kusumavenkatesh_kv
    @Kusumavenkatesh_kv 3 роки тому +1

    Thank you!!

  • @shuha3216
    @shuha3216 4 роки тому +1

    Thanks!

  • @sedatdoganay4938
    @sedatdoganay4938 5 років тому +1

    thank you soo much

  • @collegemathematics6698
    @collegemathematics6698 3 роки тому +1

    Matlab is much easier

    • @apm
      @apm  3 роки тому

      Here is a comparison of languages: apmonitor.com/che263/index.php/Main/CompareMatlabPythonMathcad Matlab is easy but lacks some of the packages in Python.

  • @tomgreg2008
    @tomgreg2008 3 роки тому +1

    Thanks!