Why am I getting an imaginary number in my loop?
Mostra commenti meno recenti
Hi everyone,
For my loop I keep getting imaginary numbers. This is not supposed to happen and was wondering if anyone had any tips. In the code, theta_o is supposed to converge when it equals 0.1696, but when it goes negative that makes the convergence not happen
%% variables
Ctreq = 0.008;
Ddo = 0.01;
Clalpha = 5.76;
sigma = 0.1;
Nb = 4;
dr = 0.025;
%% initalize
Ct = 0;
Cpi = 0;
theta_tw = 0;
theta_j = 0.1;
theta_o = 6.*Ctreq./sigma./Clalpha - 3./4.*theta_tw + 1.0607.*sqrt(Ctreq);
theta_o = [theta_o];
for r = 0:0.025:1
theta_o = theta_o(end);
lambda = (sigma.*Clalpha./16).*(sqrt(1+32.*theta_o.*r./sigma./Clalpha)-1);
dCt = (sigma.*Clalpha./2).*(theta_o.*r.^2 - lambda.*r).*dr;
Ct = Ct + dCt;
Cpi = Cpi + lambda.*dCt;
theta_j = theta_o + (6.*(Ctreq-Ct)./sigma./Clalpha + 1.0607 .* (sqrt(Ctreq)-sqrt(Ct)));
theta_o = [theta_o theta_j];
if abs((theta_j-theta_o(1))/theta_j) > 0.001
break
end
end
Risposte (1)
When your dCt is positive, your Ct increases. If it does that often enough, it can end up being larger than Ctreq, and then (Ctreq-Ct) would be negatie and sqrt(Ctreq)-sqrt(Ct) would be negative, so theta_j would start decreasing. If your dCt does not start going negative then theta_j will get smaller and smaller until it becomes negative, and then the part inside your sqrt() for lambda is at risk of going complex.
%% variables
Ctreq = 0.008;
Ddo = 0.01;
Clalpha = 5.76;
sigma = 0.1;
Nb = 4;
dr = 0.025;
%% initalize
Ct = 0;
Cpi = 0;
theta_tw = 0;
theta_j = 0.1;
theta_o = 6.*Ctreq./sigma./Clalpha - 3./4.*theta_tw + 1.0607.*sqrt(Ctreq);
theta_o = [theta_o];
for r = 0:0.025:1
theta_o = theta_o(end);
part1 = 1+32.*theta_o.*r./sigma./Clalpha
lambda = (sigma.*Clalpha./16).*(sqrt(part1)-1);
dCt = (sigma.*Clalpha./2).*(theta_o.*r.^2 - lambda.*r).*dr;
Ct = Ct + dCt;
Cpi = Cpi + lambda.*dCt;
if Ctreq < Ct
fprintf('Caution, watch out for negative, Ctreq = %g, Ct = %g\n', Ctreq, Ct);
end
theta_j = theta_o + (6.*(Ctreq-Ct)./sigma./Clalpha + 1.0607 .* (sqrt(Ctreq)-sqrt(Ct)))
theta_o = [theta_o theta_j];
if abs((theta_j-theta_o(1))/theta_j) < 0.001
break
end
end
Categorie
Scopri di più su Vehicle Dynamics Blockset in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!