I have the following function
u := proc (x, t) options operator, arrow;
50+sum((100*n*Pi*sin(n*Pi)+100*cos(n*Pi)-100)*cos((1/10)*n*Pi*x)*exp(-(1/100)*n^2*Pi^2*t)/(n^2*Pi^2), n = 1 .. 20)
end proc;
plot3d(u(x, t), x = 0 .. 10, t = 0 .. 20)
I am trying to plot it in Maple, but is not giving me the right 3D shape.
restart:
u := proc (x, t) options operator, arrow;
50+sum((100*n*Pi*sin(n*Pi)+100*cos(n*Pi)-100)*cos((1/10)*n*Pi*x)*exp(-(1/100)*n^2*Pi^2*t)/(n^2*Pi^2), n = 1 .. 20)
end proc:
plot3d(u(x, t), x = 0 .. 10, t = 0 .. 20);
plot3d(diff(u(x, t),x), x = 0 .. 10, t = 0 .. 20);
Related
I am a little bit puzzled. I wanted to check the christoffelsymbol calculation and Riemann Tensor calculation by using the Schwarzschildmetric. But I do not get the expected results. Where is my error - or does sympy miss something? I miss Gamma_t_tr and Gamma_r_rr and Gamma_r_tt.
from sympy.diffgeom import Manifold, Patch, CoordSystem, TensorProduct, metric_to_Riemann_components, metric_to_Christoffel_1st, metric_to_Christoffel_2nd
import sympy as sym
from sympy import sin,cos,sinh,cosh, acos, atan2, exp, asin, acot
sym.init_printing(num_columns=200)
TP = TensorProduct
t,x,y,z = sym.symbols("t x y z")
tau, r, theta, phi = sym.symbols("tau r theta phi")
rs, M = sym.symbols("rs M")
m = Manifold("M",4)
p = Patch("P",m)
term_r = 1 - 2*M/r
relation_dict = {
('cartesian', 'schwarz'): [(t, x, y, z), (t, (x**2 + y**2 + z**2)**(0.5), atan2(y,x), acos(z/(x*x+y*y+z*z)**0.5) )],
('schwarz', 'cartesian'): [(tau, r, phi, theta), (tau, r*cos(phi)*sin(theta), r*sin(phi)*sin(theta), r*cos(theta))]
}
cartesian = CoordSystem('cartesian', p, (t, x, y, z), relation_dict)
schwarz = CoordSystem('schwarz', p, (tau, r, phi, theta), relation_dict)
tau, r, phi, theta = schwarz.coord_functions()
g00 = -term_r
g01 = 0
g02 = 0
g03 = 0
g10 = 0
g11 = 1/term_r
g12 = 0
g13 = 0
g20 = 0
g21 = 0
g22 = r**2*sin(theta)**2
g23 = 0
g30 = 0
g31 = 0
g32 = 0
g33 = r**2
g = sym.Matrix([[g00, g01, g02, g03],
[g10, g11, g12, g13],
[g20, g21, g22, g23],
[g30, g31, g32, g33]
])
diff_forms = schwarz.base_oneforms()
metric_diff_form = sum([TensorProduct(di, dj)*g[i, j] for i, di in enumerate(diff_forms)
for j, dj in enumerate(diff_forms)
])
print(metric_diff_form)
chris2 = metric_to_Christoffel_2nd(metric_diff_form)
print(chris2)
Rie = metric_to_Riemann_components(metric_diff_form)
print(Rie)
Is there a efficient and convenient solution in Python to do something like -
Find largest combination of two numbers x and y, with the following conditions -
0 < x < 1000
0 < y < 2000
x/y = 0.75
x & y are integers
It's easy to do it using a simple graphing calculator but trying to find the best way to do it in Python
import pulp
My_optimization_prob = pulp.LpProblem('My_Optimization_Problem', pulp.LpMaximize)
# Creating the variables
x = pulp.LpVariable("x", lowBound = 1, cat='Integer')
y = pulp.LpVariable("y", lowBound = 1, cat='Integer')
# Adding the Constraints
My_optimization_prob += x + y #Maximize X and Y
My_optimization_prob += x <= 999 # x < 1000
My_optimization_prob += y <= 1999 # y < 2000
My_optimization_prob += x - 0.75*y == 0 # x/y = 0.75
#Printing the Problem and Constraints
print(My_optimization_prob)
My_optimization_prob.solve()
#printing X Y
print('x = ',pulp.value(x))
print('y = ',pulp.value(y))
Probably just -
z = [(x, y) for x in range(1, 1000) for y in range(1, 2000) if x/y==0.75]
z.sort(key=lambda x: sum(x), reverse=True)
z[0]
#Returns (999, 1332)
This is convenient, not sure if this is the most efficient way.
Another possible relatively efficient solution is -
x_upper_limit = 1000
y_upper_limit = 2000
x = 0
y = 0
temp_variable = 0
ratio = 0.75
for i in range(x_upper_limit, 0, -1):
temp_variable = i/ratio
if temp_variable.is_integer() and temp_variable < y_upper_limit:
x = i
y = int(temp_variable)
break
print(x,y)
Applying a translate-transformation (matrix(1 0 0 1 tx ty)) I get the new coordinates by just calculating x(new) = x + tx, y(new) = y + ty.
Applying a scale-transformation (matrix(sx 0 0 sy 0 0)) I just multiply:x(new) = x * sx, y(new) = y * sy.
Now here's my question: How can I do this for a rotation (with a rotation center other than 0,0)?
In general: How to compute the new coordinates one gets after applying a matrix (a b c d e f) in SVG?
I looked up some math.
It's a matrix-vector-multiplication. For SVG that means:
matrix(a b c d e f) corresponds to
x(new) = a*x + c*y + e
y(new) = b*x + d*y + f
Create a NearestInteger function that accepts integer (x) and integer (y) input. This function will search for the number closest to x and divisible by y. If x is directly divisible by y, then outputs x only.
Example:
Input: n = 17, m = 4
Output: 16
Input: n = 18, m = 6
Output: 18
You could write a function:
def nearest(n, x):
u = n % x > x // 2
return n + (-1)**(1 - u) * abs(x * u - n % x)
nearest(22, 6) #->24
nearest(24, 6) #->24
nearest(26, 6) #->24
nearest(26.999, 6) #->24.0
nearest(27, 6) #-> 24
nearest(27.00001, 6) #-> 30.0
nearest(29, 6) #->30
nearest(32, 6) #->30
How do I draw an ellipse with lines of the same length coming out of it?
It's easy to do with a circle, I can just write something like
for (u = 0 ; u < 2*pi ; u += 0.001*pi) {
drawdot (cos(u), sin(u)) ;
drawline (cos(u), sin(u), 2*cos(u), 2*sin(u) ;
}
But if I did that for an ellipse, like below, the lines are different lengths.
for (u = 0 ; u < 2*pi ; u += 0.001*pi) {
drawdot (2*cos(u), sin(u)) ;
drawline (2*cos(u), sin(u), 4*cos(u), 2*sin(u) ;
}
How do I figure out how to make them the same length?
There are a few ways of thinking about this.
You can think of an ellipse as a circle that's been stretched in some direction. In this case, you've taken the circle x^2 + y^2 = 1 and applied the transformation to all points on that curve:
x' = 2x
y' = y
You can think of this as multiplying by the matrix:
[ 2 0 ]
[ 0 1 ]
To transform normals, you need to apply the inverse transpose of this matrix (i.e. the inverse of the transpose, or transpose of the inverse; it's the same thing):
[ 1/2 0 ]
[ 0 1 ]
(This, by the way, is known as the dual of the previous transformation. This is a very important operation in modern geometry.)
A normal to the circle at the point (x,y) goes in the direction (x,y). So a normal to the ellipse at the point (2x,y) goes in the direction (0.5*x,y). This suggests:
for (u = 0 ; u < 2*pi ; u += 0.001*pi) {
x = cos(u); y = sin(u);
drawdot (2*x, y) ;
drawline (2*x, y, 2*x + 0.5*x, y+y);
}
Or if you need a unit normal:
for (u = 0 ; u < 2*pi ; u += 0.001*pi) {
x = cos(u); y = sin(u);
drawdot (2*x, y) ;
dx = 0.5*x;
dy = y;
invm = 1 / sqrt(dx*dx + dy*dy);
drawline (2*x, y, 2*x + dx * invm, y + dy * invm);
}
Another way to think about it is in terms of an implicit contour. If you define the curve by a function:
f(x,y) = 0
then the normal vector points in the direction:
(df/dx, df/dy)
where the derivatives are partial derivatives. In your case:
f(x,y) = (x/2)^2 + y^2 = 0
df/dx = x/2
df/dy = y
which, you will note, is the same as the dual transformation.