Can anyone tell me the python code to solve the equation:
2w + x + 4y + 3z = 5
w - 2x + 3z = 3
3w + 2x - y + z = -1
4x - 5z = -3
I have the following code but it isn't working:
A2 = np.array([[2,1,4,3],[1,-2,3],[3,2,-1, 1],[4, -5]])
b2 = np.array([[5,3,-1, -3]]).T
print('A\n',A2)
print('b\n',b2)
v2 = np.linalg.solve(A2,b2)
print('v')
print(v2)
The problem is how you formatted the missing variables for the equations, remember that you should use 0 instead of nothing, otherwise the arrays (equations) get misinterpreted and provide you a wrong answer/error:
This should work for you now:
import numpy as np
A = [[2,1,4,3],[1,-2,0,3],[3,2,-1,1],[0,4,0,5]]
Y = [5,3,-1,3]
res = np.linalg.inv(A).dot(Y)
print(res)
Output:
[-0.15384615 -0.30769231 0.76923077 0.84615385]
An alternative approach uses sympy, Python's symbolic mathematics package:
from sympy import Eq, solve
from sympy.abc import w, x, y, z
sol = solve([ Eq(2*w + x + 4*y + 3*z, 5),
Eq(w - 2*x + 3*z, 3),
Eq(3*w + 2*x - y + z, -1),
Eq(4*x - 5*z, -3) ])
print(sol)
print({ s:sol[s].evalf() for s in sol })
This prints:
{w: 94/45, x: -20/9, y: 74/45, z: -53/45}
{w: 2.08888888888889, x: -2.22222222222222, y: 1.64444444444444, z: -1.17777777777778}
It is even possible to directly take the string input and find a solution:
from sympy import Eq, solve
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application
eqs = ['2w + x + 4y + 3z = 5',
'w - 2x + 3z = 3',
'3w + 2x - y + z = -1',
'4x - 5z = -3']
transformations=(standard_transformations + (implicit_multiplication_application,))
eqs_sympy = [Eq(parse_expr(e.split('=')[0], transformations=transformations),
parse_expr(e.split('=')[1], transformations=transformations))
for e in eqs]
sol = solve(eqs_sympy)
print(sol)
Related
During the least squares computation:
x = N(-1) * At * Q(-1) * (yo -b)
Instead of doing:
Xnew = Xold + x[0]
Ynew = Yold + x[1]
In order to obtain the convergence I have to change the '+' into a '-':
Xnew = Xold - x[0]
Ynew = Yold - x[1]
Does anyone know the reason? I am doing something wrong?
Here is my Python Code:
import numpy as np
x_m = np.dot(np.linalg.pinv(N_matrix),A_matrix.T)
x_mat = np.dot(corr_m,np.linalg.pinv(Q_matrix))
x_matrix = np.dot(corr_mat,delta_y)
xnew.iloc[0,0] = xold.iloc[0,0] - x_matrix[0]
xnew.iloc[0,1] = xold.iloc[0,1] - x_matrix[1]
I'm a newbie learning python. I have a question, can you guys help me? This is my code:
from sympy import *
def test(f, g, a):
f1 = f.subs(x, g)
df1 = diff(f1, x).subs(x, a)
return df1
print(test((2*(x**2) + abs(x + 1)), (x - 1), -1))
Result: -Subs(Derivative(re(x), x), x, -1) - 8
I'm taking the derivative of f(g(x)) with: f = 2(x^2) + abs(x + 1), g = x - 1 and x = -1. When I use diff to calculate the result is -Subs(Derivative(re(x), x), x, -1) - 8, but when I use the formula lim x->x0 (f(x) - f(x0))/(x - x0) I got result is -9. I also tried using a calculator to calculate and the result -9 is the correct result. Is there a way to make diff return -9? Anyone have any help or can give some pointers?
Thanks!
Whenever I see a re or im appear when I didn't expect them, I am inclined to make the symbols real:
>>> from sympy import *
>>> def test(f, g, a):
... f1 = f.subs(x, g)
... df1 = diff(f1, x).subs(x, a)
... return df1
...
>>> var('x',real=True)
x
>>> print(test((2*(x**2) + abs(x + 1)), (x - 1), -1))
-9
Since I'm still a relative beginner to sympy I like to view intermediate results (I even like to do that with numpy which I know much better). Running in isympy:
In [6]: diff(f1,x)
Out[6]:
⎛ d d ⎞
⎜re(x)⋅──(re(x)) + im(x)⋅──(im(x))⎟⋅sign(x)
⎝ dx dx ⎠
4⋅x - 4 + ───────────────────────────────────────────
x
That expression contains unevaluate d/dx and the distinction between the real and imaginary parts of x.
Restricting x to real as suggested in the other answer produces:
In [19]: diff(exp,x)
Out[19]: 4⋅x + sign(x + 1)
If you look at this example:
2Bsin(x)+ (B + ¾A)*cos(x) = sin(x) + 2cos(x)
it’s easy to see that
2B = 1, B + ¾A = 2
and applying some basic linear algebra
B = ½, A = 2
In Python, using sympy, however, you run this code:
from sympy import *; var('x A B')
P1 = (B/2)*sin(x) + (B + 3*A/4)*cos(x)
P2 = sin(x) + 2*cos(x)
solve(Eq(P1, P2), [A,B])
you get this
[(-2*B*tan(x)/3 - 4*B/3 + 4*tan(x)/3 + 4/3, B)]
Is there a way to get the result in terms of A and B?
It seems like a little bit of a hack but works. I substitute sin(x) and cos(x) by x and y and turn it into a polynomial. Then I can just get the coefficients, make an equation out of those and solve it just fine.
from sympy import *; var('x y A B')
P1 = (B/2)*sin(x) + (B + 3*A/4)*cos(x)
P2 = sin(x) + 2*cos(x)
P1s = P1.subs({sin(x):x, cos(x):y})
P2s = P2.subs({sin(x):x, cos(x):y})
eqs = tuple(map(lambda x:Eq(*x),zip(Poly(P1s,[x,y]).coeffs(), Poly(P2s,[x,y]).coeffs())))
Those sympy does solve
sol = solve(eqs)
{A: 0, B: 2}
And I can even put those into the original equation to see if something weird happened:
P1.subs(sol), P2.subs(sol)
(sin(x) + 2*cos(x), sin(x) + 2*cos(x))
I'm triying to make a double reimann sum, with a limit b = (x^2 + y^2 = 16), the problem is when I use Sympy and marks as a TypeError in the linespace column, I tried to def the ecuation but nothing works, I'm doing somethin wrong or should I change something?
import numpy as np
import matplotlib.pyplot as plt
import sympy
# function to integrate = x +3*y + 1
# Limit funtion 'b' =(x^2 + y^2 = 16)
Width=15; Length=20;
x = sympy.Symbol('x')
a = 0
b =(sympy.sqrt(16-x**2))
c = 0
d = 3
#Heigth of x
deltax= (b - a) / Width
#Heigth of y
deltay = (d - c) / Length
#Area of each square
dA = deltax * deltay
x = np.linspace((a, b - deltax, Width));
y = np.linspace((c, d - deltay, Length));
f = lambda x,y: x +3*y + 1
[X, Y] = np.meshgrid(x, y);
#reimann sum
Suma=sum(dA * f(X, Y))
Suma = sum(Suma)
int(Suma)
print(Suma)
I would like to compute the integral of discrete signal segment Y (Y=[y1,y2,...y50]) and X is the following datetime64[ns] object:
x=['2018-01-24T13:41:25.057000000' '2018-01-24T13:41:25.069000000'
'2018-01-24T13:41:25.077000000' '2018-01-24T13:41:25.090000000'
'2018-01-24T13:41:25.097000000' '2018-01-24T13:41:25.111000000'
'2018-01-24T13:41:25.117000000' '2018-01-24T13:41:25.130000000'
'2018-01-24T13:41:25.138000000' '2018-01-24T13:41:25.150000000'
'2018-01-24T13:41:25.158000000' '2018-01-24T13:41:25.170000000'
'2018-01-24T13:41:25.178000000' '2018-01-24T13:41:25.199000000'
'2018-01-24T13:41:25.200000000' '2018-01-24T13:41:25.211000000'
'2018-01-24T13:41:25.218000000' '2018-01-24T13:41:25.231000000'
'2018-01-24T13:41:25.238000000' '2018-01-24T13:41:25.250000000'
'2018-01-24T13:41:25.258000000' '2018-01-24T13:41:25.269000000'
'2018-01-24T13:41:25.278000000' '2018-01-24T13:41:25.290000000'
'2018-01-24T13:41:25.298000000' '2018-01-24T13:41:25.311000000'
'2018-01-24T13:41:25.317000000' '2018-01-24T13:41:25.331000000'
'2018-01-24T13:41:25.338000000' '2018-01-24T13:41:25.350000000'
'2018-01-24T13:41:25.358000000' '2018-01-24T13:41:25.370000000'
'2018-01-24T13:41:25.378000000' '2018-01-24T13:41:25.390000000'
'2018-01-24T13:41:25.398000000' '2018-01-24T13:41:25.411000000'
'2018-01-24T13:41:25.418000000' '2018-01-24T13:41:25.430000000'
'2018-01-24T13:41:25.437000000' '2018-01-24T13:41:25.450000000'
'2018-01-24T13:41:25.469000000' '2018-01-24T13:41:25.469000000'
'2018-01-24T13:41:25.479000000' '2018-01-24T13:41:25.493000000'
'2018-01-24T13:41:25.502000000' '2018-01-24T13:41:25.510000000'
'2018-01-24T13:41:25.517000000' '2018-01-24T13:41:25.530000000'
'2018-01-24T13:41:25.537000000' '2018-01-24T13:41:25.550000000']
My attempt is using the following python code:
Case A, with the above x:
f_x_tot_acc = Y
x_n = x[-1]
x_0 = x[0]
h = (x_n - x_0) / (len(f_x_tot_acc) - 1)
print('trapz [' + str(integrate.trapz(f_x_tot_acc, dx=h)) + ']')
The Result is: trapz [6203255447 nanoseconds]
Case B,
x = range(0,50)
f_x_tot_acc = Y
x_n = x[-1]
x_0 = x[0]
h = (x_n - x_0) / (len(f_x_tot_acc) - 1)
print('trapz [' + str(integrate.trapz(f_x_tot_acc, dx=h)) + ']')
The Result is: trapz [616.5507767408332].
Which is the correct one?
Many Thanks in advance,
Best Regards,
Carlo