Matlab定义一个函数

Matlab define a function

我有一个物理方程式 P=(n R T/V - nb)-(an^2/V^2)

which n,b,a,R are constants and V,T are independent variables

the question said to construct an anonymous function (pressure)>P and then create a script function called (vanderWall) which return P,V,T and make mesh contour plot of (P,V,T) and plot of (P,V) for 10 values of T.

该函数应以 nT 步长生成限制为 T1 到 T2 的 T,还应以 nV 步长生成限制为 V1 到 V2 的 V

将这些值用于实际输出 T1=77T2=800nT=10V1=35V2=200nV=150n=1a=55.37b=30.4.

我试过这段代码没有有用的答案

%constants
n=1;
a=55.37;
b=30.4;
R=8.314;

%anonymous function of independent variables V and T
Pressure=@(V,T) ((n*R.*T)./(V-n*b))-((a*n^2)./V.^2);

[P,V,T]=vanderWall(Pressure,77,800,10,35,200,150,1,55.37,30.4);

%function to return Pressure,Volume and Temperature
function[P,V,T]=vanderWall(Pressure,T1,T2,nT,V1,V2,nV,varargin)

%Volume and temperature generatd by function
T=T1:nT:T2;
V=V1:nV:V2;
P=Pressure(V,T);

%mesh contour plot between T,V and P
meshc(T,V,P)

%plot of V and P (isotherms)
plot(V,P)

end

或许你可以试试下面的函数代码vanderWall,其中[T,V]=meshgrid(t,v)是让你的代码飞起来的关键步骤

function[P,V,T]=vanderWall(Pressure,T1,T2,nT,V1,V2,nV,varargin)

%Volume and temperature generatd by function
t = T1:nT:T2;
v = V1:nV:V2;
[T,V]=meshgrid(t,v);

P=Pressure(V,T);

%mesh contour plot between T,V and P
meshc(T,V,P)

%plot of V and P (isotherms)
plot(V,P)
end

或者您可以应用隐式单例扩展(感谢@CrisLuengo 的建议),即

function[P,V,T]=vanderWall(Pressure,T1,T2,nT,V1,V2,nV,varargin)

%Volume and temperature generatd by function
T = T1:nT:T2;
V = V1:nV:V2;

P=Pressure(V,T.')';

%mesh contour plot between T,V and P
meshc(T,V,P)

%plot of V and P (isotherms)
plot(V,P)
end

问题是,在您的函数中,您同时使用 VT 作为向量。您使用匿名函数的方式不清楚,T 除以 V。 我想你想要所有组合 - 然后你可以在你的匿名函数中用 V' 替换 V