Mass-Spring-Damper system, how to set limitations for time?

6 views (last 30 days)
I have built a code for a Mass-Spring-Damper system but can't figure out how to define the time on the x-axis plot for 0-5 seconds in 1/10 increments..Any help is appreciated! Code is below..
% Mass-Spring-Damper System
m = 450; % mass of the systemkg; k = 26519.2; % stiffness of the spring (N/m); c = 1000; % damping coefficient (N s/m); z = c/ (2*(k*m)^.5); % damping factor (zeta); T = (2*m)/c; % relaxation time (s); x0 = 0.539567; % initial displacement meters ; v0 = 1.0; % initial velocity (m/s); w0 = (k/m)^.5; % natural frequency (rads/s); np = 1/T; % settling quickness; syms t; % time variable
% Response of MSD System
if z < 1 wd = w0* (1-z^2)^.5; % underdamped system; A = x0; B = (v0+(1/T)*x0)/wd;
xud = exp(-np*t)*(A*cos(wd*t)+B*sin(wd*t)); % displacement x(t)
xudot = diff(xud,t,1); % velocity x'(t) ;
xudoubledot = diff(xud,t,2); % acceleration x''(t) ;
if c == 0
sprintf('Undamped Oscillatory Response of System' )
sprintf('Solution: x(t)= %.5f*cos(%.5f.*t)+%.5f*sin(%.5f.*t)',A,wd,B,wd)
else
sprintf(['Damping Factor is ' num2str(z,3) ' .\nUnderdamped Oscillatory Response of System'])
sprintf('Solution: xud = exp(-%.5ft)(%.5fcos(%.5ft)+%.5fsin(%.5ft)',np, A,wd,B,wd)
end
elseif z == 1
cc1 = x0;
cc2 = v0-np*x0;
xud = cc1*exp(-np*t)+cc2*t*exp(-np*t);
xudot = diff(xud,t,1);
xudoubledot = diff(xud,t,2);
sprintf(['Damping Factor is ' num2str(z,3) ' .\nCritically Damped Oscillatory Response of System'])
sprintf('Solution: xud = %.5f.*exp(-%.5f.*t) + %.5f.*t.*exp(%.5f.*t) ', cc1,np,cc2,np)
else
r1 = (-np + sqrt(np^2 - w0^2));
r2 = (-np - sqrt(np^2 - w0^2));
c1 = (x0*r2-v0)/(r2-r1);
c2 = (v0-c1*r1)/r2;
xud = c1*exp(r1*t) + c2*exp(r2*t);
xudot = diff(xud,t,1);
xudoubledot = diff(xud,t,2);
sprintf(['Damping Factor is ' num2str(z,3) ' .\nOverdamped Transient Response of System'])
sprintf('Solution: xud = %.5f*exp(-%.5f.*t) + %.5f*exp(-%.5f.*t) ', c1,r1,c2,r2)
end
tV = 0:T/500:T; %resultant time vector
%variable substitution of result for t, resulting in data vectorization
xudV = subs(xud,t,tV); xudotV = subs(xudot,t,tV); xudoubledotV = subs(xudoubledot,t,tV);
% Tables
columnheader = {'Time','Disp','Spd','Acc'};
%matrixresults
trix = [tV' double(xudV)' double(xudotV)' double(xudoubledotV)']; format short
disp(columnheader) disp(trix)
% Excel results
exlFile = 'table.xlsx'; xlswrite(exlFile, trix)
save trix.out trix -ASCII
%plotting
figure plot(tV, xudV, 'b', tV, xudotV, 'r--', tV, xudoubledotV, 'm:') title('Displacement, Speed, and Acceleration Simple MSD Systems') xlabel('Time (seconds)'); ylabel(' Displacement (m), Velocity (m/s), Acceleration (m/s^2)');
%legend
legend({'Displacement ','Velocity ','Acceleration '}, 'Location', 'northeast');

Answers (1)

Star Strider
Star Strider on 3 Feb 2015
In an ideal world, it would be possible to define the 'XMinorTick' locations. This isn’t an ideal world, so this will have to do in the interim:
x = rand(1, 50)*5; % Create Data
y = rand(1, 50); % Create Data
figure(1)
plot(x, y, 'bp') % Plot Your ODE Solution Here
hold on
tl = diff(ylim)*0.025;
plot([0:5; 0:5], [zeros(1,6); ones(1,6)]*tl, 'k', 'LineWidth',0.5)
hold off
xtlbl = repmat({' '}, 1, 51);
xtlbl([0:10:50]+1) = {'0'; '1'; '2'; '3'; '4'; '5'};
set(gca, 'XTick', [0:0.1:5], 'XTickLabel',xtlbl)
axis([0 5 ylim])
I did not run your code, but defaulted to simply addressing th plot issue.

Categories

Find more on Graphics 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!