if (jjz == jji) v = vt(jjz); Iap = apor(jjz); Ilamd = lamdpor(jjz); elseif (jjz == 1 && jji == 2) S1 = Sz * (de - H_layer(1)) / dee; S2 = Sz * H_layer(1) / dee; S3 = Sz - S2 - S1; s3 = 1 / (S1 + S2 + S3); v = vt(jjz); Ilamd = exp((d1*S1 + d0 * S2 + d0 * S3)*s3); Ip = (S1 * p(2) + S2 * p(1) + S3 * p(1))*s3; Ic = (S1 * c(2) + S2 * c(1) + S3 * c(1))*s3; Iap = Ilamd / Ip / Ic; elseif (jjz == 1 && jji == 3) S1 = Sz * (de - H_layer(2)) / dee; S2 = Sz * (H_layer(2) - H_layer(1)) / dee; S3 = Sz * H_layer(1) / dee; S4 = Sz - S1 - S2 - S3; s4 = 1 / (S1 + S2 + S3 + S4); v = vt(jjz); Ilamd = exp((d2*S1 + d1 * S2 + d0 * S3 + d0 * S4)*s4); Ip = (S1 * p(3) + S2 * p(2) + p(1) * S3 + p(1) * S4)*s4; Ic = (S1 * c(3) + S2 * c(2) + c(1) * S3 + c(1) * S4)*s4; Iap = Ilamd / Ip / Ic; end 优化
时间: 2023-12-18 14:09:00 浏览: 140
js优化1
同样可以使用 `switch` 语句来代替多个 `elseif` 语句,从而使代码更加简洁易懂。具体实现如下:
```
switch true
case (jjz == jji)
v = vt(jjz);
Iap = apor(jjz);
Ilamd = lamdpor(jjz);
case (jjz == 1 && jji == 2)
S1 = Sz * (de - H_layer(1)) / dee;
S2 = Sz * H_layer(1) / dee;
S3 = Sz - S2 - S1;
s3 = 1 / (S1 + S2 + S3);
v = vt(jjz);
Ilamd = exp((d1*S1 + d0 * S2 + d0 * S3)*s3);
Ip = (S1 * p(2) + S2 * p(1) + S3 * p(1))*s3;
Ic = (S1 * c(2) + S2 * c(1) + S3 * c(1))*s3;
Iap = Ilamd / Ip / Ic;
case (jjz == 1 && jji == 3)
S1 = Sz * (de - H_layer(2)) / dee;
S2 = Sz * (H_layer(2) - H_layer(1)) / dee;
S3 = Sz * H_layer(1) / dee;
S4 = Sz - S1 - S2 - S3;
s4 = 1 / (S1 + S2 + S3 + S4);
v = vt(jjz);
Ilamd = exp((d2*S1 + d1 * S2 + d0 * S3 + d0 * S4)*s4);
Ip = (S1 * p(3) + S2 * p(2) + p(1) * S3 + p(1) * S4)*s4;
Ic = (S1 * c(3) + S2 * c(2) + c(1) * S3 + c(1) * S4)*s4;
Iap = Ilamd / Ip / Ic;
end
```
这样代码就更加清晰易懂,并且可以方便地扩展到其他情况。
阅读全文