My plot comes out to be discontinuous
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!