MATLAB How can I accurately model gravity using vector fields in Matlab?

AI Thread Summary
The discussion revolves around modeling gravity using vector field lines in Matlab, with a focus on both 2D and 3D plots. The user expresses difficulty in understanding the "scale" parameter in the quiver function, which is crucial for visualizing vector fields accurately. They provide code snippets for both 3D and 2D plots but mention that the 3D plot is not functioning correctly and the 2D plot is unsatisfactory. The 3D code attempts to create a meshgrid and calculate gravitational acceleration, but it results in a messy output. The 2D code uses gravitational constants and mass to generate a contour plot, but the quiver function's scale parameter is set to zero, preventing proper vector visualization. The user seeks assistance with their code and invites suggestions for better methods, including the possibility of sharing images for clarity.
Dirty D
Messages
6
Reaction score
0
I am trying to model gravity with vector field lines in Matlab. I can kinda do it with a 2D plot, but I can't quite grasp the "scale" field in the quiver function. I then tried to model in 3D vectors and I just made a mess w/ that. Below is my simple 3D and 2D code.

Help w/ this code or if you know a better way to do this would be nice!

non working 3D plot
[X Y Z] = meshgrid(-3:.1:3);
r = sqrt(X.^2 + Y.^2 + Z.^2);
ag = 1./r.^2;
[ax ay az] = gradient(ag);
quiver3(X, Y, Z, ax, ay, az)

Poor 2D plot
M = 1; %Mass of Earth in kg
G = 1; %Gravitational Constant
R = 1; %Radius of Earth in m
[x y] = meshgrid(-3:.75:3);
r = sqrt(x.^2 + y.^2);
ag = G*M./r.^2;
[ax ay] = gradient(ag);
contour(x,y,r,[R R])
colormap cool
hold on
scale = 0;
quiver(x,y,ax,ay,scale)
hold off
 
Last edited:
Physics news on Phys.org
Prehaps you could post an image file of some sort. I cannot open your .m file and will not aprove it without doing so.
 
is this better?
 

Similar threads

Replies
2
Views
3K
Replies
4
Views
1K
Replies
16
Views
6K
Replies
1
Views
2K
Replies
1
Views
4K
Back
Top