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