MATLAB: Question about surf function surf t = linspace(0,2,100) x = linspace(0,10,100) [X,T]=meshgrid(x,t) U = 10*sin(2*pi*(t-x/5))+2*sin(2*pi*(t+x/5)) surf(X,T,U) Why it can not work? It always tips me Z must be a matrix. Best Answer t = linspace(0,2,100)x = linspace(0,10,100)[X,T]=meshgrid(x,t)U = 10*sin(2*pi*(T-X/5))+2*sin(2*pi*(T+X/5))surf(X,T,U) Related SolutionsMATLAB: What’s wrong with this script You cannot use 0 or fractions in your indices into U.The syntax you are using is not a function definition: the syntax is an array access.UseU = 10 * sin(2 * pi * (t-x/5)) + 2 * sin(2 * pi * (t+x/5)); MATLAB: Complementary error function (surf plot) Add: [X,T] = ndgrid(x,t);and it works: x = linspace(0,10^(-6),20);t = linspace(0,5,5);[X,T] = ndgrid(x,t);u0 = 10^(-6); k = 10^(-9); dn = 2.*k.*sqrt(T);e2 = erf( X.*(dn).^(-1) );u = u0.*(1 - e2); surf(X,T,u)title('Complementary Error function from 0 to 10^(-6)')xlabel('Distance x')ylabel('Time t')although you may want to revise the code a bit. Related QuestionUsing surf with two-dimensional function handle argumentHelp plotting with simple linespace and surf3D Surf plot
Best Answer