Using Meshgrid for 3 variables

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

Answers (2)

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]
for example:
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
>> mesh(LL,aa,Z')
>> mesh(aa,LL,Z')
while
>> mesh(a,L,Z)
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.

4 Comments

So do I need to evaluate d at all possible combinations? My original function is
% code
A_var = linspace(-sigma(1), sigma(1), N);
L_var = linspace(-sigma(6), sigma(6), N);
Y = zeros(1,N);
POI_A = [A_var;Y;Y;Y;Y;L_var;Y]';
and d is given by
% code
d = eval_poly(cosy_out2(1).C, cosy_out2(1).E, POI_A);
which is polynomial evaluation, then I need to change my POI matrix with all possible A vs L combination.
I don't know what exactly you want, so I don't know what you need and if you need to evaluate the function at all possible combinations. That's what people usually do when they use mesh. Based on your code, I don't know if you want a mesh at all or if you just want to evaluate d(A_var(1), L_var(1)), d(A_var(2), L_var(2)),...,d(A_var(N),L_var(N)). In this case, most likely you rather want to use plot3(A_var,L_var,d).
I am not looking for 3d plot, rather I need the x and y axis to have a and L with d plotted against them, I tried using plotyy but not quite the result I expected. The only other option I got was meshing. Probably I'll try all combinations for grids.
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.

Sign in to comment.

Asked:

on 21 Apr 2015

Commented:

on 21 Apr 2015

Community Treasure Hunt

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

Start Hunting!