if condition problem and positive condition

2 views (last 30 days)
Dear Matlab experts,
I added "if condition" in the Matlab coding.
I defined m1 = n1-ee1, and I used "if condition" because I would like to get only positive value for m1.
However, I could not get any result.
if n1 <= ee1
m1 =0;
else if n1 > ee1
m1 = n1 - ee1;
My full coding is below. Please let me know how to fix it.
Thank you very much in advance.
Sincerely yours,
J1
t = 0:.0001: 5;
z = 1819;
e1 = -42;
e2 = -7;
e3= -20;
e4= -29;
e5 = -32;
e6 = -112;
e7 = -70;
e8 = -9;
e9 = -48;
e10 = -14;
e11 = -2;
e12 = -22;
e13= -42;
e14= -15;
e15 = -26;
e16 = -58;
e17 = -24;
e18 = -73;
e19 = -102;
e20 = -78;
e21 = -600;
e22 = -11;
e23= -65;
e24= -285;
e25 = -32;
p = exp(-t.*e1) + exp(-t.*e2) + exp(-t.*e3) + exp(-t.*e4) + exp(-t.*e5) + exp(-t.*e6) + exp(-t.*e7) + exp(-t.*e8) + exp(-t.*e9) + exp(-t.*e10) + exp(-t.*e11) + exp(-t.*e12) + exp(-t.*e13) + exp(-t.*e14) + exp(-t.*e15) + exp(-t.*e16) + exp(-t.*e17) + exp(-t.*e18) + exp(-t.*e19) + exp(-t.*e20) + exp(-t.*e21) + exp(-t.*e22) + exp(-t.*e23) + exp(-t.*e24) + exp(-t.*e25) ;
b1 = (exp(-t.*e1))./p;
b2 = (exp(-t.*e2))./p;
b3 = (exp(-t.*e3))./p;
b4 = (exp(-t.*e4))./p;
b5 = (exp(-t.*e5))./p;
b6 = (exp(-t.*e6))./p;
b7 = (exp(-t.*e7))./p;
b8 = (exp(-t.*e8))./p;
b9 = (exp(-t.*e9))./p;
b10 = (exp(-t.*e10))./p;
b11 = (exp(-t.*e11))./p;
b12 = (exp(-t.*e12))./p;
b13 = (exp(-t.*e13))./p;
b14 = (exp(-t.*e14))./p;
b15 = (exp(-t.*e15))./p;
b16 = (exp(-t.*e16))./p;
b17 = (exp(-t.*e17))./p;
b18 = (exp(-t.*e18))./p;
b19 = (exp(-t.*e19))./p;
b20 = (exp(-t.*e20))./p;
b21 = (exp(-t.*e21))./p;
b22 = (exp(-t.*e22))./p;
b23 = (exp(-t.*e23))./p;
b24 = (exp(-t.*e24))./p;
b25 = (exp(-t.*e25))./p;
n1 = z*b1;
n2 = z*b2;
n3 = z*b3;
n4 = z*b4;
n5 = z*b5;
n6 = z*b6;
n7 = z*b7;
n8 = z*b8;
n9 = z*b9;
n10 = z*b10;
n11 = z*b11;
n12 = z*b12;
n13 = z*b13;
n14 = z*b14;
n15 = z*b15;
n16 = z*b16;
n17 = z*b17;
n18 = z*b18;
n19 = z*b19;
n20 = z*b20;
n21 = z*b21;
n22 = z*b22;
n23 = z*b23;
n24 = z*b24;
n25 = z*b25;
ee1 = 42;
ee2 = 7;
ee3 = 20;
ee4 = 29;
ee5 = 32;
ee6 = 112;
ee7 = 70;
ee8 = 9;
ee9 = 48;
ee10 = 14;
ee11 = 2;
ee12 = 22;
ee13 = 42;
ee14 = 15;
ee15 = 26;
ee16 = 58;
ee17 = 24;
ee18 = 73;
ee19 = 102;
ee20 = 78;
ee21 = 600;
ee22 = 11;
ee23 = 65;
ee24 = 285;
ee25 = 32;
m1 = n1 - ee1;
m2 = n2 - ee2;
m3 = n3 - ee3;
m4 = n4 - ee4;
m5 = n5 - ee5;
m6 = n6 - ee6;
m7 = n7 - ee7;
m8 = n8 - ee8;
m9 = n9 - ee9;
m10 = n10 - ee10;
m11 = n11 - ee11;
m12 = n12 - ee12;
m13 = n13 - ee13;
m14 = n14 - ee14;
m15 = n15 - ee15;
m16 = n16 - ee16;
m17 = n17 - ee17;
m18 = n18 - ee18;
m19 = n19 - ee19;
m20 = n20 - ee20;
m21 = n21 - ee21;
m22 = n22 - ee22;
m23 = n23 - ee23;
m24 = n24 - ee24;
m25 = n25 - ee25;
if n1 <= ee1
m1 =0;
else if n1 > ee1
m1 = n1 - ee1;
y = m1.*m1 + m2.*m2 + m3.*m3 + m4.*m4 + m5.*m5 + m6.*m6 + m7.*m7 + m8.*m8+ m9.*m9 + m10.*m10 +m11.*m11 + m12.*m12 + m13.*m13 + m14.*m14 + m15.*m15 + m16.*m16 + m17.*m17 + m18.*m18+ m19.*m19 + m20.*m20+ m21.*m21 + m22.*m22 + m23.*m23 + m24.*m24 + m25.*m25;
plot(t, sqrt(y))
[ymin, yi] = min(y);
tmin = t(yi)
ymin =y(yi)

Accepted Answer

Image Analyst
Image Analyst on 19 Oct 2019
You separated the else and the if. It should be elseif (one word) but actually you don't even need it. Try this:
if n1 <= ee1
m1 = 0;
else
m1 = n1 - ee1;
end

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!