Using Meshgrid for 3 variables
24 Ansichten (letzte 30 Tage)
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 am 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).
Michael Haderlein am 21 Apr. 2015
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.