Load needed packages:
with(plots): with(linalg):Set up array to hold the temperatures:
n := 21; t := array(1..n, 1..n):Define the equations which express the mean value principle:
eq := array(2..n-1, 2..n-1); for i from 2 to n-1 do for j from 2 to n-1 do eq[i,j] := t[i,j] = (1/4)*( t[i+1,j] + t[i,j+1] + t[i-1,j] + t[i,j-1] ); od; od;Verify an equation:
eq[2,4];Define the boundary values:
for i from 1 to n do t[1,i] := 1.0; t[i,1] := 1.0; od: for i from 2 to n do t[n,i] := 0.0; t[i,n] := 0.0; od:Convert the array of equations to a set so that they will have the right type for solve:
eqs := convert(eq, set):Solve the equation:
sol := solve( eqs ):Assign the solutions found to the array t:
assign(sol);Verify a temperature. The temperature of the middle cell be very close to the average temperature of the boundary values.
t[11,11];Plot the result:
matrixplot(t);