I can rotate an object about an axis but theta is very off
it should be rotating from 1 degree to 45 degrees in 1 degree increments but it's doing much more than that. I converted theta (th) from degrees to radians but the issue still occurs.
Here's a link to the animation of the rotation
https://www.dropbox.com/s/bnc7m4fxipicizr/rot2.gif
And the code is below:
clf
[Z,Y,X] = cylinder(10:-1:0, 50);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
array_sz_begin=size(X);
W=repmat(1,array_sz_begin); %create ones for w
figure(1), clf;surf(X,Y,Z);axis equal;
%--- z-rotation matrix Rz
for n=1:1:45
th=n*pi/180; %angle of rotation converted to radians;
Rz=[cos(th) -sin(th) 0 0;sin(th) cos(th) 0 0;0 0 1 0;0 0 0 1];
P=[X(:) Y(:) Z(:) W(:)]*Rz; %rotate each point on surface
X=reshape(P(:,1),array_sz_begin);%transform surface vertices back
Y=reshape(P(:,2),array_sz_begin);
Z=reshape(P(:,3),array_sz_begin);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
clear P;
title(['Shift in ',num2str(n),' deg']);
hold on;surf(X,Y,Z);axis equal;
pause (.5)
end
PS I'm using octave 4.0 which is like matlab.
Decided to post the solution in case someone has an issue like this. All thanks goes to BillBokeey
clf
[Z_orig,Y_orig,X_orig] = cylinder(10:-1:0, 50);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
array_sz_begin=size(X_orig);
W=repmat(1,array_sz_begin); %create ones for w
figure(1), clf;surf(X_orig,Y_orig,Z_orig);axis equal;
%--- define z-rotation matrix Rz
for n=1:1:90
th=n*pi/180; %angle of rotation converted to radians;
Rz=[cos(th) -sin(th) 0 0;sin(th) cos(th) 0 0;0 0 1 0;0 0 0 1];
%Rz=[cos(th) -sin(th) 0;sin(th) cos(th) 0;0 0 1];
P=[X_orig(:) Y_orig(:) Z_orig(:) W(:)]*Rz; %rotate each original point on surface
X=reshape(P(:,1),array_sz_begin);%transform surface vertices's back
Y=reshape(P(:,2),array_sz_begin);
Z=reshape(P(:,3),array_sz_begin);
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
clear P;
title(['Rotate in ',num2str(n),' deg']);
hold on;surf(X,Y,Z);axis equal;
pause (.1)
clear P
end
Related
How can I find the centroid of left and right side part of ellipsoid to cross section plane. Here is the code.
[x,y,z]=ellipsoid(0,0,0,0.5,1,0.25,50);
direction = [1 0 0];
figure
h=surfl(x,y,z);
set(h,'facealpha',0.5)
shading interp
xlabel('x')
ylabel('y')
zlabel('z')
hold on
s=patch([1 -1 -1 1], [1 1 -1 -1], [0 0 0 0]) ;
rotate(s,direction,120)
I'm new to Matlab can anyone explain why the circle is measuring as 10 on the graph double to the radius. I expected it to be 5 matching the radius
xCenter = 5;
yCenter = 5;
theta = 0 : 0.01 : 2*pi;
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 10]);
ylim([0 10]);
grid on;
Thanks
If you want the circle to reach 5 on the x and y axes, then it should be centered at the origin.
Below is some code that recreates my problem as simplified as I can make it. It does a subplot with two plots, you'll notice the plot on the right (contour only) has the correct correlation between the contour colors and the color bar but when a surface is added (left plot) the colors no longer match up.
Notes:
I've tried contourslice but I get the same results. I've posted the code for that below too.
How far off the colors are seems to depend on the values of the contour data itself. If you replace my contour data with peaks, it works fine. However this does not solve the underlying problem.
Code using contour:
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
%make contour data
[x,y] = meshgrid(-1:.5:1,-1:.5:1);
con = (x.^2+y.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contour(x,y,con) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,con)
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Code using contourslice instead of contour (same problem)
clear all; close all; clc
%define box coordinates
bx = [0 1 1 0 0;0 1 1 0 0]-.5;
by = [0 0 1 1 0;0 0 1 1 0]-.5;
bz = [0 0 0 0 0;1 1 1 1 1]-.5;
x = -1:.5:1;
y = x;
z = x;
%make contour data
[xg,yg,zg] = ndgrid(x,y,z);
V = 3-(xg.^2+yg.^2+zg.^2);
figure(1)
subplot(1,2,1)
box = surf(bx,by,bz); %draw box
set(box,'FaceColor',[1 1 1],'FaceAlpha',1,'EdgeAlpha',0,'EdgeColor',[.5 .5 .5])
hold on
camlight(30,70)
contourslice(x,y,z,V,[],[],0) %draw contour
colorbar
axis([-1 1 -1 1 -1 1])
axis equal
subplot(1,2,2)
contour(x,y,V(:,:,3))
axis([-1 1 -1 1])
axis equal
colorbar
set(gcf,'outerposition',[150 150 800 300])
Thanks for your help!
Just set the caxis property as you wish:
colorbar
caxis([0 2])
...
colorbar
caxis([0 2])
The problem was probably caused, because the surf plot changed the color determining values of your plot. By setting a fixed color axis you can avoid all misinterpretations.
I have a plot like this
How can I change the length of y axis to [0 20] in order to have bigger plot size.
Assume I want x axis [0 20]
y axis [0 20]
Immediately after the plot command:
set(gca, 'ylim', [0 20], 'xlim', [0 20]);
I'm plotting 2 subplots, and I want each subplot to have different axis scaling.
My current code is:
subplot(2,1,1)
axis([0 20 0 1])
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
subplot(2,1,2)
axis([0 5 0 1]);
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
But both subplots plot as if no axis was specified.
Any ideas?
Try:
subplot(2,1,1)
plot(t,Ca,'-.',t,Cb,'.',t,Cc);
axis([0 20 0 1])
subplot(2,1,2)
plot(t2,Ca2,'-.',t2,Cb2,'.',t2,Cc2);
axis([0 5 0 1]);
plot will refigure the axes, so you have to call axis AFTER the last plot.