Using Meshgrid for 3 variables
47 views (last 30 days)
Show older comments
Divas on 21 Apr 2015
Commented: Michael Haderlein on 21 Apr 2015
I am new to meshing and MATLAB, I have two variables 'a' and 'L' both varying from -sigma to +sigma and a third variable d which results from a matrix evaluation of [a;L] over a function. When I try if true [X,Y] = meshgrid(a,L); mesh(X,Y,d); end I get error saying d must be matrix. where as d is a scalar of same length as 'a' and 'L'. what is wrong here? Thanks in advance
David Sanchez on 21 Apr 2015
Make sure the size of d is right:
If length(a) = n and length(L) = m, then, you have to have size(d) = [m x n]
a = rand(10,1);
L = rand(5,1);
[aa,LL] = meshgrid(a,L);
Z = rand(10,5);
% then, these will work
>> mesh(L,a,Z) % this will not yield an error
Error using mesh (line 76)
Data dimensions must agree.
from documentation: mesh(X,Y,Z) draws a wireframe mesh with color determined by Z, so color is proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z).
For simplicity, let's set a and L to [1 2 3] each, ok? And your function to get d is just summation.
As your code snippet does not include the function, I guess you have done it before. So you have the values
a=[1 2 3]; %create a, L
L=[1 2 3];
d=a+L; %create d, value is [2 4 6];
Obviously, you don't have one d for each combination of a and L. What you acutally need is
a=[1 2 3];
L=[1 2 3];
X=[1 2 3;1 2 3;1 2 3];
Y=[1 1 1;2 2 2;3 3 3];
D=X+Y; %create D, value is [2 3 4;3 4 5;4 5 6];
This will then also work with mesh.
So do you know if you want d for all a,L combinations? I mean, the answer is just "yes" or "no". If "yes" you'll need mesh, if "no" you'll need plot3. Your code suggests "no", but it's not clear. In case N is small, you might also just want plot() and use "a" as x value and plot multiple lines for different L. plotyy does not sound like it should help in this context. Given the information I have, I cannot state more.
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!