For an assignment, I am supposed to use MatLab to plot the vector field. I've tried
[x,y]=meshgrid(-5:1:5,-5:1:5);
quiver(x,y,0.2*(x.^2+y.^2),0.2*(x-y),0);
which gives me the following picture:
vectorfield1.PNG
I have also tried this:
[x,y]=meshgrid(-5:1:5,-5:1:5);
quiver(x,y,0.2*(x^2+y^2),0.2*(x-y),0);
which gives me this picture:
vectorfield2.PNG
These are so different, and I'm not sure which one is correct because I don't understand why .^ is giving a different result than just ^ alone.
Any advice is greatly appreciated. Thank you!

 Réponse acceptée

madhan ravi
madhan ravi le 4 Déc 2018

1 vote

when you have a vector the right usage is .^ which is element wise operation each element is raised to the power ^ is only for scalars so use .^

4 commentaires

Jay
Jay le 4 Déc 2018
So since x and y are not vectors, does that mean the second image is the correct image since it was generated with just the ^ notation?
madhan ravi
madhan ravi le 4 Déc 2018
Modifié(e) : madhan ravi le 4 Déc 2018
I am not the one who says this is correct and that is wrong you should learn about Array vs Matrix operations. Basically what happens is if a matrix is square ^ performs matrix multiplication and .^ performs element wise operation so now you are the one to decide which one is apt for your need.
Jay
Jay le 4 Déc 2018
Thank you so much!
madhan ravi
madhan ravi le 4 Déc 2018
Anytime :)

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 4 Déc 2018
Modifié(e) : Image Analyst le 4 Déc 2018

4 votes

Well you're sort of close - at least you tried - but I don't think either of your attempts is correct. Madhan is correct in that using dot means that it's element-by-element raising to a power. But you need to look at what quiver wants, and that is vectors whereas your x and y are matrices. If you take a second look at it, I think you'll realize you need to use (:) to turn them into vectors when you compute u and v, and you'll get something like this:
[x, y] = meshgrid(-5:1:5, -5:1:5);
u = 0.2 * (x(:) .^ 2 + y(:) .^ 2);
v = 0.2 * (x(:) - y(:));
% Plot quiver:
subplot(2, 1, 1);
quiver(x(:), y(:), u, v, 'LineWidth', 1);
grid on;
axis square;
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
title('Quiver Plot', 'FontSize', 20);
% Plot surface of the magnitude
subplot(2, 1, 2);
z = reshape(sqrt(u .^ 2 + v .^ 2), [11,11]);
surf(-5:1:5, -5:1:5, z);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
title('Magnitude Plot', 'FontSize', 20);
0000 Screenshot.png
Notice that when x = y (along the diagonal), the vectors are flat, meaning no vertical component, as you'd expect from the equation which involves x-y.

1 commentaire

Jay
Jay le 5 Déc 2018
That makes so much more sense. Thank you so much for your help and explanation!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by