Vector field Streamline on sphere - matlab

I have the probelm connected with vector field on sphere. I have six 2d arrays: first three for coordinates and second three for vector components.
I need to draw streamlines of this vector field on sphere. I have tried streamlines and streamslice, but they dont support spherical surfacec. Do you have any suggestions how I can plost streamlines of this vector field?

First define the flow arena with meshgrid. This creates a 3D array of points at which the velocity magnitudes and directions can be calculated.
[x,y,z] = meshgrid(-1.01:0.02:1.01,-1.01:0.02:1.01,-1.01:0.02:1.01);
Define your flow parameters.
U = 1; % x-wise free stream velocity
a = 0.5; % sphere radius
mu = -2*pi*a^3*U; % doublet strength required to create spherical streamlines
The x,y,z velocity components for an x-wise (streamwise) doublet superimposed with an x-direction flow is:
ux = U+mu.*(2.*x.^2-y.^2-z.^2)./(4*pi.*(x.^2+y.^2+z.^2).^(5/2));
uy = 3*mu.*x.*y./(4*pi.*(x.^2+y.^2+z.^2).^(5/2));
uz = 3*mu.*x.*z./(4*pi.*(x.^2+y.^2+z.^2).^(5/2));
The start point of the streamlines needs to be defined. For this case a small 2D mesh of points on the upstream side of the flow field centred around the stagnation streamline is a good choice.
[startx, starty, startz] = meshgrid(-1,-0.1:0.02:0.1,-0.1:0.02:0.1);
Generate the streamline plot.
streamline(x,y,z,ux,uy,uz,startx,starty,startz)
The result looks a bit like an onion but represents the 3D potential flow around a sphere.

Related

How can I create a slice of a surface plot to create a line? (Matlab)

Given some function z = f(x,y), I'm interested in creating a (1D) line plot along an arbitrary cutting plane in x,y,z. How do I do this in Matlab? Slice, for example, provides a higher dimensional version (colormap of density data) but this is not what I'm looking for.
E.g.:
z = peaks(50);
surf(z);
%->plot z along some defined plane in x,y,z...
This has been asked before, e.g. here, but this is the answer given is for reducing 3D data to 2D data, and there is no obvious answer on googling. Thanks.
If the normal vector of the plane you want to slice your surface will always lay in the xy plane, then you can interpolate the data over your surface along the x,y coordinates that are in the slicing line, for example, let the plane be defined as going from the point (0,15) to the point (50,35)
% Create Data
z=peaks(50);
% Create x,y coordinates of the data
[x,y]=meshgrid(1:50);
% Plot Data and the slicing plane
surf(z);
hold on
patch([0,0,50,50],[15,15,35,35],[10,-10,-10,10],'w','FaceAlpha',0.7);
% Plot an arbitrary origin axis for the slicing plane, this will be relevant later
plot3([0,0],[15,15],[-10,10],'r','linewidth',3);
Since it is a plane, is relatively easy to obtain the x,y coordinates alogn the slicing plane with linspace, I'll get 100 points, and then interpolate those 100 points into the original data.
% Create x and y over the slicing plane
xq=linspace(0,50,100);
yq=linspace(15,35,100);
% Interpolate over the surface
zq=interp2(x,y,z,xq,yq);
Now that we have the values of z, we need against what to plot them against, that's where you need to define an arbitrary origin axis for your splicing plane, I defined mine at (0,15) for convenience sake, then calculate the distance of every x,y pair to this axis, and then we can plot the obtained z against this distance.
dq=sqrt((xq-0).^2 + (yq-15).^2);
plot(dq,zq)
axis([min(dq),max(dq),-10,10]) % to mantain a good perspective

MATLAB: Area and centre of mass of a plotted figure

I have made a function that allows a user to draw several points and interpolate those points. I want the function to calculate the centre of mass using this vector :
I think I should therefore first calculate the area of the figure (I drew this example to illustrate the function output). I want to do this using Green's theorem
However since I'm quite a beginner in MATLAB I'm stuck at how to implement this formula in order to find the centre of mass. I'm also not sure how to get the data as my output so far is only the x- and y cordinates of the points.
function draw
fig = figure();
hax = axes('Parent', fig);
axis(hax, 'manual')
[x,y] = getpts();
M = [x y]
T = cumsum(sqrt([0,diff(x')].^2 + [0,diff(y')].^2));
T_i = linspace(T(1),T(end),1000);
X_i = interp1(T,x',T_i,'cubic');
Y_i = interp1(T,y',T_i,'cubic');
plot(x,y,'b.',X_i,Y_i,'r-')
end
The Center Of Mass for a 2D coordinate system should just be the mean of the interpolated x-coordinates and y-coordinates. The Interpolation should give you evenly spaced coordinates which you can use to your advantage. So simply add to your existing function:
CenterOfMass= [mean(X_i),mean(Y_i)]
plot(x,y,'b.',X_i,Y_i,'r-')
hold on
plot(CenterOfMass(1),CenterOfMass(2),'ro')
should give you the center of mass assuming that all points are weighted equally.

Plotting a rectangular matrix into a circle

I have generated a rectangular matrix with the azimouth angle changing with rows and the radius changing as you change column. These are meant to represent the relative velocities experienced by a rotating helicopter blade. This produces a matrix called Vmat. I want to plot this to appears in a circle (representing the rotation of the blade)
So far I have tried
[R,T] = meshgrid(r,az);
[x,y] = pol2cart(T,R);
surf(x,y,Vmat(r,az));
which should produce a contoured surface showing velocity as it changes with azimouth angle and radius but it comes up with dimension errors.
I don't mind if it is a 2d contour plot or 3d plot i guess both would be written in a similar way.
Thanks
James
The error is in writing Vmat(r,az), presuming that these are actual values of radius and azimuth, not indexes into your radius and azimuth. If you want to take only a subset of Vmat that's a slightly different matter, but this should work:
[R,T] = meshgrid(r,az); % creates a grid in polar coordinates
[x,y] = pol2cart(T,R); % changes those to cartesian for surf
surf(x,y,Vmat);
Alternatively you could do a contour plot:
h = polar([0 2*pi], [0 max(r)]); % set up polar axes with right scale
delete(h) % remove line
hold on
contour(x,y,Vmat);

Plotting characteristics of antenna radiation in matlab

I need to plot this function
theta = (-pi:0.01:pi);
f = 3*10^9;
c = 299792458;
da = 2;
Here's my code, but I'm not sure it's correct. I dont know where exactly dot mark should be. How to set X-Axis in degress?
beta = (2*pi*f)/c;
const= (da*beta)/2;
j= (cos(theta)+1).*(besselj(1,const*sin(theta))./(const*sin(theta)));
My another question is how to plot this function in polar coordinates.
I made something like this.
polar(theta,j);
Is it possible to rotate that function(by y-axis) to get 3D plot?
Things are quite right to me, although I wouldn't use the symbol j as a variable because (as i does) it is the symbol for the imaginary unit (sqrt(-1)). Doing so you are overriding it, thus things will work until you don't need complex numbers.
You should use element-wise operations such as (.*) when you aim at combining arrays entries element by element, as you correctly did to obtain F(\theta). In fact, cos(theta) is the array of the cosines of the angles contained in theta and so on.
Finally, you can rotate the plot using the command Rotate 3D in the plot window. Nonetheless, you have a 2D curve (F(\theta)) therefore, you will keep on rotating a 2D graph obtaining some kind of perspective view of it, nothing more. To obtain genuine information you need an additional dependent variable (Or I misunderstood your question?).
EDIT: Now I see your point, you want the Surface of revolution around some axis, which I suppose by virtue of the symmetry therein to be theta=0. Well, revolution surfaces can be obtained by a bit of analytic geometry and plotted e.g. by using mesh. Check this out:
% // 2D polar coordinate radius (your j)
Rad= (cos(theta)+1).*(besselj(1,const*sin(theta))./(const*sin(theta)));
Rad = abs(Rad); % // We need its absolute value for sake of clarity
xv = Rad .* cos(theta); % // 2D Cartesian coordinates
yv = Rad .* sin(theta); % // 2D Cartesian coordinates
phi = -pi:.01:pi; % // 3D revolution angle around theta = 0
% // 3D points of the surface
xf = repmat(xv',size(phi));
yf = yv' * cos(phi);
zf = yv' * sin(phi);
mesh(xf,yf,zf)
You can also add graphics effects
this is done via
mesh(xf,yf,zf,'FaceColor','interp','FaceLighting','phong')
camlight right
and a finer angular discretization (1e-3).

Matlab - Propagate points orthogonally on to the edge of shape boundaries

I have a set of points which I want to propagate on to the edge of shape boundary defined by a binary image. The shape boundary is defined by a 1px wide white edge.
I have the coordinates of these points stored in a 2 row by n column matrix. The shape forms a concave boundary with no holes within itself made of around 2500 points. I have approximately 80 to 150 points that I wish to propagate on the shape boundary.
I want to cast a ray from each point from the set of points in an orthogonal direction and detect at which point it intersects the shape boundary at. The orthogonal direction has already been determined. For the required purposes it is calculated taking the normal of the contour calculated for point, using point-1 and point+1.
What would be the best method to do this?
Are there some sort of ray tracing algorithms that could be used?
Thank you very much in advance for any help!
EDIT: I have tried to make the question much clearer and added a image describing the problem. In the image the grey line represents the shape contour, the red dots the points
I want to propagate and the green line an imaginary orthongally cast ray.
alt text http://img504.imageshack.us/img504/3107/orth.png
ANOTHER EDIT: For clarification I have posted the code used to calculate the normals for each point. Where the xt and yt are vectors storing the coordinates for each point. After calculating the normal value it can be propagated by using the linspace function and the requested length of the orthogonal line.
%#derivaties of contour
dx=[xt(2)-xt(1) (xt(3:end)-xt(1:end-2))/2 xt(end)-xt(end-1)];
dy=[yt(2)-yt(1) (yt(3:end)-yt(1:end-2))/2 yt(end)-yt(end-1)];
%#normals of contourpoints
l=sqrt(dx.^2+dy.^2);
nx = -dy./l;
ny = dx./l;
normals = [nx,ny];
It depends on how many unit vectors you want to test against one shape. If you have one shape and many tests, the easiest thing to do is probably to convert your shape coordinates to polar coordinates which implicitly represent your solution already. This may not be a very effective solution however if you have different shapes and only a few tests for every shape.
Update based on the edited question:
If the rays can start from arbitrary points, not only from the origin, you have to test against all the points. This can be done easily by transforming your shape boundary such that your ray to test starts in the origin in either coordinate direction (positive x in my example code)
% vector of shape boundary points (assumed to be image coordinates, i.e. integers)
shapeBoundary = [xs, ys];
% define the start point and direction you want to test
startPoint = [xsp, ysp];
testVector = unit([xv, yv]);
% now transform the shape boundary
shapeBoundaryTrans(:,1) = shapeBoundary(:,1)-startPoint(1);
shapeBoundaryTrans(:,2) = shapeBoundary(:,2)-startPoint(2);
rotMatrix = [testVector(2), testVector(1); ...
testVector(-1), testVector(2)];
% somewhat strange transformation to keep it vectorized
shapeBoundaryTrans = shapeBoundaryTrans * rotMatrix';
% now the test is easy: find the points close to the positive x-axis
selector = (abs(shapeBoundaryTrans(:,2)) < 0.5) & (shapeBoundaryTrans(:,1) > 0);
shapeBoundaryTrans(:,2) = 1:size(shapeBoundaryTrans, 1)';
shapeBoundaryReduced = shapeBoundaryTrans(selector, :);
if (isempty(shapeBoundaryReduced))
[dummy, idx] = min(shapeBoundaryReduced(:, 1));
collIdx = shapeBoundaryReduced(idx, 2);
% you have a collision with point collIdx of your shapeBoundary
else
% no collision
end
This could be done in a nicer way probably, but you get the idea...
If I understand your problem correctly (project each point onto the closest point of the shape boundary), you can
use sub2ind to convert the "2 row by n column matrix" description to a BW image with white pixels, something like
myimage=zeros(imagesize);
myimage(imagesize, x_coords, y_coords) = 1
use imfill to fill the outside of the boundary
run [D,L] = bwdist(BW) on the resulting image, and just read the answers from L.
Should be fairly straightforward.