I am using trying to understand someone else's code in Mathematica for the Finite Difference Method, except I changed a few values, but none of the code itself. Everything except for the Interpolation function later on in the code makes sense to me. And my intuition seemed right because I'm getting the errors:

Interpolation::inhr: Requested order is too high; order has been reduced to {0,0}. InterpolatingFunction::dmval: Input value {7.14286*10^-6,3.57143*10^-6} lies outside the range of data in the interpolating function. Extrapolation will be used.

Mx = 0.01;
My = 0.05;
Lx = Ly = 1.;
dx = Lx/Mx;
dy = Ly/My;
V[0, j_] = 0;
V[Mx, j_] = 1;
V[i_, 0] = 0;
V[i_, My] = 0;

p[i_, j_] = 0;

var = Flatten[Table[V[i, j], {i, 1, Mx - 1}, {j, 1, My - 1}]];

eqs = Flatten[
   Table[(V[i + 1, j] - 2 V[i, j] + 
          V[i - 1, j])/(dx)^2 + (V[i, j + 1] - 2 V[i, j] + 
          V[i, j - 1])/(dy)^2 .. p[i, j], {i, 1, Mx - 1}, {k, 1, 
     My - 1}]];

sol = Solve[eqs, var][[1]];

Vsol = Interpolation[
   Flatten[Table[{i dx, j  dy, V[i, j]}, {i, 0, Mx}, {j, 0, My}] /. 
     sol, 1]];

ContourPlot[Vsol[x, y], {x, 0, 0.1}, {y, 0, 0.05}, 
 FrameLabel -> {"x(m)", "y(m)"}, 
 PlotLabel -> "Finite Difference Method Solution when p_v = 0"]
  • 1
    $\begingroup$ Inspect the output of Table[{i dx, j dy, V[i, j]}, {i, 0, Mx}, {j, 0, My}]: by default Table` increments the variables by 1 and Mx, and My are less than 1. $\endgroup$ – Michael E2 Mar 17 at 18:49
  • $\begingroup$ Perhaps you meant Mx = 100, My = 20? Also, I think the .. in eqns is a typo. $\endgroup$ – Michael E2 Mar 17 at 18:54
  • $\begingroup$ @MichaelE2 ahh didn't see the .. in the eqs part.I have Mx and My set up that way to indicated the boundary constraints. That's the only way I can figure out how to put them in there, because the Dirchlet boundary condition function doesn't apply in this case. $\endgroup$ – LtGenSpartan Mar 17 at 21:15
  • $\begingroup$ What do you want i and j to run through in your Table? Your table consist of just one point, and that's not what you want, right? Maybe you want the Table arguments to be {i, 0, Mx, Mx/100}, {j, 0, My, My/100} or something like it? $\endgroup$ – Michael E2 Mar 17 at 21:25
  • $\begingroup$ @MichaelE2 I need the points to run through 0<x<0.1, and 0<y<0.05. It doesn't particularly matter how many points I have, but obviously more points are better for more data. Additionally, I need to know values at certain points, i.e (0.05, 0.025), so I would need enough points that cover the points of the values that I need to know. I also changed Mx and My to both be 100, as I forgot to account for something. $\endgroup$ – LtGenSpartan Mar 17 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.