Solve Linear Equations with Python

  Рет қаралды 106,658

APMonitor.com

APMonitor.com

9 жыл бұрын

Linear equations such as A*x=b are solved with NumPy in Python. This tutorial demonstrates how to create a matrix (A) and vector (b) as NumPy arrays and solve the set of equations with linalg.solve

Пікірлер: 86
@snow24121
@snow24121 2 жыл бұрын
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!
@9130cris
@9130cris 5 жыл бұрын
you dont know how much i love you
@apm
@apm 5 жыл бұрын
I'm glad it helped.
@oshanegray2834
@oshanegray2834 6 жыл бұрын
Thank you so much!
@AJ-et3vf
@AJ-et3vf 3 жыл бұрын
Very nice! Thank you!
@abdelrhmandameen2215
@abdelrhmandameen2215 3 жыл бұрын
Very satisfying thanks.
@araSalieri
@araSalieri 6 жыл бұрын
Thanks :)
@federicoalbesa3748
@federicoalbesa3748 3 жыл бұрын
Geniooo gracias!!!
@chonylau6981
@chonylau6981 4 жыл бұрын
Thanks!!!
@Raisan-II
@Raisan-II 5 жыл бұрын
Thank you
@kusumavenkatesh6592
@kusumavenkatesh6592 3 жыл бұрын
Thank you!!
@sedatdoganay4938
@sedatdoganay4938 5 жыл бұрын
thank you soo much
@rvilleg95
@rvilleg95 8 жыл бұрын
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 жыл бұрын
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.
@milkhunter467
@milkhunter467 4 жыл бұрын
Thanks Jim Halpert!
@apm
@apm 4 жыл бұрын
Thanks! Someone else just made that comparison this past week on another video. Maybe I could be his voice stunt double.
8 жыл бұрын
Nice, solved my problem! Thanks.
@apm
@apm 8 жыл бұрын
I'm glad that it helped. Thanks for the feedback.
@irreproachableincorrigable1378
@irreproachableincorrigable1378 6 жыл бұрын
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 6 жыл бұрын
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.
@Misseldine
@Misseldine 2 жыл бұрын
What screen capturing software did you use to make this video?
@apm
@apm 2 жыл бұрын
Here are some suggestions: kzbin.info/www/bejne/iWLEqqJno5x0qa8 I also like OBS Studio: obsproject.com/
@kennyPAGC
@kennyPAGC 6 жыл бұрын
is there a video where I can see the actual implementation of the equation solver?
@apm
@apm 6 жыл бұрын
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.
@basticorrea
@basticorrea 3 жыл бұрын
which way is efficienly better, this method or using Gauss-Jordan or LU?
@apm
@apm 3 жыл бұрын
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 жыл бұрын
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
@festeration8005
@festeration8005 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 4 жыл бұрын
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
@lululin1002
@lululin1002 Жыл бұрын
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.
@kennyPAGC
@kennyPAGC 6 жыл бұрын
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 6 жыл бұрын
I think you can feed either one to the linear solver - it analyzes the dimensions and transforms it to a 2x1 automatically.
@kd7799
@kd7799 4 жыл бұрын
@@apm no the outputs are different, I agree with kenny that this should be ([[-42],[2]])
@user-ns2bk9pz8s
@user-ns2bk9pz8s 5 ай бұрын
Sir why we got dot with numbers in the answer in numpy? Hope for getting the answer.
@apm
@apm 5 ай бұрын
The dots are there as a placeholder because it can’t display all of the numbers.
@weiminli2010
@weiminli2010 5 жыл бұрын
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.
@riviera443
@riviera443 6 жыл бұрын
Thank You, i need help on solving distance of shortest path for the traveler where the diagram is given..
@apm
@apm 6 жыл бұрын
+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 6 жыл бұрын
Thank You, i shall try and if i stumble, i shall again disturb you:-)
@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 жыл бұрын
don't forget the second set of square brackets when defining your A matrix.
@ibrogamingman8591
@ibrogamingman8591 7 жыл бұрын
Hi can you make a video on how to make a full-fledged math solving app
@apm
@apm 7 жыл бұрын
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?
@mccaffeina
@mccaffeina 8 жыл бұрын
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: kzbin.info/www/bejne/fHzampaQe5yrhKc
@gramps2225
@gramps2225 7 жыл бұрын
from math import sqrt sqrt(equation)
@wawankusuma3911
@wawankusuma3911 7 жыл бұрын
what do i do ?? if i have 3 variable in linier equations mr?? like simplex model ??
@apm
@apm 7 жыл бұрын
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 7 жыл бұрын
Can i use python to solve simplex metode to solve problem?
@wawankusuma3911
@wawankusuma3911 7 жыл бұрын
Maybe do you have a tutorial that has colum visual
@apm
@apm 7 жыл бұрын
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 7 жыл бұрын
Okey mr, thanks you for you help
@azeemmustafa7103
@azeemmustafa7103 4 жыл бұрын
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 4 жыл бұрын
You need to import numpy before you use the array function. Try "import numpy as np" at the top of your script.
@usmanfawad304
@usmanfawad304 4 жыл бұрын
What if the linear system has infinitely many solutions?
@apm
@apm 4 жыл бұрын
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).
@akambarsao8458
@akambarsao8458 2 жыл бұрын
Plz suggest how can i take user input
@apm
@apm 2 жыл бұрын
Something like x = float(input('What is a number? ')) is a way to input a user value.
@truclam2195
@truclam2195 3 жыл бұрын
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
@esti445
@esti445 3 жыл бұрын
Awesome! Why don't you write b as a column? I see you wrote b as a row
@apm
@apm 3 жыл бұрын
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!
@anot1259
@anot1259 5 жыл бұрын
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
@Road_Warrior81
@Road_Warrior81 4 жыл бұрын
How to check if this solution is correct or not?
@apm
@apm 4 жыл бұрын
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 4 жыл бұрын
APMonitor.com Yeah I know that but like how about writing a test piece of code to check it?
@chandancv6819
@chandancv6819 5 жыл бұрын
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 3 жыл бұрын
Bro did u get out put for this program PLZZ reply me
@tejak4996
@tejak4996 3 жыл бұрын
20+x=40 for this u want program PLZZ reply me sir plzzz
@apm
@apm 3 жыл бұрын
Here is example code that you can use to create the program: apmonitor.com/che263/index.php/Main/PythonSolveEquations
@tejak4996
@tejak4996 3 жыл бұрын
@@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 3 жыл бұрын
@@tejak4996 see example #2 at apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization You can leave the equation as x+20=40 and solve it.
@collegemathematics6698
@collegemathematics6698 3 жыл бұрын
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.
@shuha3216
@shuha3216 4 жыл бұрын
Thanks!
@tomgreg2008
@tomgreg2008 2 жыл бұрын
Thanks!
Solve Nonlinear Equations with Python
8:40
APMonitor.com
Рет қаралды 63 М.
Python Symbolic and Numeric Solutions
16:15
APMonitor.com
Рет қаралды 16 М.
Can You Draw A PERFECTLY Dotted Circle?
00:55
Stokes Twins
Рет қаралды 45 МЛН
1 or 2?🐄
00:12
Kan Andrey
Рет қаралды 58 МЛН
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 16 МЛН
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 58 МЛН
Python Tutorial: Learn Scipy - Linear Algebra linalg() in 10 Minutes
10:45
eMaster Class Academy
Рет қаралды 9 М.
Arrays in Python / Numpy
11:38
APMonitor.com
Рет қаралды 210 М.
Linear Algebra - Matrix Operations
7:08
Postcard Professor
Рет қаралды 389 М.
Eigenstates of ANY 1D Potential in PYTHON
19:41
Mr. P Solver
Рет қаралды 35 М.
Solve Linear Equation Using NumPy | Linear Algebra | Python Tutorials
7:56
Symbolic Manipulation in Python
28:18
APMonitor.com
Рет қаралды 63 М.
Gaussian Elimination & Row Echelon Form
18:40
The Organic Chemistry Tutor
Рет қаралды 2,5 МЛН
SciPy Beginner's Guide for Optimization
11:03
APMonitor.com
Рет қаралды 294 М.
😱Хакер взломал зашифрованный ноутбук.
0:54
Последний Оплот Безопасности
Рет қаралды 757 М.
Samsung Galaxy Unpacked July 2024: Official Replay
1:8:53
Samsung
Рет қаралды 23 МЛН
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 5 МЛН
Красиво, но телефон жаль
0:32
Бесполезные Новости
Рет қаралды 987 М.
Сколько реально стоит ПК Величайшего?
0:37
Телефон-електрошокер
0:43
RICARDO 2.0
Рет қаралды 382 М.