3
votes

I've got a lab where I need to first derrive a transfer function for a fourth order mass spring damper system, then plot this for a 200N step response. The dashpot damping coefficient is a variable and we are to plot a range of values graphically and choose the optimium value i.e (the one closest to a critically damped response).

I have all of the above tasks working fully as expected, and maybe I'm just being dumb here but I can't get the graph legend to behave as desired. I'd ideally like it to say "B="Current Value of B for that itteration of the loop.

%Initialisations
close all;format short g;format compact;clc;clear;
k1=500000;
k2=20000;
m1=20;
m2=400;
opt=stepDataOptions('StepAmplitude',200);
for (b = [1000:1000:15000])
Hs = tf([b k2],[(m1*m2) (b*(m1+m2)) (k2*(m1+m2)+k1*m2) (b*k1) (k1*k2)]);
hold on;
stepplot(Hs,opt)
title("Shock Absorber Performance to Step Input");
ylabel("Force (N)");
legend;
hold off;
end

Thanks in advance :)

1

1 Answers

5
votes

Although not such a clean solution, this will work:

close all;format short g;format compact;clc;clear;

k1=500000;
k2=20000;
m1=20;
m2=400;
opt=stepDataOptions('StepAmplitude',200);

% initialize a cell array that will hold the labels
labels = {};

for (b = [1000:1000:15000])
    Hs = tf([b k2],[(m1*m2) (b*(m1+m2)) (k2*(m1+m2)+k1*m2) (b*k1) (k1*k2)]);
    hold on;
    stepplot(Hs,opt)
    title("Shock Absorber Performance to Step Input");
    ylabel("Force (N)");
    hold off;

    % set the label name, with the value for b
    labels{end+1} = ['B = ' num2str(b)];
end

% create the legend with the labels
legend(labels)