clc
clear all
t = [-0.095; -3.953; -7.52; 7.59; 37.66]
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
for i=1:length(t)
ti=t(i)
X(i) = m0 + m1*ti + m2*ti^2 + m3*ti^3 + m4*ti^4 + m5*ti^5 + m6*ti^6;
Y(i) = g0 + g1*ti + g2*ti^2 + g3*ti^3 + g4*ti^4 + g5*ti^5;
Z(i) = X(i)/Y(i);
if Z(i) > 0
wc(i) = sqrt(R22/Z(i))
td(i) = (2/wc(i))*atan(wc(i)*ti);
else
return
end
td = min(td(j))
end
The for loop is intended to compute values of Z(i) as per corresponding values of t (i). Then, if the value of Z(i) is positive value, only then the values of wc(i) and td(i) going to be computed. For all possible values of td(i), the chosen value going to be the minimum of all.

 채택된 답변

DGM
DGM 2021년 6월 3일
편집: DGM 2021년 6월 3일
I don't know what this is trying to do, so here goes.
t = [-0.095; -3.953; -7.52; 7.59; 37.66];
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
% preallocate, otherwise vectors will have mismatched length
% and indexing errors will happen
N = numel(t);
X = zeros(N,1);
Y = zeros(N,1);
Z = zeros(N,1);
wc = NaN(N,1);
td= NaN(N,1);
for i=1:N
ti=t(i);
X(i) = m0 + m1*ti + m2*ti^2 + m3*ti^3 + m4*ti^4 + m5*ti^5 + m6*ti^6;
Y(i) = g0 + g1*ti + g2*ti^2 + g3*ti^3 + g4*ti^4 + g5*ti^5;
Z(i) = X(i)/Y(i);
if Z(i) > 0
wc(i) = sqrt(R22/Z(i));
td(i) = (2/wc(i))*atan(wc(i)*ti);
% return halts all execution -- everything inside and outside the loop
% if you want to break out of the loop conditionally, use break
% if you want to skip to the next loop cycle, use continue
end
end
tdmin = min(td(~isnan(td))) % this doesn't make any sense to be inside the loop
tdmin = -0.1900
As an aside,
% by default, both i and j are sqrt(-1).
% that's part of why using them as indices can lead to problems
td = min(td(j)) % j was not user-defined as an index
Array indices must be positive integers or logical values.

댓글 수: 5

Suud Hasen
Suud Hasen 2021년 6월 3일
편집: Suud Hasen 2021년 6월 3일
The intention in the if statement is to yield angular frequency for positive Z(i) only. Z(i) has possible 5 values of 42.8435, -933.58, -1.7141, 7.8419 and -23.5903. Only for the positive values, computes wc(i) and td(i), then, take td the minimum.
DGM
DGM 2021년 6월 3일
편집: DGM 2021년 6월 3일
I didn't assume that the input values were the only possible ones. In this case, min(td)<0, but if min(td)>0, then there would be a problem. As the comment mentions, using zeros() to create the empty array means the default zeros in the positions associated with Z>0 would interfere with correctly calculating min(td). If instead you used NaN(), the default values would be NaN and could unambiguously be distinguished from valid td values and excluded from the minimization.
EDIT:
I went ahead and edited the code to use NaN() instead of zeros() and excluded invalid entries from the minimization. You can do something similar with wc if you want.
Suud Hasen
Suud Hasen 2021년 6월 3일
편집: Suud Hasen 2021년 6월 3일
Yes, you are right. Value must be td > 0. Delay margin value in Matlab/Simulink shows td_min supposed to be 8.7695. Which is one of the 2 possible results.
Weeell for simplification, I just rewrote this without the loop. It's easier to see what's going on.
t = [-0.095; -3.953; -7.52; 7.59; 37.66]
t = 5×1
-0.0950 -3.9530 -7.5200 7.5900 37.6600
m6 = 1.4544e+06;
m5 = -5.7624e+05;
m4 = -1.8298e+08;
m3 = -6.3483e+08;
m2 = -1.0784e+08;
m1 = -8.3759e+07;
m0 = -7.4807e+06;
g5 = -1.8497e+06;
g4 = -7.9658e+06;
g3 = -1.5743e+07;
g2 = -3.0234e+06;
g1 = -2.0215e+06;
g0 = -1.7686e+05;
R22 = 0.4752;
X = m0 + m1*t + m2*t.^2 + m3*t.^3 + m4*t.^4 + m5*t.^5 + m6*t.^6;
Y = g0 + g1*t + g2*t.^2 + g3*t.^3 + g4*t.^4 + g5*t.^5;
Z = X./Y
Z = 5×1
42.8435 -0.9336 -1.7141 7.8419 -23.5903
wc = sqrt(R22./Z);
wc(Z<0) = NaN; % mark out invalid results
td = (2./wc).*atan(wc.*t)
td = 5×1
-0.1900 NaN NaN 8.7695 NaN
tdmin = min(td(td>0)) % this would be the minimum positive value
tdmin = 8.7695
After modified my actual model as per your earlier suggestions, it worked well. Much appreciated. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2021년 6월 3일

댓글:

2021년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by