3D Plotting with Vectors and Meshgrid

I want to create a 3D plot for something that is very similar to the example in meshgrid (below)
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
Basically, I have vectors in my definition of Z and I am unsure about how to handle this. My definition of Z is
Z = mean((S - (X*A+Y)).^2);
S and A are vector quantities so this line doesn't make sense. The only solution I can think of here is to create a 3D meshgrid where the third dimension of the length of my vectors S and A (they are the same length). I think this could get tricky though. Is there an easier way.
Example: I have two vectors P and Q like these:
P = [zeros(1, 30) ones(1,40) zeros(1,30)] + .1*randn(1,100);
Q=3.4.*([zeros(1, 30) ones(1,40) zeros(1,30)] + .1.*randn(1,100))+5.3;
Because Q is scaled and offset finding the mean squared error between them directly is pointless.
MSE = mean((P-Q).^2);
this MSE doesn't mean much because Q is offset and scaled. I re-define MSE:
myMSE = mean((P-(a*Q+b)).^2)
where a and b are scalars. What I would like to do is plot myMSE as a function of both a and b. IE a is on the x-axis and b is on the y axis and Z is the value for myMSE.

 Respuesta aceptada

Matt Tearle
Matt Tearle el 12 de Abr. de 2011
There are times when, much as I love to mess with matrices and/or bsxfun, a for-loop is just the easiest way to go:
Z = zeros(size(X));
n = length(A);
for k = 1:n
Z = Z + (S(k) - (X*A(k)+Y)).^2;
end
Z = Z/n;
(I'm assuming that the length of A and S is not related to the size of X and Y, and that you want to average over that dimension -- ie average over the values of A and S.)
EDIT TO ADD: based on the comments below, here's the whole workflow in one easy script :)
EDIT AGAIN: using P & Q as added to the question
% Make example vectors P & Q
P = [zeros(1, 30) ones(1,40) zeros(1,30)] + .1*randn(1,100);
Q=3.4.*([zeros(1, 30) ones(1,40) zeros(1,30)] + .1.*randn(1,100))+5.3;
% Assuming Q = xP + y, look for best x and y
[X,Y] = meshgrid(-10:.2:10, -10:.2:10);
n = length(P);
Z = zeros(size(X));
for k = 1:n
Z = Z + (Q(k) - (X*P(k)+Y)).^2;
end
Z = Z/n;
% Get initial guess by clicking on the graph
contour(X,Y,Z,20)
x0 = ginput(1)
f = @(x) mean((Q - (x(1)*P+x(2))).^2);
xmin = fminsearch(f,x0)
% xmin should be about [3.4,5.3]
% Easier way? Do linear fit!
xmin2 = polyfit(P,Q,1)
% Look similar...?

4 comentarios

Devin
Devin el 12 de Abr. de 2011
This is actually an optimazation problem, I want to find the scalar values of X and Y that will give me the minimum mean squared error.
Matt Tearle
Matt Tearle el 12 de Abr. de 2011
OK, so you mean: given a scalar X and Y value, you want (scalar) Z to be mean((S - (X*A+Y)).^2) -- so "mean" here refers to averaging over the vectors S and A, right? Then you want to do this for a bunch of X and Y values to plot the result and see where the smallest Z is. Correct?
Devin
Devin el 12 de Abr. de 2011
I think I did a poor job explaining this question. I edited my iniital question to include an example.
Matt Tearle
Matt Tearle el 13 de Abr. de 2011
OK, that doesn't really change the answer, except to add that if you're assuming Q is a linear scaling of P, why not just do a linear fit? Answer above edited accordingly...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Abr. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by