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
@snow241212 жыл бұрын
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!
@9130cris5 жыл бұрын
you dont know how much i love you
@apm5 жыл бұрын
I'm glad it helped.
@oshanegray28346 жыл бұрын
Thank you so much!
@AJ-et3vf3 жыл бұрын
Very nice! Thank you!
@abdelrhmandameen22153 жыл бұрын
Very satisfying thanks.
@araSalieri6 жыл бұрын
Thanks :)
@federicoalbesa37483 жыл бұрын
Geniooo gracias!!!
@chonylau69814 жыл бұрын
Thanks!!!
@Raisan-II5 жыл бұрын
Thank you
@kusumavenkatesh65923 жыл бұрын
Thank you!!
@sedatdoganay49385 жыл бұрын
thank you soo much
@rvilleg958 жыл бұрын
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?
@apm8 жыл бұрын
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.
@rvilleg958 жыл бұрын
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.
@milkhunter4674 жыл бұрын
Thanks Jim Halpert!
@apm4 жыл бұрын
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.
@apm8 жыл бұрын
I'm glad that it helped. Thanks for the feedback.
@irreproachableincorrigable13786 жыл бұрын
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)'
@apm6 жыл бұрын
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.
@Misseldine2 жыл бұрын
What screen capturing software did you use to make this video?
@apm2 жыл бұрын
Here are some suggestions: kzbin.info/www/bejne/iWLEqqJno5x0qa8 I also like OBS Studio: obsproject.com/
@kennyPAGC6 жыл бұрын
is there a video where I can see the actual implementation of the equation solver?
@apm6 жыл бұрын
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.
@basticorrea3 жыл бұрын
which way is efficienly better, this method or using Gauss-Jordan or LU?
@apm3 жыл бұрын
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-sv5li5 жыл бұрын
can you help when the input and has to be given in a input.txt file and output printed to output.txt file?
@apm5 жыл бұрын
Check out these other tutorials on importing and exporting data or text files: apmonitor.com/che263/index.php/Main/PythonDataAnalysis
@festeration80054 жыл бұрын
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
@apm4 жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
The package is able to accept a row vector. I understands that it needs to be transposed and does it automatically in the package.
@kennyPAGC6 жыл бұрын
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?
@apm6 жыл бұрын
I think you can feed either one to the linear solver - it analyzes the dimensions and transforms it to a 2x1 automatically.
@kd77994 жыл бұрын
@@apm no the outputs are different, I agree with kenny that this should be ([[-42],[2]])
@user-ns2bk9pz8s5 ай бұрын
Sir why we got dot with numbers in the answer in numpy? Hope for getting the answer.
@apm5 ай бұрын
The dots are there as a placeholder because it can’t display all of the numbers.
@weiminli20105 жыл бұрын
Why it complains that raise LinAlgError("Singular matrix")
@apm5 жыл бұрын
There is no unique solution to your system of equations because the A matrix is not invertible.
@riviera4436 жыл бұрын
Thank You, i need help on solving distance of shortest path for the traveler where the diagram is given..
@apm6 жыл бұрын
+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
@riviera4436 жыл бұрын
Thank You, i shall try and if i stumble, i shall again disturb you:-)
@Tmac20167 жыл бұрын
i have a problem, but it kept saying "LinAlgerror: 1-dimensional array give. Array must be atleast two-dimensional". please help me fix this
@apm7 жыл бұрын
don't forget the second set of square brackets when defining your A matrix.
@ibrogamingman85917 жыл бұрын
Hi can you make a video on how to make a full-fledged math solving app
@apm7 жыл бұрын
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?
@mccaffeina8 жыл бұрын
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
@apm8 жыл бұрын
+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
@gramps22257 жыл бұрын
from math import sqrt sqrt(equation)
@wawankusuma39117 жыл бұрын
what do i do ?? if i have 3 variable in linier equations mr?? like simplex model ??
@apm7 жыл бұрын
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
@wawankusuma39117 жыл бұрын
Can i use python to solve simplex metode to solve problem?
@wawankusuma39117 жыл бұрын
Maybe do you have a tutorial that has colum visual
@apm7 жыл бұрын
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).
@wawankusuma39117 жыл бұрын
Okey mr, thanks you for you help
@azeemmustafa71034 жыл бұрын
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.
@apm4 жыл бұрын
You need to import numpy before you use the array function. Try "import numpy as np" at the top of your script.
@usmanfawad3044 жыл бұрын
What if the linear system has infinitely many solutions?
@apm4 жыл бұрын
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).
@akambarsao84582 жыл бұрын
Plz suggest how can i take user input
@apm2 жыл бұрын
Something like x = float(input('What is a number? ')) is a way to input a user value.
@truclam21953 жыл бұрын
How can I solve 5 equations but only 4 unknowns?
@apm3 жыл бұрын
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
@esti4453 жыл бұрын
Awesome! Why don't you write b as a column? I see you wrote b as a row
@apm3 жыл бұрын
The package accepts either. It understands that a row vector would give an error so it automatically treats it as a column vector.
@esti4453 жыл бұрын
@@apm great thanks!
@anot12595 жыл бұрын
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
@apm5 жыл бұрын
input_string = input("Enter a list element separated by space ")
@apm5 жыл бұрын
list = input_string.split()
@apm5 жыл бұрын
If you need nxn numbers then I'd recommend that you read in one row at a time with a loop.
@anot12595 жыл бұрын
@@apm thank you so much
@Road_Warrior814 жыл бұрын
How to check if this solution is correct or not?
@apm4 жыл бұрын
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_Warrior814 жыл бұрын
APMonitor.com Yeah I know that but like how about writing a test piece of code to check it?
@chandancv68195 жыл бұрын
how to solve 3x - 9 = 0 in python??
@apm5 жыл бұрын
Please see source code for problem #2 here: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization
@tejak49963 жыл бұрын
Bro did u get out put for this program PLZZ reply me
@tejak49963 жыл бұрын
20+x=40 for this u want program PLZZ reply me sir plzzz
@apm3 жыл бұрын
Here is example code that you can use to create the program: apmonitor.com/che263/index.php/Main/PythonSolveEquations
@tejak49963 жыл бұрын
@@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
@apm3 жыл бұрын
@@tejak4996 see example #2 at apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization You can leave the equation as x+20=40 and solve it.
@collegemathematics66983 жыл бұрын
Matlab is much easier
@apm3 жыл бұрын
Here is a comparison of languages: apmonitor.com/che263/index.php/Main/CompareMatlabPythonMathcad Matlab is easy but lacks some of the packages in Python.