3D Plotting with Vectors and Meshgrid

2 vues (au cours des 30 derniers jours)
Devin
Devin le 12 Avr 2011
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.

Réponse acceptée

Matt Tearle
Matt Tearle le 12 Avr 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 commentaires
Devin
Devin le 12 Avr 2011
I think I did a poor job explaining this question. I edited my iniital question to include an example.
Matt Tearle
Matt Tearle le 13 Avr 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...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by