swoopin_swallow
swoopin_swallow

Reputation: 207

How to implement similar statements in matlab using a loop?

Look at the code snippet here in the mathworks documentation,

Digital Servo Control of a Hard-Disk Drive

Gf1 = tf(w1*[a1 b1*w1],[1 2*z1*w1 w1^2]); % first  resonance
Gf2 = tf(w2*[a2 b2*w2],[1 2*z2*w2 w2^2]); % second resonance
Gf3 = tf(w3*[a3 b3*w3],[1 2*z3*w3 w3^2]); % third  resonance
Gf4 = tf(w4*[a4 b4*w4],[1 2*z4*w4 w4^2]); % fourth resonance

my question is, how can I implement the above statements within a loop like,

% pseudo code

for i = 1:4

Gf%d = tf(w%d*[a%d b%d*w%d],[1 2*z%d*w%d w%d^2]); i

and then execute the result in matlab?

Upvotes: 0

Views: 88

Answers (3)

hwrdprkns
hwrdprkns

Reputation: 7585

The best way to do this is to use arrays.

for i = 1:n
  trans(i) = % your stuff here %
end

Then just replace the different variables with the correct array indexes.

Upvotes: 0

Isaac
Isaac

Reputation: 3616

You could either use eval:

for i = 1:4
    eval(sprintf('Gf%d = tf(w%d*[a%d b%d*w%d],[1 2*z%d*w%d w%d^2]);', i));
end

or you could turn your parameters into arrays. Arrays would be more efficient, if you have control over how you format your data.

Upvotes: 0

tmpearce
tmpearce

Reputation: 12693

Here's one option:

w = [w1 w2 w3 w4];
%# same thing for a, b, d...

for i=1:4
    Gf(i) = tf(w(i)*[a(i) b(i)*w(i)],[1 2*z(i)*w(i) w(i)^2]); % ith  resonance
end

Upvotes: 1

Related Questions