MATLAB Answers

Trouble running 5 separate data sets through one code and saving the answers for each data set.

3 ビュー (過去 30 日間)
Melissa
Melissa 2016 年 12 月 8 日
回答済み: Chaya N 2016 年 12 月 8 日
I have a code that calculates certain variables such as damping ratio and natural frequency for a set of data. I have also made graphs to correspond to my results. I can successfully run one data set all the way through and get the answers I am looking for. However, I have been given 5 sets of data and need to automatically run each set of data through the code, save each variable for each case, and save all of the graphs for each case. I tried using 5 stacked for loops but in the end I am only left with the variables and the graphs from the last data set. Is there an easy way to run each set of data through and end up with 5 separate plots?

  2 件のコメント

Chaya N
Chaya N 2016 年 12 月 8 日
Could you please post your code here?
If you are reusing variable names for your 5-stacked-loops case then you would only be left with data from the last set. Try calling the output variables from each loop with different names.
Also, are all these data sets of the same size?
Melissa McCabe
Melissa McCabe 2016 年 12 月 8 日
I think they are the same size. I am reusing the variable names for each run right now. I tried to change that too but I don't know how to tell MATLAB to write one variable to one data set and rewrite it for another. Below is my code. Thank you so much 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 labes
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
%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
NUM = abs(log(OS/100));
DEN = sqrt(((pi())^2)+((log(OS/100))^2));
Zeta = NUM/DEN
%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

サインイン to comment.

回答 (2 件)

KSSV
KSSV 2016 年 12 月 8 日
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.

  1 件のコメント

Melissa McCabe
Melissa McCabe 2016 年 12 月 8 日
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

サインイン to comment.


Chaya N
Chaya N 2016 年 12 月 8 日
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.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by