MATLAB: Hi, I keep getting the same error of Z must be a 2×2 matrix with the following code. Any takers willing to help a desperate student

contour issue

%%Parameters
Q = 137653.6; %J/mol
R = 8.314; %J/mol/K
T0 = 473.15; %K
r = 10; %Cooling history in kelvins per myr
c = 7.5e-5; %m
D0 = c^2.*exp(13.4); %m^2/s
A0 = 0; %Initial age
T = T0 - r.*t;
D = D0.*exp(-Q/R.*T);
tmax = 50e6* 365*24*3600; % in seconds !!
%%Discretisation
Dx = 1e-5;%


Dtmax = 0.5*Dx^2./D;%
Dt = 0.5.*Dtmax;%
%time vector:
t = 0:Dt:tmax;
%space vector:
x = 0:Dx:c;
%number of nodes in time and space:
N = length(t);
I = length(x);
%%initialisation
T = zeros(N,I);
%initial conditions:
T(1,:) = 0;
%%iterations
for n=1:N-1
%boundary conditions:
T(n+1, 1) = 0;
T(n+1, I) = 0;
%interior points:
for in=2:I-1
T(n+1, in) = T(n, in) + (alpha*Dt/Dx^2)*(T(n,in+1) ...
- 2*T(n,in) + T(n, in-1));
end
end
%%plots
figure;
[c, h] = contour(t/(1e6*24*3600*365), T', 'K');
clabel(c, h);
xlabel('time (My)')
ylabel('depth (km)')

Best Answer

You have
[c, h] = contour(t/(1e6*24*3600*365), T', 'K');
where t is a row vector and T is a 2D array.
When contour is called with three arguments, the arguments must be:
  • X, Y, Z -- which requires that the third parameter be a 2D array, not a scalar character
  • Z, n, linespec -- which requires that the first parameter be a 2D array
  • Z, v, linespec -- which requires that the first parameter be a 2D array
  • ax, Z, n -- which requires that the first parameter be an axes
  • ax, Z, v -- which requires that the first parameter be an axes
None of those conditions are met.
Perhaps you wanted to pass x between t and T' so as to provide the y data?
Note: your code needs t to construct T = T0 - r.*t but t is not assigned until several lines down, as t = 0:Dt:tmax; . Dt is defined Dtmax = 0.5*Dx^2./D which requires D. D is defined D = D0.*exp(-Q/R.*T) which requires T. So, to construct t from Dt you need D which needs T which needs t. You have a loop in your definition of t that cannot be solved.