My plot comes out to be discontinuous
2 次查看(过去 30 天)
显示 更早的评论
Hello, I have been trying to get a continuous plot but I couldn't. There are some discontinuities in my plot at the t values which the user inputs. and I couldn't find out the reason. Could you please help me? Thank you.
P.S.: The plot function is at the end of the code.
n = input('Enter the number of points: ');
for j = 1:n
str1 = [' Enter the time value of the point ' num2str(j) ': '];
t(1,j) = input(str1);
end
t
for i = 1:n
str2 = [' Enter the theta value of the point ' num2str(i) ': '];
theta(1,i)=input(str2);
end
theta
for h = 1:n-2 % thetadots
thetadot(1)=0;
if sign(theta(h+1)-theta(h)) ~= sign(theta(h+2)-theta(h+1))
thetadot(h+1)=0;
elseif sign(theta(h+1)-theta(h)) == sign(theta(h+2)-theta(h+1))
thetadot(h+1)=((theta(h+1)-theta(h))/(t(h+1)-t(h))+(theta(h+2)-theta(h+1))/(t(h+2)-t(h+1)))/2;
end
thetadot(n)=0;
end
% theta(t)=a0+a1.*t+a2.*t.^2+a3.*t.^3
%a0=theta0
%a1=theta0dot
%a2=3./(tf.^2)*(thetaf-theta0)-2./tf*theta0dot-1./tf*thetafdot
%a3=-2./(tf.^3)*(thetaf-theta0)+1./(tf.^2)*(thetafdot+theta0dot)
tn=linspace(t(1),t(n),1000);
figure
hold on
for g = 1:n-1 %segments
idx1= (t(g)<=tn & tn<=t(g+1));
s1=theta(g)+thetadot(g).*(tn-t(g))...
+((3/((t(g+1)-t(g))^2)*(theta(g+1)-theta(g))-(2/(t(g+1)-t(g)))*thetadot(g)-(1/(t(g+1)-t(g)))*thetadot(g+1))).*((tn-t(g)).^2) ...
+((-2/((t(g+1)-t(g))^3)*(theta(g+1)-theta(g))+(1/((t(g+1)-t(g))^2))*(thetadot(g+1)+thetadot(g)))).*((tn-t(g)).^3);
plot(tn(idx1),s1(idx1), 'LineWidth',2);
xlabel('t (s)');
ylabel('theta (deg)');
end
0 个评论
采纳的回答
Cris LaPierre
2020-12-5
The problem is that linspace most likely is not creating a vector that only contains the user inputted values at the beginning and end. Let's say t=[2 3 4 5 ... 15]. If I inspect the interval around 3, I see that tn = [... 2.963 2.976 2.989 3.002 3.015 3.028 ...].
When creating indx1, the first segment with end at 2.989 but the second will start at 3.002. Visually, this will appear as a gap between these two points.
Your easiest solution is to create tn inside your for loop for each segment. That way, the end points always match the previous and next segments, and your "resolution" is less affected by the value of n.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!