Expanding Mathematica code from 2 dimensions

In summary, the conversation discusses using the finite difference method to solve a variation of the 2 Dimensional Poisson's equation. The scientist provides guidance on modifying the code for the 1 and 3 dimensional cases, including changes to the GridNum, Gen function, FDeqn, and ListCorrelate steps. They also mention the use of LinearSolve and Partition to solve the equations for the coefficient array and right-hand side vector.
  • #1
brando623
3
0
I have this mathematica code to solve a variation of the 2 Dimensional Poisson's equation using the finite difference method.

xo = .5; yo = .5; \[Sigma] = .05; q = 1;
Gen[x_, y_] := -q*E^-((((x - xo)^2) + ((y - yo)^2))/(2*\[Sigma]^2));

GridNum = 300; Hstep = N[1/(GridNum + 1)];
GridPts = Table[u[i, j], {i, GridNum}, {j, GridNum}];

FDeqn = {{0, 1, 0}, {1, -4, 1}, {0, 1, 0}}/Hstep^2;
MatFD = ListCorrelate[FDeqn, GridPts, {2, 2}, 0];
Short[HE2 =
Thread[Map[Flatten,
MatFD ==
Table[Gen[Hstep i, Hstep j], {i, GridNum}, {j, GridNum}]]], 10];

{row, col} = CoefficientArrays[HE2, Flatten[GridPts]];

sol2D = LinearSolve[col, row];
sol2D = Partition[sol2D, GridNum];
sol3D = LinearSolve[col, -row];
sol3D = Partition[sol3D, GridNum];


I would like some assistance in using this same exact method to solve the problem for the 1 dimensional and 3 dimensional case.

I tried to just delete each part dealing with one dimension (to do the one dimensional case) such that everything that had to deal with x and y just dealt with x. The Table was a 300x1 table etc. I tried to add in a dimension in the same fashion, but kept getting errors when I tried to do the ListCorrelate step saying that the kernel and the table didn't have the same tensor rank. Thank you in advance.
 
Physics news on Phys.org
  • #2


I would first like to commend you for using the finite difference method to solve this variation of the 2 Dimensional Poisson's equation. This method is widely used in various fields of science and engineering for solving partial differential equations numerically.

To solve the 1 dimensional case, you can simply modify the code by setting the GridNum to 1 and removing the second variable in the Gen function. The GridPts will now be a 1-dimensional table and the FDeqn will be a 1x1 matrix. The rest of the code should remain the same.

For the 3 dimensional case, you will need to make a few changes. First, set the GridNum to 300 in all directions. Then, modify the Gen function to include the third variable, z. The GridPts will now be a 300x300x300 table. The FDeqn will be a 3x3 matrix with 1's along the main diagonal and -6's in the other entries. The ListCorrelate step will also need to be modified to account for the third dimension.

Additionally, when using LinearSolve, you will need to pass in a 3-dimensional matrix for the coefficient array and a 1-dimensional vector for the right-hand side of the equation. You can use the Partition function to convert the GridPts into a 1-dimensional vector for the coefficient array and the Gen function to create the right-hand side vector.

I hope this helps you to successfully solve the 1 and 3 dimensional cases using the finite difference method. Best of luck in your research!
 

FAQ: Expanding Mathematica code from 2 dimensions

How can I expand my Mathematica code from 2 dimensions to higher dimensions?

To expand your Mathematica code from 2 dimensions to higher dimensions, you can use the built-in function "Table" to create arrays of any dimension. You can also use the "Map" function to apply operations to each element of a multidimensional array. Additionally, you can use the "Transpose" function to manipulate the dimensions of an array.

Can I use loops in my Mathematica code to handle higher dimensions?

Yes, you can use the "For" and "Do" loops in Mathematica to handle higher dimensions. These loops allow you to iterate through multidimensional arrays and perform operations on each element.

How can I visualize higher dimensional data in Mathematica?

Mathematica has built-in functions such as "ListPlot3D" and "ListSliceDensityPlot3D" that allow you to visualize data in 3 dimensions. For higher dimensions, you can use the "SliceContourPlot3D" function to create 2D slices of your data, or the "HyperbolicPlot" function to create a 3D plot of data on a hyperbolic surface.

Are there any specific packages or libraries that can assist with expanding Mathematica code to higher dimensions?

Yes, there are several packages and libraries available for Mathematica that can assist with handling higher dimensions. Some popular options include the "Tensorial" package for tensor calculations and the "NDimensionalData" package for working with multidimensional data.

Can I use Mathematica code to solve problems in higher dimensions in other fields of science?

Yes, Mathematica has a wide range of applications in various fields of science, including physics, chemistry, and biology. Its ability to handle higher dimensions makes it a useful tool for solving problems in these fields, such as analyzing complex data sets or simulating multi-dimensional systems.

Similar threads

Back
Top