how to vectorize this "For Loop" ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
nelson
am 16 Jul. 2017
Beantwortet: Walter Roberson
am 16 Jul. 2017
close all
clear all
clc
tic;
for k=1:0.5:10;
for a=1:0.5:5;
num=k;
den=[1 a 0];
sys=tf(num,den);
bode(sys);grid on;hold on;
end
end
time=toc
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Jul. 2017
k=1:0.5:10;
a=1:0.5:5;
[K, A] = ndgrid(k, a);
Numerator = num2cell(K(:));
nusz = size(Numerator);
Denominator = num2cell( [ones(nusz), A(:), zeros(nusz)], 2);
tfs = tf(Numerator, Denominator);
Now at this point you have two choices:
1)
bode(tfs); grid on
This is not the same as your original code: it produces an array of plots. To be honest, it is not understandable even in full screen mode -- it just looks like a series of straight lines across the screen. However, the execution time is not so bad considering.
2)
for K = 1 : 171; bode(tfs(K)); hold on; end; grid on
This is the same as your original code. It would create a single pair of plots with 171 lines each (but most of which overlay each other.) This takes quite a while to execute.
The drawing of the plots takes much much longer than the creation of the transfer functions -- to the point where it is hardly even worth bothering to vectorize the creation of the transfer functions considering the loss in clarity about what is being plotted.
0 Kommentare
Weitere Antworten (1)
scadaprog
am 16 Jul. 2017
Hi, This is one way of doing it:
k=1:.5:10; a=1:.5:5;
num=repmat(k',numel(a),1);
a = [ones(numel(a),1) a' zeros(numel(a),1)];
den = repmat(a, numel(k),1);
sys = tf(num2cell(num),num2cell(den,2));
bode(sys);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Time and Frequency Domain Analysis finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!