I want to solve the heat diffusion equation with variable thermal conductivity. So I have the following code
Clear["Global'*"];
Needs["NDSolve`FEM`"];
mesh = ToElementMesh[Rectangle[{0, 0}, {10, 10}]];
k[x_, y_] := piecewise[{{10, x^2 + y^2 <= 2}, {5, x^2 + y^2 > 2}}];
gT = Grad[w[x, y], {x, y}];
tC = {{k, 0}, {0, k}};
pde = Div[tC.gT, {x, y}];
sol = First@NDSolve[{pde == 0,
DirichletCondition[w[x, y] == 100, y == 0],
DirichletCondition[w[x, y] == 400, y == 10],
DirichletCondition[w[x, y] == 0, x == 0],
DirichletCondition[w[x, y] == 0, x == 10]},
{w}, {x, y} \[Element] mesh];
ContourPlot[sol[x, y], {x, 0, 10}, {y, 0, 10}]
However I get this error
The PDE coefficient {{k,0},{0,k}} does not evaluate to a numeric \
matrix of dimensions {2,2}.
And I have not been able to figure out why this occurs and I've already looked at the following which didn't help.
http://reference.wolfram.com/language/FEMDocumentation/tutorial/SolvingPDEwithFEM.html NDSolve for axisymmetric problem
Any help would be appreciated. Thanks.
Also, as another problem I'm experiencing, is that if I take away the First@ in First@NDsolve. I get the following errors.
0.000714286 cannot be used as a variable
0.000714286 cannot be used as a variable
Why does this occur?
k
andk[x,y]
are different things as far as Mathematica is concerned. Have you tried replacing the former with the latter in your definition fortC
? $\endgroup$The PDE coefficient {{piecewise[{{10,x^2+y^2<=2},{5,x^2+y^2>2}}],0},{0,\ piecewise[{{10,x^2+y^2<=2},{5,x^2+y^2>2}}]}} does not evaluate to a \ numeric matrix of dimensions {2,2}.
and0.0007142857142857143 is not a valid variable
$\endgroup$Piecewise
should be capitalized, like all built-in Mathematica functions. $\endgroup$ContourPlot[w[x, y] /. sol // Evaluate, {x, 0, 10}, {y, 0, 10}]
. The solutionsol
is a replacement rule forw
. (Typesol
to see.) BTW, you can set up the mesh with an interior boundary that matches the discontinuity ink
. See the "Element Mesh Creation" tutorial. (In this example, the discontinuity is practically insignificant, though.) $\endgroup$