็็Have a question about plot graph

1 次查看(过去 30 天)
I'm new to Matlab program, I want to plot mesh 3D graph but this code have a problem in "mesh(T,Cs,x)" please give any advise to me.
Thank you
%Graph 1
T=[873:100:1073];
Cs=[0.15 0.30 0.45];
[T,Cs]=meshgrid(T,Cs);
mesh(T,Cs,x)
Unrecognized function or variable 'x'.
Ylabel('length Carbon can diffuse(cm)','fontsize',18);
Xlabel('temperature (°C)','fontsize',18);
Zlabel('CarbonStart ,fontsize',18);
  3 个评论
Ratchapon Nilprapa
Ratchapon Nilprapa 2021-12-20
编辑:Walter Roberson 2021-12-20
Yes, but 'x' value is found from the equation on the line above (x = z .* scalingFactor;).
this is all of my code
%%input parameter%%
R=8.314;
T=[873:100:1073];%%%K
Cs=[15 30 45];%%Carbon at surface
Cx=0.12;
C0= 0.08;%Carbon in specimen
t=3600;% sec
% find diffusion coefficients
D=-0.27*(1.04/-0.22)*exp((-246)./(R*T)); %(unit: cm^2*sec^-1)
% use D to find how far that carbon can diffusion
Erf = (Cs-Cx)./(Cs-C0);
z=NaN(size(Erf));
scalingFactor = 2*sqrt(D*t);
for n = 1 : numel(Erf)
if Erf(n)<2
z(n)=(-0.3725*Erf(n)^2)+(1.2144*(Erf(n)))+(0.0006);%error(0.01-0.08%) 0.4286
else
z(n)=(-0.0109*Erf(n)^2)+(0.0577*(Erf(n)))+(0.9235);%error(<0.01%)
end
%find X6
x = z .* scalingFactor; %%% ***t=3600***
end
%Graph 1
T=[873:100:1073];
Cs=[0.15 0.30 0.45];
[T,Cs]=meshgrid(T,Cs);
mesh(T,Cs,x)
Error using mesh (line 71)
Z must be a matrix, not a scalar or vector.
Ylabel('length Carbon can diffuse(cm)','fontsize',18);
Xlabel('temperature (°C)','fontsize',18);
Zlabel('CarbonStart ,fontsize',18);
Walter Roberson
Walter Roberson 2021-12-20
Cs=[15 30 45];%%Carbon at surface
Vector.
Cx=0.12;
C0= 0.08;%Carbon in specimen
scalar and scalar
Erf = (Cs-Cx)./(Cs-C0);
vector minus scalar is vector, vector minus scalar is vector, vector ./ vector with the same orientation gives a vector result, so Erf is a vector.
z=NaN(size(Erf));
So z will be initialized as a vector.
for n = 1 : numel(Erf)
if Erf(n)<2
z(n)=(-0.3725*Erf(n)^2)+(1.2144*(Erf(n)))+(0.0006);%error(0.01-0.08%) 0.4286
else
z(n)=(-0.0109*Erf(n)^2)+(0.0577*(Erf(n)))+(0.9235);%error(<0.01%)
end
%find X6
Scalar entries of z are assigned to, so z remains a vector.
x = z .* scalingFactor; %%% ***t=3600***
all of x is being overwritten each time. The right hand side uses all of z, including the entries that are still nan because they have not been written to yet, so x will be set as something the same size as z, so x will be a vector.
T=[873:100:1073];
Cs=[0.15 0.30 0.45];
[T,Cs]=meshgrid(T,Cs);
After that, T and Cs will be 2D grids that are 3 x 3.
mesh(T,Cs,x)
T is 3x3, Cs is 3x3, x is a vector the same size as z which is the same size as Erf which is the same size as the earlier meaning of Cs.
Note by the way that the earlier Cs uses 15, 30, 45, but the new Cs uses 0.15, 0.30, 0.45, which is 100 times smaller. Using that as coordinates for a plot is misleading, as the calculation was done in terms of the larger-scale Cs.
You are plotting x as if it depends upon T (temperature), but nothing in your calculation uses temperature.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by