clear % Clear variables and functions from memory.
g = 9.8;  % [m/s^2]
% Mass of office chair seat
m = 5;  % [kg]
% Mass of person taking a seat on the office chair
M = 75;  % [kg]
% Damping constant, value has to be entered before running simulation
b1 = 400;
b2 = 1200;
b3 = 2400;
% Spring constant
k = 25920;
% Simulation duration in seconds
simtime = 3;  % [seconds]
% Väljer parametrar för simuleringarna så att de plottade kurvorna blir
% släta. Exempelvis feltoleranser.
optionvec1 = simset('AbsTol',1e-9, 'RelTol', 1e-6);
optionvec2 = simset(optionvec1,'MaxStep',simtime/500);

% Vi simulerar systemet.
b = b1;
simOut = sim('OfficeChairIntegratorsNiklasRygart', simtime, optionvec2);
yout1 = simOut.yout;
% Vi simulerar systemet en andra gång.
b = b2;
simOut = sim('OfficeChairIntegratorsNiklasRygart', simtime, optionvec2);
yout2 = simOut.yout;
% Vi simulerar systemet en tredje gång.
b = b3;
simOut = sim('OfficeChairIntegratorsNiklasRygart', simtime, optionvec2);
yout3 = simOut.yout;
% Plot the simulation result, time versus deviation of seat from resting
% position. Here the deviation is scaled form meters to centimetres
figure(1)
hold off
plot(yout1*100)
hold on
plot(yout2*100)
hold on
plot(yout3*100)
hold off
% Label of the plot
title(['Currently used damping constants, b= (',num2str(b1),', ', ...
num2str(b2),', ', num2str(b3), ') [N.s/m]'])
% Guide text of y-axis
ylabel('Deviation from resting position [cm]')
% Guide text of x-axis
xlabel('Time [sec]')


% Denna del ar överkurs. Har gors ytterligare ett diagram som visar några
% olika satt att kontrollera ett diagrams utseende.
% Se dokumentation: Documentation Home -> MATLAB -> Graphics ->
% -> Formatting and Annotation
figure(2)
t1 = yout1.Time;
x1 = yout1.Data;
t2 = yout2.Time;
x2 = yout2.Data;
t3 = yout3.Time;
x3 = yout3.Data;
lines = plot(t1, x1*100, t2, x2*100, t3, x3*100);
lines(1).LineWidth = 1.5;
lines(1).Color = 'blue';
lines(2).LineWidth = 1.5;
lines(2).Color = 'red';
lines(3).LineWidth = 1.5;
lines(3).Color = 'magenta';
% Label of the plot
rowstr1 = 'Currently used damping constants';
rowstr2 = ['b_1 = ',num2str(b1),', ',...
' b_2 = ',num2str(b2),', ', ' b_3 = ',num2str(b3), ...
' [N \cdot s/m]'];
title({rowstr1, rowstr2}, 'FontSize',14)
% Kommentar fran hosten 2018.
% Foljande kommando fungerar inte pa alla datorer. Darfor har det
% kommenterats bort och ersatts av ett som fungerar pa alla datorer.
% legend('b_1', 'b_2', 'b_3', 'location', 'southeast', 'FontSize',14)
legend('b_1', 'b_2', 'b_3', 'location', 'southeast');
text(1, 0.04*100, 'Att göra denna figur är överkurs', 'FontSize',14)
% Guide text of y-axis
ylabel('Deviation from resting position [cm]', 'FontSize',14)
% Guide text of x-axis
xlabel('Time [sec]', 'FontSize',16)
ax = gca;
ax.FontSize = 12;
% Bring the plot window to the foreground
shg
