3 views (last 30 days)

KSSV
on 8 Dec 2016

You write a function with the code which you ran for one data set....to the function make inputs which are your data sets. The outputs should be your required variables which you want to plot. Now, pick your data set and run a loop, call this function in the loop. Take the output of all the data sets, do the necessary initialization. Once you have all the data in your hand, then you can plot the results. You can save the figure using saveas. If you feel you dont want to save your data then, you can plot the results immediately with in the loop and save the figure.

It is a pretty straight forward job, try using functions in matlab, it makes life easy. If you face any difficulty post your code here.

Melissa McCabe
on 8 Dec 2016

I have a code that I can run for each one separately and they run fine. But I need the program to output like 5 different damping ratios and 5 different percent overshoot. So I added a for loop to the beginning but it only puts out the last answer for each. Maybe you could tell me how to tell matlab to save each answer as like Zeta1, Zeta2, etc. My code is posted below. Thank you for your help.

for Time = Time_1, Response = Response_1;

for Time = Time_2, Response = Response_2;

for Time = Time_3, Response = Response_3;

for Time = Time_4, Response = Response_4;

for Time = Time_5, Response = Response_5;

%Plot the data on a graph with labesl

figure(1)

subplot(1,3,1)

plot(Time,Response)

axis([0,5,0,1.2])

title('Original Data')

xlabel('Time [s]')

ylabel('Response')

y = smooth(Response,20);

subplot(1,3,2)

plot(Time,y)

axis([0,5,0,1.2])

title('Smooth Data')

xlabel('Time [s]')

ylabel('Response')

S = stepinfo(Response,Time)

% USING THIS CODE TO PLOT AND SOLVE

% OUTPUT SAYS %O.S. IS 2.8571, BUT AFTER INSPECTION OF THE GRAPH, THIS IS

% WRONG. THEREFORE, THE FOLLOWING MUST BE CALCULATED INDIVIDUALLY.

%Rise Time

yr=.95;

rise_idx=min(find(y>=yr));

RiseT1 = Time(rise_idx)

%b)Peak Time

pks = findpeaks(y);

m = max(pks);

PeakT1 = Time(y == m)

%c) Percent Overshoot

OS = (m-1)/1

%d) 5% Settling Time

settle5_idx = max(find(y>=1.05|y<=.95));

ys = y(settle5_idx);

Settle5T1 = Time(settle5_idx)

%e) 2% Settling Time

settle2_idx = max(find(y>=1.02|y<=.98));

ys = y(settle2_idx);

Settle2T1 = Time(settle2_idx)

%f 1% Settling Time

settle1_idx = max(find(y>=1.01|y<=.99));

ys = y(settle1_idx);

Settle1T1 = Time(settle1_idx)

%g) Damping Ratio

% FORCE THE %O.S. TO BE AS CLOSE TO ZERO AS POSSIBLE

OS1 = .000000000000000001

NUM = abs(log(OS1/100));

DEN = sqrt(((pi())^2)+((log(OS1/100))^2));

Zeta = NUM/DEN

% ZETA COMES OUT TO BE 0.9977 AND WE KNOW FROM CLASS THAT THIS IS A

% CRITICALLY DAMPED SYSTEM. THEREFORE, WE WILL ROUND ZETA TO THE NEAREST

% WHOLE NUMBER TO ENSURE THE FORM OF RESPONSE COMES OUT CORRECTLY

Zeta1 = round(Zeta)

%h) Natural Frequency

Wn = 4/(Zeta1*Settle2T1)

%i) Damped Frequency

Wd = Wn*sqrt(1-(Zeta1)^2)

%j) Form of Response

for c=Zeta1

if c == 1

disp('Critically damped')

elseif c == 0

disp('Undamped')

elseif c > 1

disp('Overdamped')

else 0 < c < 1

disp('Underdamped')

end

end

%k) Transfer Function

h = tf([Wn^2],[1,2*Zeta1*Wn,Wn^2])

figure(1)

subplot(1,3,3)

step(h)

a = stepinfo(h)

axis([0,5,0,1.2])

end

end

end

end

end

Chaya N
on 8 Dec 2016

The loop scheme you have here is not serving any purpose. I suggest creating a function out of the lines of code that you have INSIDE the 5 for loops. Please refer the function page here.

Identify the inputs and outputs that you require for each set of data and use it to create the function. Call this function individually for each dataset. Do not number any of the figures inside the function. If all your plot (and/or subplot) data are available under your list of outputs for each set, it would also be best not to plot any figures inside the function at all. Once you have the results for all the datasets, you could simply pull up separate figures and do your plots in the end.

For example, say you are analyzing your third dataset. Let's say the inputs you pass are Response and Time, and the outputs you require are damping ratio ( Zeta), natural frequency ( Wn), damped frequency ( Wd) and transfer function ( h) The following is a big picture of what you would be doing:

% call function with corresponding inputs and outputs

[Zeta_3, Wn_3, Wd_3, h_3] = your_function_name(Response_3, Time_3)

Note the numbers attached to the input and output variables. These help you keep track of the dataset that you are currently working with. When defining the function, you do not need any such number referencing since you will be running all datasets through the same function. When plotting your graphs (outside the function), call up separate figure windows. For the third dataset above, the plot would be:

figure;

subplot(1,3,1), plot(Time_3, Response_3);

subplot(1,3,2), plot(Time_3, smooth(Response_3,20));

subplot(1,3,3), step(h_3);

I have skipped the axes, titles and labels here but you could insert them as you normally would.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/315908-trouble-running-5-separate-data-sets-through-one-code-and-saving-the-answers-for-each-data-set#comment_412169

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/315908-trouble-running-5-separate-data-sets-through-one-code-and-saving-the-answers-for-each-data-set#comment_412169

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/315908-trouble-running-5-separate-data-sets-through-one-code-and-saving-the-answers-for-each-data-set#comment_412323

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/315908-trouble-running-5-separate-data-sets-through-one-code-and-saving-the-answers-for-each-data-set#comment_412323

Sign in to comment.