I want to add some arrows (direction) to my plot like this picture:
I used Arrowhead and arrowPlot but I have some errors.
a1 = 1.05;
b = 0.29;
c = 0.3;
d= 0.31;
L1= 0.002;
L2= 0.008;
L3=0.0075;
L4 =0.006;
M1 = 0.0023;
N1 = 0.0035;
P1 = 0.003;
eta = [60 30 30 30];
F = #(t,y) [
a1*y(1) - L1*y(1)*y(1) - L2*y(1)*y(2) - L3*y(1)*y(3) - L4*y(1)*y(4);
-b*y(2) + M1*y(2)*y(1);
-c*y(3) + N1*y(3)*y(1);
-d*y(4) + P1*y(4)*y(1)];
[t,y] = ode45(F,(0: (300/1000000): 300), eta);
plot(y(:,1),y(:,2)+y(:,3)+y(:,4), 'LineWidth',2)
You can use "annotation('arrow',x,y)" command. Below I added some arrows to your plot. You can add some more but only remember that X and Y values must be between 0 and 1.
f1=[0 0 1];%Arrow's Color (Blue)
a1 = 1.05;
b = 0.29;
c = 0.3;
d= 0.31;
L1= 0.002;
L2= 0.008;
L3=0.0075;
L4 =0.006;
M1 = 0.0023;
N1 = 0.0035;
P1 = 0.003;
eta = [60 30 30 30];
F = #(t,y) [
a1*y(1) - L1*y(1)*y(1) - L2*y(1)*y(2) - L3*y(1)*y(3) - L4*y(1)*y(4);
-b*y(2) + M1*y(2)*y(1);
-c*y(3) + N1*y(3)*y(1);
-d*y(4) + P1*y(4)*y(1)];
[t,y] = ode45(F,(0: (300/1000000): 300), eta);
plot(y(:,1),y(:,2)+y(:,3)+y(:,4), 'LineWidth',2)
%Plotting Arrows%
annotation('arrow', [.56 .58], [.221 .223],'color',f1);%left to right arrow
annotation('arrow', [.35 .37], [.221 .223],'color',f1);% /// ///
annotation('arrow', [.37 .35], [.701 .703],'color',f1); %right to left arrow
annotation('arrow', [.74 .72], [.701 .723],'color',f1);
Related
This is a analog clock. It runs perfectly. But once I close the clock, another figure opens up, and only the clock hands show. This makes it difficult to stop the program. How can I stop the program?
x=0;y=0;r=10;
hold on;
theta = 0:pi/60:2*pi;
xc = r * cos(theta);
yc = r * sin(theta);
h = plot(xc,yc,'r','linewidth',4);
axis off
r=9; i=1;
set(gca,'FontWeight','bold');
for theta = pi/6: pi/6: 2*pi
y1 = r * cos(theta);
x1 = r * sin(theta);
plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 1])
text(x1/9*9.5,y1/9*9.5,num2str(i),'color',[0 0 1]);
i=i+1;
end
for theta=pi/30 : pi/30 : 2*pi
y1 = 10 * cos(theta);
x1 = 10 * sin(theta);
plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 0])
end
while(1)
tic
c = clock;
c = c(1,4:6);
minute =c(1,2); sec=c(1,3);
if (c(1,1)>12)
hr = c(1,1)-12;
else
hr = c(1,1);
end
min1 = ceil(minute/12);
theta = (hr*pi)/6 + (min1*pi)/30;
f=figure(1); hold on;
y1 = 3 * cos(theta); Yhr = [0 y1];
x1 = 3 * sin(theta); Xhr = [0 x1];
hrhnd=plot(Xhr,Yhr);hold on;
theta1 = (minute*pi)/30;
y2 = 4.5 * cos(theta1); Ymin = [0 y2];
x2 = 4.5 * sin(theta1); Xmin = [0 x2];
minhnd=plot(Xmin,Ymin);
theta2 = (sec*pi)/30;
y3 = 5 * cos(theta2); Ysec = [0 y3];
x3 = 5 * sin(theta2); Xsec = [0 x3];
sechnd=plot(Xsec,Ysec);
z=toc;
pause(1-z);
delete(sechnd);
delete(minhnd);
delete(hrhnd);
end
The simplest way to stop the script when you close the window is to have the script test inside its loop if the window still exists.
We start the script by creating a figure window and recording its handle:
fig = figure;
Next, in the loop, we check to see if the window still exists using ishandle:
while(ishandle(fig))
...
end
The full program:
x=0;y=0;r=10;
fig = figure; %!!! NEW LINE
hold on;
theta = 0:pi/60:2*pi;
xc = r * cos(theta);
yc = r * sin(theta);
h = plot(xc,yc,'r','linewidth',4);
axis off
r=9; i=1;
set(gca,'FontWeight','bold');
for theta = pi/6: pi/6: 2*pi
y1 = r * cos(theta);
x1 = r * sin(theta);
plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 1])
text(x1/9*9.5,y1/9*9.5,num2str(i),'color',[0 0 1]);
i=i+1;
end
for theta=pi/30 : pi/30 : 2*pi
y1 = 10 * cos(theta);
x1 = 10 * sin(theta);
plot([x1/9*8 x1/9*7],[y1/9*8 y1/9*7],'color',[0 0 0])
end
while(ishandle(fig)) %!!! UPDATED LINE
tic
c = clock;
c = c(1,4:6);
minute =c(1,2); sec=c(1,3);
if (c(1,1)>12)
hr = c(1,1)-12;
else
hr = c(1,1);
end
min1 = ceil(minute/12);
theta = (hr*pi)/6 + (min1*pi)/30;
f=figure(1); hold on;
y1 = 3 * cos(theta); Yhr = [0 y1];
x1 = 3 * sin(theta); Xhr = [0 x1];
hrhnd=plot(Xhr,Yhr);hold on;
theta1 = (minute*pi)/30;
y2 = 4.5 * cos(theta1); Ymin = [0 y2];
x2 = 4.5 * sin(theta1); Xmin = [0 x2];
minhnd=plot(Xmin,Ymin);
theta2 = (sec*pi)/30;
y3 = 5 * cos(theta2); Ysec = [0 y3];
x3 = 5 * sin(theta2); Xsec = [0 x3];
sechnd=plot(Xsec,Ysec);
z=toc;
pause(1-z);
delete(sechnd);
delete(minhnd);
delete(hrhnd);
end
You could improve your program by not deleting and re-drawing the hands, but updating their position. You'd do hrhnd=plot(Xhr,Yhr); before the loop, to draw the hand in its initial position, and then set(hrhnd,'XData',Xhr,'YData', Yhr) to update its position.
You could also do axis equal after drawing the clock face, to ensure it is round.
Note that you only need to give hold on once at the top, it is not needed after every plot command.
How can I use the secant method in order to calculate the angle b(between the positive x-axis and negative y-axis) I have to throw a ball for it to land on the x-axis, with these conditions:
the ball is thrown from origo, at a height z=1.4 m, with the and angle a (for the horizontal plane) 30◦, with a speed of 25 m / s. The air resistance coefficient is c = 0.070 and the wind force is 7 m / s at the ground which increases with the height according to: a(z) = 7 + 0.35z.
The motion of the ball is described with the following:
x''=−qx', y''=−q(y'−a(z)), z''=−9.81−qz', q=c*sqrt(x'^2+(y'−a(z))^2+z'^2)
I did a variable substitution (u) and then used RK4 to calculate the ball's motion, however I can't figure out how to use the secant method to find the angle b. The problem is that when I plot with these start and guessing values is that the ball doesn't land on the x-axis:
clear all, close all, clc
a = pi/3; %start angle
c = 0.07; % air resistance coeff.
v0 = 25; %start velocity, 25 m/s
t = 0; %start time
h = 0.1; % 0.1 second step
b = 0
x0 = 0; xPrim = v0*sin(a)*cosd(b);
y0 = 0; yPrim = v0*sin(a)*sind(b);
z0 = 1.4; zPrim = v0*cos(a);
u = [x0 xPrim y0 yPrim z0 zPrim]';
uVek = u';
% Secant method
b0 = 270; % Start guess nr 1
b1 = 360; % Start guess nr 2
f0 = funk (b0);
db = 1;
while abs(db) > 0.5e-8
f1 = funk (b1);
db = f1 * (b1 - b0) / (f1 - f0);
b0 = b1; % Updates b0
f0 = f1; % Updates f0
b1 = b1 - db % new b
end
while u(5) >= 0 && u(3)<=0
f1 = FRK4(t,u);
f2 = FRK4(t+h/2,u+(h/2)*f1);
f3 = FRK4(t+h/2,u+(h/2)*f2);
f4 = FRK4(t+h,u+h*f3);
f = (f1 + 2*f2 + 2*f3 + f4)/6;
u = u + h*f;
uVek = [uVek; u'];
t = t+h;
end
x = uVek(:,1)'; y = uVek(:,3)'; z = uVek(:,5)';
plot3(x,y,z)
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on
where FRK4 is a function:
function uPrim = FRK4(t,u)
c = 0.07;
az = 7+0.35*(u(5));
q = c*sqrt(u(2)^2 +(u(4)- az)^2 + u(6)^2);
uPrim = [u(2) -q*u(2) u(4) -q*(u(4) - az) u(6) -9.81-q*u(6)]';
end
where funk is a function:
function f = funk(b)
a = pi/3
v0 = 25
x0 = 0; xPrim = v0*sin(a)*cosd(b);
y0 = 0; yPrim = v0*sin(a)*sind(b);
z0 = 1.4; zPrim = v0*cos(a);
f = [x0 xPrim y0 yPrim z0 zPrim]';
end
I want to move a red star marker along the spiral trajectory with an equal distance of 5 units between the red star points on its circumference like in the below image.
vertspacing = 10;
horzspacing = 10;
thetamax = 10*pi;
% Calculation of (x,y) - underlying archimedean spiral.
b = vertspacing/2/pi;
theta = 0:0.01:thetamax;
x = b*theta.*cos(theta)+50;
y = b*theta.*sin(theta)+50;
% Calculation of equidistant (xi,yi) points on spiral.
smax = 0.5*b*thetamax.*thetamax;
s = 0:horzspacing:smax;
thetai = sqrt(2*s/b);
xi = b*thetai.*cos(thetai);
yi = b*thetai.*sin(thetai);
plot(x,y,'b-');
hold on
I want to get a figure that looks like the following:
This is my code for the circle trajectory:
% Initialization steps.
format long g;
format compact;
fontSize = 20;
r1 = 50;
r2 = 35;
r3= 20;
xc = 50;
yc = 50;
% Since arclength = radius * (angle in radians),
% (angle in radians) = arclength / radius = 5 / radius.
deltaAngle1 = 5 / r1;
deltaAngle2 = 5 / r2;
deltaAngle3 = 5 / r3;
theta1 = 0 : deltaAngle1 : (2 * pi);
theta2 = 0 : deltaAngle2 : (2 * pi);
theta3 = 0 : deltaAngle3 : (2 * pi);
x1 = r1*cos(theta1) + xc;
y1 = r1*sin(theta1) + yc;
x2 = r2*cos(theta2) + xc;
y2 = r2*sin(theta2) + yc;
x3 = r3*cos(theta3) + xc;
y3 = r3*sin(theta3) + yc;
plot(x1,y1,'color',[1 0.5 0])
hold on
plot(x2,y2,'color',[1 0.5 0])
hold on
plot(x3,y3,'color',[1 0.5 0])
hold on
% Connecting Line:
plot([70 100], [50 50],'color',[1 0.5 0])
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
drawnow;
axis square;
for i = 1 : length(theta1)
plot(x1(i),y1(i),'r*')
pause(0.1)
end
for i = 1 : length(theta2)
plot(x2(i),y2(i),'r*')
pause(0.1)
end
for i = 1 : length(theta3)
plot(x3(i),y3(i),'r*')
pause(0.1)
end
I can't think of a way to compute distance along a spiral, so I'm approximating it with circles, in hopes that it will still be useful.
My solution relies on the InterX function from FEX, to find the intersection of circles with the spiral. I am providing an animation so it is easier to understand.
The code (tested on R2017a):
function [x,y,xi,yi] = q44916610(doPlot)
%% Input handling:
if nargin < 1 || isempty(doPlot)
doPlot = false;
end
%% Initialization:
origin = [50,50];
vertspacing = 10;
thetamax = 5*(2*pi);
%% Calculation of (x,y) - underlying archimedean spiral.
b = vertspacing/(2*pi);
theta = 0:0.01:thetamax;
x = b*theta.*cos(theta) + origin(1);
y = b*theta.*sin(theta) + origin(2);
%% Calculation of equidistant (xi,yi) points on spiral.
DST = 5; cRes = 360;
numPts = ceil(vertspacing*thetamax); % Preallocation
[xi,yi] = deal(NaN(numPts,1));
if doPlot && isHG2() % Plots are only enabled if the MATLAB version is new enough.
figure(); plot(x,y,'b-'); hold on; axis equal; grid on; grid minor;
hAx = gca; hAx.XLim = [-5 105]; hAx.YLim = [-5 105];
hP = plot(xi,yi,'r*');
else
hP = struct('XData',xi,'YData',yi);
end
hP.XData(1) = origin(1); hP.YData(1) = origin(2);
for ind = 2:numPts
P = InterX([x;y], makeCircle([hP.XData(ind-1),hP.YData(ind-1)],DST/2,cRes));
[~,I] = max(abs(P(1,:)-origin(1)+1i*(P(2,:)-origin(2))));
if doPlot, pause(0.1); end
hP.XData(ind) = P(1,I); hP.YData(ind) = P(2,I);
if doPlot, pause(0.1); delete(hAx.Children(1)); end
end
xi = hP.XData(~isnan(hP.XData)); yi = hP.YData(~isnan(hP.YData));
%% Nested function(s):
function [XY] = makeCircle(cnt, R, nPts)
P = (cnt(1)+1i*cnt(2))+R*exp(linspace(0,1,nPts)*pi*2i);
if doPlot, plot(P,'Color',lines(1)); end
XY = [real(P); imag(P)];
end
end
%% Local function(s):
function tf = isHG2()
try
tf = ~verLessThan('MATLAB', '8.4');
catch
tf = false;
end
end
function P = InterX(L1,varargin)
% DOCUMENTATION REMOVED. For a full version go to:
% https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections
narginchk(1,2);
if nargin == 1
L2 = L1; hF = #lt; %...Avoid the inclusion of common points
else
L2 = varargin{1}; hF = #le;
end
%...Preliminary stuff
x1 = L1(1,:)'; x2 = L2(1,:);
y1 = L1(2,:)'; y2 = L2(2,:);
dx1 = diff(x1); dy1 = diff(y1);
dx2 = diff(x2); dy2 = diff(y2);
%...Determine 'signed distances'
S1 = dx1.*y1(1:end-1) - dy1.*x1(1:end-1);
S2 = dx2.*y2(1:end-1) - dy2.*x2(1:end-1);
C1 = feval(hF,D(bsxfun(#times,dx1,y2)-bsxfun(#times,dy1,x2),S1),0);
C2 = feval(hF,D((bsxfun(#times,y1,dx2)-bsxfun(#times,x1,dy2))',S2'),0)';
%...Obtain the segments where an intersection is expected
[i,j] = find(C1 & C2);
if isempty(i), P = zeros(2,0); return; end
%...Transpose and prepare for output
i=i'; dx2=dx2'; dy2=dy2'; S2 = S2';
L = dy2(j).*dx1(i) - dy1(i).*dx2(j);
i = i(L~=0); j=j(L~=0); L=L(L~=0); %...Avoid divisions by 0
%...Solve system of eqs to get the common points
P = unique([dx2(j).*S1(i) - dx1(i).*S2(j), ...
dy2(j).*S1(i) - dy1(i).*S2(j)]./[L L],'rows')';
function u = D(x,y)
u = bsxfun(#minus,x(:,1:end-1),y).*bsxfun(#minus,x(:,2:end),y);
end
end
Result:
Note that in the animation above, the diameter of the circle (and hence the distance between the red points) is 10 and not 5.
I have two binary circles of one object.They don't connect together. I finished fill color for a circle. Now, I want to fill same color into two circles that don't connect together. This is my code. But it does work. Could you edit help me please?
figure
[xx yy] = meshgrid(1:384,1:384);
phi1 = (sqrt(((xx - 190).^2 + (yy - 260).^2 )) - 15);
phi2 = (sqrt(((xx - 270).^2 + (yy - 200).^2 )) - 20);
phi= sign(phi1).*sign(phi2);
phi(phi==-1)=0;
imshow(phi);
cont = contourc(phi, [0 0])';
cont = cont(2 : end, :);
patch(cont(:, 1), cont(:, 2), 'g', 'EdgeColor', 'w')
Here is a little script to draw a filled circle. You can make it into a function and use hold on and hold off to draw many circles.
r = 2;
xc = 1;
yc = 1;
st = r/100;
x1 = (-r+xc):st:(r+xc);
y1 = yc+sqrt(r^2-(x1-xc).^2);
x2 = x1(end:-1:1);
y2 = yc-sqrt(r^2-(x2-xc).^2);
x = [x1 x2];
y = [y1 y2];
patch(x,y,'g','FaceColor', 'g');
If you want to create a more general shape using implicit definition of the curve, the following works too:
[xx, yy] = meshgrid(1:384,1:384);
phi1 = 15^2 < ((xx-190).^2 + (yy-260).^2);
phi2 = 20^2 < ((xx-270).^2 + (yy-200).^2);
phi = phi1 .* phi2;
contourf(phi, [0,1]);
colormap([0,1,0;1,1,1]);
I have two lines y1 = -a1*x1 + c1 for theta =30 and y1 = -a2*x1 + c2 for theta = 45
is it possible to interpolate a equation for y1 for theta between 30 and 45 in matlab ? The lines are almost parallel to each other. Anyone has an easy way of doing this?
You can interpolate the coeff a and c:
w = (theta - 30) / (45 - 30 ); % w = 0 for theta = 30 and w = 1 for theta = 45
aTheta = a2 * w + a1 * ( 1 - w );
cTheat = c2 * w + c1 * ( 1 - w );
yTheta = -aTheta * x + cTheta * y;
x = 1:10;
a30 = 1;
a45 = 1.1;
c30 = 0;
c45 = 3;
y30 = -a1*x + c1;
y45 = -a2*x + c2;
Now to find y40 we can just interpolate the curve parameters (i.e. slope (a) and offset (c))
a40 = interp1([30,45], [a30, a45], 40);
c40 = interp1([30,45], [c30, c45], 40);
And now our interpolated y40 is
y40 = -a40*x + c40;
plot(x,y30,x,y45,x,y40);