Find value of z of a surface plot when values of x and y are known.

2 次查看(过去 30 天)
I have a dispersal model to be solved numerically using matlab. The codes are as below:
function dispersal(fdis,c1,c2,L,T,h,k,D)
%solve the dispersal model with Initial Condition u(x,0)=fdis(x)and Boundary Condition c(0,t)=c1 and c(L,t)=c2 using explicit finite-difference (forward-difference) method.
n=L/h; m=T/k;
lambda=(D^2)*(k/(h^2))
z=0:h:L;
for i=1:n+1
u(i)=feval(fdis,(i-1)*h);
gt(1,i)=u(i);
end
for j=1:m
t=j*k;
for i=1:n+1
if (i==1)
y(i)=c1;
elseif (i==n+1)
y(i)=c2;
else
y(i)=(1-2*lambda)*u(i)+lambda*(u(i+1)+u(i-1));
end;
gt(j+1,i)=y(i);
end;
u=y;
end
box on
x=0:h:L; y=0:k:T;
[X,Y]=meshgrid(x,y);
surf(X,Y,gt)
xlabel('x'); ylabel('t'); zlabel('u');
end
The codes produced a solution in terms of a table and a surface plot. Now, i want to find the values of u when i provide the values of x and t from the surface plot. How should i do that in matlab codes??
Thank you very much.
Regards,
  3 个评论
Walter Roberson
Walter Roberson 2012-9-19
t does not appear in the surface plot. You calculate t=j*k but you never use it.
CY
CY 2012-9-19
Thank you for your comment. t need not to be included in the calculation of the value for u here. This code approximates the solution of dispersal model(a partial differential equation) - du/dx=D*(d^2u/dx^2) - by one of the numerical methods, i.e. explicit finite difference method - the value of u when x=x_i,and t=t_j+1, u(x_i,j_j+1) = [1-lambda]*U(x_i,t_j)+lambda*[u(x_i+1,t_j)+u(x_i-1,t_j)] where lambda = (D^2)*(k/(h^2)). The surface plot consists of all the points of u for all the points of x_i, i=0..n and t_j, j=0..m.
For example: Let fdis=sin(pi*x),c1=c2=0,L=1,T=0.025,h=0.1,k=0.0025 and D=1.
Then, n=1/0.1=10, m=0.025/0.0025=10 and lambda=(1^2)*(0.0025/(0.1^2))=0.25.
For j=0, then t=0, when i=1, u(x_1,t_0)=sin(pi*((1-1)*0.1)=0, when i=2, u(x_2,t_0)=sin(pi*((2-1)*0.1)=0.309017, when i=3, u(x_3,t_0)=sin(pi((3-1)*0.1)=0.587785 and so on.
For j=1, then t=0.0025, when i=1, u(x_1,t_1)=0 [from the boundary condition], when i=2, u(x_2,t_1)=[1-2*0.25]*u(x_2,t_0)+lambda*[u(x_3,t_0)+u(x_1,t_0)=(0.5*0.309017)+0.25(0.587785-0)=0.301455 and so on.
So, for every value of t (0.0, 0.0025,..,0.0250), there's a value of u correspond to every value of x(0.0, 0.1, .. 1.0). The values of x and t are used to form the meshgrid to surface plot the corresponding values of u for every t and its' corresponding x. The resulting surface plot is something like the rectangular blanket with one of the edge slightly lifted up.
I hope my explanation is not confusing.
Thank you very much. Really hope that someone can answer my question - how to use matlab to find the values of u from the surface plot when the values of x and t are provided. Thank you.

请先登录,再进行评论。

回答(1 个)

Laura Proctor
Laura Proctor 2012-9-19
You can do this using the TriScatteredInterp function.
Here is the example given from the doc link above:
% Create a data set:
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
% Construct the interpolant:
F = TriScatteredInterp(x,y,z);
% Evaluate the interpolant at the locations (qx, qy).
% The corresponding value at these locations is qz .
ti = -2:.25:2;
[qx,qy] = meshgrid(ti,ti);
qz = F(qx,qy);
mesh(qx,qy,qz);
hold on;
plot3(x,y,z,'o');

类别

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