I want to convolute a 2D Gaussian with a cylinder function in Mathematica. Mathematica will however just create an empty function without error messages (i.e. it behaves normally), thus when plotting the function or using it for calculations, nothing happens at all. Here is the 2D Gauss:
GaussFkt2D[x_, y_, mux_, muy_, sigmax_, sigmay_, A_] :=
A*E^-((x - mux)^2/(2 sigmax^2) + (y - muy)^2/(2 sigmay^2))
And here is the cylinder function:
cylFkt2D[x_, y_, w_] :=
Piecewise[{{0, x^2 + y^2 > (w)^2}, {1, x^2 + y^2 <= (w)^2}}]
To convolute, I use:
ConvolutionCylinderGauss2D[u_, v_, mux_, muy_, sigmax_, sigmay_, A_,w_] =
Convolve[
GaussFkt2D[x, y, mux, muy, sigmax, sigmay, A],
cylFkt2D[x, y, w], {x, y}, {u, v}
];
Is there a possibility to implement the convolution in such a way that Mathematica will not spoil the convolution? I suppose the problem could be that Mathematica is just overloaded when doing the convolution.
Related
This question is for the Halide language.
Say for a particular (x, y), I want to operate on a KxK patch around (x, y). E.g. sum them, square them, etc., to get the obtain the new value for (x, y).
Most of the Halide examples I've found "hard-code" selecting the neighboring coordinates. Like in this example, and also the blur algorithm example on the home page:
Func blur_x, blur_y; Var x, y;
// hard codes selecting x-1:x+1 and y-1:y+1
blur_x(x, y) = (input(x-1, y) + input(x, y) + input(x+1, y))/3;
blur_y(x, y) = (blur_x(x, y-1) + blur_x(x, y) + blur_x(x, y+1))/3;
But let's say I want to paramertize the size of my KxK patch. How would I select and then operate on a neighborhood of arbitrary size around (x, y)?
Regarding your questions in the comments, I think what you need is a Func with 4 Vars: output(x, y, xi, yi)
x, y is the coordinate of the pixel in the center of each patch, which is effectively the ordinary coordinate of the pixel in an image. And xi, yi is the inner coordinate of the pixels within each patch.
output(x, y, xi, yi) = input(x + xi, y + yi)
In this way, you get a group of pixels that you can operate on.
Maybe this is an answer.
// First add the boundary condition.
Func clamped = BoundaryConditions::repeat_edge(input);
// Define a 5x5 box that starts at (-2, -2)
RDom r(-2, 5, -2, 5);
// Compute the 5x5 sum around each pixel.
Func local_sum;
local_sum(x, y) = 0; // Compute the sum as a 32-bit integer
local_sum(x, y) += clamped(x + r.x, y + r.y);
Im trying to do some homework in which they gave me a domain and a double integral.
I was trying to use wolfram alpha or symbolab to know how to do it but I can't get them to work, with wolfram I get to draw the domain but I dont know how to integrate with that domain, because I have the integral, which is ∫∫D( x2 + 5y2 )dxdy and the D is the Domain I have in the plot, so Im trying to search some way to tell wolfram you have this plot and you need to calculate this integral in this domain, but I don't know how.
To get WolframAlpha to integrate the function x^2+5y^2 over the domain you can use WolframAlpha notation to integrate over the outer semicircle and subtract the integral over the inner semicircle
integrate x^2+5 y^2, {x,-4,4},{y,0,sqrt(16-x^2)} - integrate x^2+5 y^2, {x,-2,2},{y,0,sqrt(4-x^2)}
link to the WolframAlpha page
If calculations like this exceed either the line length limit or the computation time limit then you might do these as two separate calculations and then subtract the results as a final step.
In:
a = -10;
b = 10;
f = x^2 + 5 y^2;
Integrate[f, x, y] (*Indefinite Integral*)
Integrate[f, {x, a, b}, {y, a, b}] (*Definite Integral*)
Plot3D[f, {x, a, b}, {y, a, b}](*Plot*)
Out:
(x^3 y)/3 + (5 x y^3)/3
80000
I'm trying to solve a problem: I have to find the trajectory of an electron in a graphene lattice using Mathematica. I've tried to solve the Coulomb Force equation with NDSolve and to plot the result for each direction, but i obtain a white plot. Could someone help me please? Thank you in advance. Here's the code for the x direction:
coordx = {0.6327, 1.88058, 3.03927, 4.28716, 5.44584, 6.69373,
7.85241, 9.10029, 1.9728, 3.22069, 4.37937, 5.62726, 6.78594,
8.03382, 9.19251, 10.4404, 3.3129, 4.56079, 5.71947, 6.96736,
8.12604, 9.37393, 10.53261, 11.7805, 4.653, 5.90089, 7.05956,
8.30746, 9.46614, 10.71403, 11.87271, 13.1206};
me = 9.01*10^-31;
pi = 3.14159;
epsilon0 = 8.854*10^-12;
q = -1.6*10^-19;
Q = 1.6*10^-19;
step = 0.01;
Forzax[p_, r_] :=
Sum[(Q*q)/(4 pi*epsilon0*Norm[r - p[[i]]]^2), {i, Length[p]}]
Forzax[coordx, {x[t]}];
NDSolve[{x''[t] == Forzax[coordx, {x[t]}]/me , x[0] == 0,
x'[0] == 0}, {x[t]}, {t, 0, 1500}]
Show[ParametricPlot[Evaluate[{x[t]} /. %], {t, 0, 1500},
PlotRange -> All]]
I don't know what you're trying to plot, but these few modifications seem to plot your function.
sol = NDSolve[{x''[t] == Forzax[coordx, {x[t]}]/me,
x[0] == 0, x'[0] == 0}, {x}, {t, 0, 1500}];
f = sol[[1, 1, 2]];
Plot[f[t], {t, 0, 1500}, PlotRange -> All]
From what I can tell, your code is running fine but the plot is empty because you're calling ParametricPlot with just one function. From the documentation, this is how you call ParametricPlot:
ParametricPlot[{fx[t], fy[t]}, {t, tmin, tmax}]
Since you're still solving the 1D problem and only have x[t], ParametricPlot cannot draw anything; it's missing the y coordinate of the trajectory. Once you do a 2D calculation, ParamatricPlot should be able to give you to figure you want. If you want to do a 3D calculation, you should use ParamatricPlot3D.
A question though: how do you intend to to a 3D calculation of an electron in graphene? The motion of the electron in the 3rd dimension will not follow Newtonian mechanics at all because it is confined so much in that direction. In fact, I'd be kind of cautious about using Newton's 2nd law in graphene no matter what, since electrons in graphene behave like massless particles. I leave the interpretation of the results up to you, but the physicist in me cannot resist adding this word of caution.
Context: Two sets of data, one is the radius, r, and the other is the velocity, v. v can be positive and negative. The following code
p1=ListLogLogPlot[Table[{r[[i]],v[[i]]},{i,1,number_of_data}]];
p2=ListLogLogPlot[Table[{r[[i]],-v[[i]]},{i,1,number_of_data}],PlotStyle->{Red}];
Show[p1,p2]
is used to give a curve, with positive and negative v both plotted in log-log coordinates.
Question: How to draw a circular, contour-like plot, with Log[r] as the distance to the center of the circle, and the velocities (Log[v]) shown as different, but continuously varying colors, according to v's sign and magnitude?
You may use a DensityPlot function:
v[r_] := Sin[r]*r^2
DensityPlot[v[Norm[{x, y}]], {x, -5, 5}, {y, -5, 5}]
You can deal with the tabular data in two ways. You can either interpolate and use the interpolating function as above or you may use a ListDensityPlot function:
ListDensityPlot[Table[With[{r = RandomReal[{0, 4}], t = RandomReal[{0, 2 Pi}]},
{r Cos[t], r Sin[t], v[r]}], {10^4}]]
I hope this helps.
I am trying to parametrize a 3D geometry for shape optimization. The structure looks like the following. Another real example is here.
Currently I am using BSplines to create the lower part and using symmetry to create the whole down part of the foil. Here is what I get.
Now I have many control points to take care in order to run a shape optimization. I also don't know how to join the upper part with the bottom hydrofoil part in a sensible way. I don't know how to design a good middle part of the foil (fat nose part of the foil) where the upper part is linked to. I also need to accompany a flap with in the geometry.
Please offer some suggestion for parametrization of such a surface so that we can manipulate the geometry from MMA. The less control points are there better the situation is for optimization. May be combination of some analytic function in 3D. But I doubt if that is possible.
BR
I think you have two choices: 1) create the second part of the geometry and then write a face-face intersection algorithm to merge them. 2) create the second part of the geometry and write two functions that return -1 if a query point is inside the geometry and +1 if it is out side (other values will do). Then use RegionPlot3D[ f1[x,y,z]<0 || f2[x,y,z]<0,....]. The idea is the to extract the GraphicsComplex and use that. The question is going to be how well you can approximate the corners with that. Here is an illustration of what I mean.
if1[x_, y_, z_] := If[x^2 + y^2 + z^2 <= 1, -1, 1]
if2[x_, y_, z_] := If[(x - 1)^2 + y^2 <= 1 && -1.5 <= z <= 1.5, -1, 1]
res = RegionPlot3D[
if1[x, y, z] < 0 || if2[x, y, z] < 0, {x, -2, 2}, {y, -2,
2}, {z, -2, 2}, PlotPoints -> 100, Boxed -> False, Axes -> False]
Then extract the coords and the polygons.
coords = res[[1, 1]];
poly = Cases[res[[1]], _Polygon, Infinity];
Graphics3D[GraphicsComplex[coords, poly], Boxed -> False]
Hope this helps.