Sometimes in finite difference we want to evaluate the force or the function in the
RHS of the equation \(f(x,y)\) using the coordinates \(x,y\) on a 2D grid. The grid spacing can
be \(h\) and the coordinates extend from some \(x\) value to some other \(x\) value and from
some \(y\) value to some other \(y\) values. In Matlab, the coordinates are setup using
meshgrid
.
In Mathematica, there is really no need for meshgrid
as the build-in command Map
can be
used to evaluate the function at each grid point. The coodinates physical values are
generated using the Table
command. Here is example evaluating and plotting \(f(x,y)= \sin (10 x y) e^{-x y}\) on a grid of
spacing \(h=1/N\) where \(N\) is number of elements. The grid goes from \(x=-1\) to \(x=1\) by spacing of \(h\). Same for the \(y\)
direction.
To use the actual physical coordinates on the axis above, then replace the plot above with this below. This also includes an implementation of meshgrid like function in Mathematica which makes it easier to work with for someone familiar with Matlab:
To see each point value, use InterpolationOrder -> 0
in the plot command.
Matlab clear all; close all; nElem = 10; h = 1/nElem; [X,Y] = meshgrid(-1:h:1,-1:h:1); f = @(x,y) sin(x*10.*y) .* exp(-x.*y); force = f(X,Y); surf(X,Y,force)
|
|