Hi everybody. I wrote a code but something is going wrong, I need at that point.
I need to stop the code at the gain of 0.5 and I need to take the values that I need as P and D. But The code does not do what I need. It always work around i = 65 and j = 65. What is the error ?
clear all; clc;
Kp = -10:0.31:10;
Kd = -10:0.31:10;
% n = [Kd Kp];
% d = [(1+Kd) (Kp+1)];
%
% [r,p,k] = residue(n,d);
A = zeros(length(Kp), length(Kd));
for i = 1:length(Kp)
for j = 1:length(Kd)
n = [Kd(j) Kp(i)];
d = [(1+Kd(j)) (Kp(i)+1)];
[r,p,k] = residue(n,d);
S = sum(r);
TF = tf(n,d);
[z, gain] = zero(TF);
if gain == 0.5
break;
end
% if S<0.6 && S>0.4
% A(i,j) = 1;
% else
% A(i,j) = 0;
% end
end
end

回答 (1 件)

James Tursa
James Tursa 2018 年 3 月 20 日

0 投票

It is often better to use tolerances for floating point comparisons, unless you wrote the underlying code and you know that the result must be exactly 0.5. So this test
if gain == 0.5
should probably look something like this instead
if abs(gain - 0.5) < tol % for some appropriate value of tol

5 件のコメント

Stephen23
Stephen23 2018 年 3 月 21 日
編集済み: Stephen23 2018 年 3 月 21 日
Berkcan Oz's "Answer" moved here:
What do you mean ? What should I change ?
Stephen23
Stephen23 2018 年 3 月 21 日
編集済み: Stephen23 2018 年 3 月 21 日
@Berkcan Oz: your code contains exactly one instance of the operator ==. Change it as James Tursa recommended.
Berkcan Oz
Berkcan Oz 2018 年 3 月 21 日
What is tol ? How to define ?
Stephen23
Stephen23 2018 年 3 月 21 日
You could start with tol=1e-5;
Berkcan Oz
Berkcan Oz 2018 年 3 月 21 日
編集済み: Walter Roberson 2018 年 3 月 21 日
Now, I did this but this give me a system that starts at gain almost 0.5 and goes 1. I need to make it from 0 to 0.5. Not from 0.5 to 1. What should I do ?
clear all; clc;
Kp = -10:0.31:10;
Kd = -10:0.31:10;
tol = 1e-5;
A = zeros(length(Kp), length(Kd));
for i = 1:length(Kp)
for j = 1:length(Kd)
n = [Kd(j) Kp(i)];
d = [(1+Kd(j)) (Kp(i)+1)];
[r,p,k] = residue(n,d);
S = sum(r);
TF = tf(n,d);
[z, gain] = zero(TF);
if gain <0.6 && gain>0.4
break;
end
end
end
sys = tf(n,d)
step(sys);
grid;

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

質問済み:

2018 年 3 月 20 日

編集済み:

2018 年 3 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by