Rolling (rotate and translate) a 2D circle in MATLAB? - matlab

Could you please write a MATLAB Script to animate the motion of the rolling disk for two complete rotations, also showing (as a trace) the trajectory of the point on the rim. Take: radius of the disk equal to 10 units, radius of the point is also equal to 10 units.
Produce a static plot for your system, showing the speed of the rim point using the “quiver”
command.
Here is the code so far:
figure;
%XX=[0 2 4 5 0];
%YY=[1 0 1 5 1];
NN=100;
RR=2;
th=linspace(0,360,NN+1)*pi/180;
XX=RRcos(th);
YY=RRsin(th);
h=patch('XData',XX,... 'YData',YY,'FaceColor',[1 0 0]);
axis([0 10 -2 8]);
axis square
N=100; dX=5/N;
for ii=1:N XX=XX+dX;
set(h,'XData',XX);
drawnow;
pause(0.1)
end

just add one timer and then inside the timer function change the position of circle according the rotation and transform ( if you don't know how to change position then go through with basic geometry concept).

Related

Limitation to MATLAB Hidden Line Removal?

I've asked this question on Mathwork's site so if cross-posting isn't allowed let me know and I'll delete this.
I'm trying to render small objects and large objects together in MATLAB. I'm using the camera commands to restrict my field of view such that I can see both. However, when I do that, the hidden line removal fails on the small objects. I would have thought that hidden line removal would have been done at machine precision for floats, but it appears not. Is this a function of my graphics card or can I work around this?
The code below is the minimum example I could come up with. Plotted on an axis with default limits, the hidden line removal works fine (left) When the axis is set to large extents (compared to the object) the line removal fails (middle). When I make the axis disappear things are fine again (right).
For this example, I can just hide the axis and the output looks correct. But for what I'm actually trying to do, that's not an option. Any suggestions or can someone point me to the proper limit between the smallest and largest objects in a scene that will properly render?
The code to generate the spheres above is below. Thanks in advance!
Images as generated by MATLAB 2018A
clearvars
F1 = figure(1);
clf
set(F1,'color',[1 1 1],'Renderer','opengl'); % have to enable this to clip surfaces behind the camera
for step = [2 1 3] % out of order because the axis in case 2 is trying to hide the first plot
subplot(1,3,step)
view(gca,3);
camproj(gca,'Perspective'); % have to enable this to clip surfaces behind the camera
[Xs,Ys,Zs] = sphere(20);
r = 30e-6;
surf(gca,Xs*r,Ys*r,Zs*r);
axis(gca,'equal');
% three different behaviors, pick a number 1, 2, or 3
switch step
case 1 % this plots the sphere correctly
%axis([-2 2 -2 2 -2 2]);
%axis off
case 2 % this messes up the hidden line removal
axis([-2 2 -2 2 -2 2]);
%axis off
case 3 % removing the axis walls fixes things again
axis([-2 2 -2 2 -2 2]);
axis off
end
% put viewpoint on unit sphere
camera_pos = get(gca,'CameraPosition');
mag_camera_pos = sqrt(sum(camera_pos.^2));
camera_pos = camera_pos / mag_camera_pos;
set(gca,'CameraPosition',camera_pos);
final_angle = 2.5*atand(r/1);
set(gca,'CameraViewAngle',final_angle);
end
drawnow

How to animate rectangles

I am trying to make an animation with circles obtained with the rectangle function in Matlab2013. In order to animate the plot, I tried using clf, drawnow and pause, but it does not seem to work. On the other hand, when I work with dots or lines, I use set and pause and it works fine, but I do not see a way to use these with rectangles.
Here, I show you how I tried to do it with drawnow. There are 1000 time steps, and at every time step I have stored the x and y coordinates of four circles.
%At every time step I would like to plot 4 circles.
PosxProt = rand(1000, 4)
PosyProt = rand(1000, 4)
for i=1:1000
clf
hold on
for j=1:4
rP=0.345; %radius of the circles
cP=[PosxProt(i,j) PosyProt(i,j)]; %center of the circles
rectangle('Position',[cP-rP 2*rP 2*rP],'Curvature',[1 1],'facecolor','r') %plot circle
end
drawnow
pause(0.05)
end
You can parametrize the rectangle using the following equation:
% 2*p and 2*q are the size of the rectangle
t = 0:0.01:1;
x=p*(abs(cos(t))*cos(t)+abs(sin(t))*sin(t))
y=q*(abs(cos(t))*cos(t)-abs(sin(t))*sin(t))
Then plot the rectangle using comet:
comet(x,y)
Also you can find more options for comet here.

Create a colormap in matlab [duplicate]

This question already has answers here:
How to create a custom colormap programmatically?
(2 answers)
Closed 7 years ago.
I have a contour plot with data that goes from -90 to 90 degrees. for now i am using jet so I have a map that looks like this
I have been asked to change the colormap so that instead of having a gradient, I have a fixed color for each 5 degress (so I believe 36 colors). Also i was thinking of maybe having same colors for the interval [5 10] and [-10 -5], and so on if that makes sense.
My code is quite long because i have a lot of data to process, but that's part of it just so you can see what function i am using to plot this
%%
x1=data(:,5); %x location
y1=data(:,16); %y location
z1=phi*90; %angle phi
z2=gamma*90; %angle gamma
n=300; precision of grid
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
figure(3);
contourf(X,Y,griddata(x1,y1,z1,X,Y),100,'EdgeColor', 'None')
%title('Variation of In-plane angle \phi')
axis equal
axis ([0 8000 0 12000])
axis off
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:15:90])
Does anyone know how to create this colorbar?
Cheers
Every colormap-generating function in Matlab, including jet, takes an argument that specifies how many colormap entries there should be. In your case, you want 180 / 5 = 36 discrete colors:
colormap(jet(36))
To make sure the 36 colors cover exactly the 5 degree steps, set the color axis explicitly:
caxis([-90 90])
The result looks e.g. like this:

Ask for plotting rectangular pulse with controlled rising/falling edge

I would like to plot a series of rectangular pulse to a vector data input. The plot profile would create a rising edge of the pulse for a positive number and create the falling edge for the negative of that number. The plot should separate color for each represented number.
For example, if vector input X is [1 -1 2 -2 3 4 1 -4 -1 -3]
Amplitude of data ‘1’ is 5,
Amplitude of data ‘2’ is 4,
Amplitude of data ‘3’ is 3 and
Amplitude of data ‘4’ is 2
So, the input X got index from t(1) to t(10). The output of plot or chart should look like the inserted image
How would I can write a MATLAB code for this work?
Thank you.
B.Bundit
The plot is very small but I think you want something like bar in matlab. Documentation is here. If the vector that you have is changes then you can create a vector of values for plotting using cumsum.
X=[1 -1 2 -2 3 4 1 -4 -1 -3];
Xplot=cumsum([5,X(2:end)]); % //so 5 will be your initial value
Since the plot you give above has different widths of bars, you would also need a vector of the center of each data point and the width of each data point.
means= [1 2 3 4 5 6 7 8 9 10];
widths=[1 1 1 1 1 1 1 2 1 1];
For different color bar plots you can do:
colors=['r','g','k','b','c','m','y','r','g','k'];
for i=1:length(X)
h=bar(means(i),Xplot(i),widths(i));
if i==1, hold on; end
set(h,'FaceColor',colors(i));
end
% //This will label each bar, or you could define your axes before the loop
set(gca,'XTickLabel',means);
You can explore other properties to set here. The class barseries does not have a FaceAlpha property so I'm not sure if you can make them transparent. You could however set the FaceColor to none and have the EdgeColor be set to your color of choice. Note that edge color is specified by an RGB triplet and not a letter code.
You may also need to resort your data by width, so that the widest bars are plotted last and are thus on top. This would look like this:
[widths_sorted,sort_idx]=sort(widths,'ascending');
Xplot_sorted=Xplot(sorted_idx);
means_sorted=means(sorted_idx);

Surf command Axis Scaling

I'm having an issue with moving the datum while plotting a 3D surface using the surf. I have a matrix RES with values < 10^-5 (e.g. RES(30,40)=0.000043245). If I plot this matrix using surf:
surf(RES); axis([0 70 0 70 0 0.0008]);
Then I get a nice smooth image.
Now I need to add a constant value of 10 to the entire matrix and then to plot it with a new datum. So I want the exact same plot as the above figure except I want the z-axis to go from 10 to 10.0008. In this case, all the numbers are more like RES(30,40)=10.000043245.
So I try it:
surf(RES+10); axis([0 70 0 70 10 10.0008]);
And I get this really chunky, blocky output.
Does anyone know why it does this? Both figures should look exactly the same shouldn't they? All I've done is move the z-axis up by 10.