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);
Related
How do I calculate the area of a projection? For example, using the following code, I get a projection on the X-Z plane.
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
view([0,0])
X-Z Plane Projection
I want to be able to determine the area of the projection of any surf plot that I create. Is there a command or function for this?
Short answer
polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
Explanation
The plot you want to calculate its area is,
In area calculation you need only the borders of the projection,
plot(x(1,:),max(z))
hold on
plot(x(1,:),min(z),'r')
and the output is,
The total area is the summation of both areas (upper border to x axis and lower border to x axis),
>> polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
>> 28.5947
If you want to get the projection area at an arbitrary view angle, you can use the viewmtx function to project the surface onto the viewing plane, and then use boundary and polyarea to extract the boundary and calculate the area. Something like this:
% draw your surface
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
axis equal;
%extract the viewing angle, and calculate the resulting transformation matrix
[az,el]=view(gca);
T= viewmtx(az,el);
% transform the surface points by this transformation matrix
pts= [x(:),y(:),z(:),ones(numel(x),1)]';
tpts= T*pts;
tpts=(tpts(1:3,:))';
% now "tpts" has the surface points projected onto the viewing plane
figure, plot( tpts(:,1), tpts(:,2), 'bo'); axis equal;
% calculate the border of this region, and calculate its area.
border = boundary(tpts(:,1), tpts(:,2));
projectedArea = polyarea(tpts(border,1), tpts(border,2));
This approach is based off of the help for viewmtx.
I have yet to start creating the code because I am a little stuck and would like some help to guide me in the right direction. I have to use MATLAB to solve the following issue:
I am given the x,y,z of a 3D surface (about 300 points) that is tilted by an arbitrary angle theta about the x axis and tilted by an arbitrary angle alpha about the y axis. The goal is to tilt the surface so that all of the z values are the same, meaning that the surface is level.
I have tried using rotation matrices, but it has not worked out how I expected. Any suggestions and ideas are greatly appreciated.
It's not elegant, but you could try brute forcing the solution by rotating the graph in set increments about both the x and y axes. Then, simply take the solution where the maximum number of z-coordinates fall within a narrow range. This would mean that the surface is approximately level.
A similar approach but a bit more formal would be to calculated the vector parallel to the surface and knowing the direction of this vector, the rotation parameters got really easy to obtain by c=norm((a,b,c))*cos(angle).
If you have a plane-like surface, you could fit it the to a plane equation a*x+b*y+c*z+d=0 and the vector normal to plane is (a,b,c).
If you have another type of surface -- that is normally what one has -- you may still find the vector of the plane useful, as the plane may be parallel to the surface or may give the direction to compare to made the rotation.
Depending on how you implement it, it will just rotate having point of rotation at the origin (0,0,0). If you plane is far from the origin, both figures will be far from each other. A simple translation can solve it.
With some random point around a plane i got it:
With the code that i used.
n=300;
x= 20.*rand(n,1)-10; %interval
y= 20.*rand(n,1)-10; %interval
%if you want to plot a plane
z=(2*(y+rand(n,1)*0.3)+2*(x+rand(n,1)*0.3)-7)/.4+rand(n,1)*0.3;
figure()
plot3(x,y,z,'.')
hold on
%here i put the plane average data on zero
Centerplane=[mean(x) mean(y) mean(z)];
xc=x-mean(x);
yc=y-mean(y);
zc=z-mean(z);
%get the 'a' and 'b' component of the plane equation (z=a*x +b*y +d)
A=[xc yc]; B=zc;
r=A\B %%%Matlab!
r = vrrotvec( (([r(1) r(2) 1]/norm([r(1) r(2) 1]))),[0 0 1]);
RM=vrrotvec2mat(r); %get the rotation matrix
rdata=[xc,yc,zc]*RM; %rotate data
% put in the proper position back
rt_c_x=rdata(:,1)+mean(x);
rt_c_y=rdata(:,2)+mean(y);
rt_c_z=rdata(:,3)+mean(z);
%and plot
plot3(rt_c_x,rt_c_y,rt_c_z,'c.')
A=[x y]; B=z; %default data
r=A\B %%%Matlab!
r = vrrotvec( (([r(1) r(2) 1]/norm([r(1) r(2) 1]))),[0 0 1]);
RM=vrrotvec2mat(r); %get the rotation matrix
rdata2=[x,y,z]*RM; %rotate data
%and plot
plot3(rdata2(:,1),rdata2(:,2),rdata2(:,3),'g.')
xlabel('X')
ylabel('Y')
zlabel('Z')
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.
So I have data in the form [x y z intensity] that I plot on a scatter3 figure with xyz axes. The colour of the data is used to dictate the intensity value. Problem is, using a scatter plot means the data points show up as discrete points. What I need, is a smooth shape - so I guess I need some kind of interpolation between the points?
I've tried using trisurf, but the problem with this one is that it interpolates between points that it shouldn't. So where I should have 'gaps' in my surface, it joins up the edges instead so it fills in the gaps. See the attached pics for clarification.
Does anyone have any suggestions?
The code I use is as below (the commented out scatter3 is what does the scatter plot, the rest does the trisurf):
% Read in data
dataM = csvread('3dDispersion.csv');
% scatter3(dataM(:,1), dataM(:,2), dataM(:,3), 5, dataM(:,4),'filled');
% Plot
hold on;
x = dataM(:,1);
y = dataM(:,2);
freq = dataM(:,3);
tri = delaunay(x,y);
h = trisurf(tri, x, y, freq);
% Make it pretty
% view(-45,30);
view(3);
axis vis3d;
lighting phong;
shading interp;
Use the boundary function in Matlab. This will apply a mesh similar to shrinkwrap over your points. In order to reduce the "gap closers", you will want to increase the "shrink factor".
Try K = boundary(X,Y,Z,0.9)
Where X, Y & Z are the vectors of your data points
https://www.mathworks.com/help/matlab/ref/boundary.html
You can then use trimesh or related surface plotting functions depending on how you want to display it.
I have a script to create scatter plots (using gscatter) based on x-y data (discrete data points, not continuous) produced by another script. Since these data points are actually the locations of certain objects in a circular space, adding polar grid lines will make the plots more meaningful.
Does anyone know how to show polar grid lines on a Cartesian scatter plot, or am I better off using polar plots?
I once made this script for drawing a polar coordinate system on top of a regular plot. Perhaps it can be useful for you. It is based on this script but simplified to only draw the coordinate system and no data. If this wasn't what you were looking for, check out the linked script, perhaps it can help as well.
Be sure to tweak the radius as needed! I usually disable the axis but it's up to you to fix that if you need another look :)
R=6000; %radius
S=10; %num circ.lines
N=10; %num ang.lines
sect_width=2*pi/N;
offset_angle=0:sect_width:2*pi-sect_width;
%------------------
r=linspace(0,R,S+1);
w=0:.01:2*pi;
clf %remove if needed
hold on
axis equal
for n=2:length(r)
plot(real(r(n)*exp(j*w)),imag(r(n)*exp(j*w)),'k--')
end
for n=1:length(offset_angle)
plot(real([0 R]*exp(j*offset_angle(n))),imag([0 R]*exp(j*offset_angle(n))),'k-')
end
%------------------
You can always use the pol2cart function to generate the polar grid lines.
For example:
function DrawGridLines
x = randn(10);
y = randn(10);
figure;scatter(x(:),y(:));
hold on ;
for angle = 0:20:(360-20)
[x1,y1] = pol2cart( angle / 180 * pi , [0 2]);
plot(x1,y1,'r')
end
for rho = 0:0.1:2
[x1,y1] = pol2cart( 0:0.01:2*pi , rho);
plot(x1,y1,'b')
end
axis equal
end