MATLAB: A single solution in a loop is displaying incorrectly

print numerical

When I run this code, Nk displays simple numerical values for n=1,2,4 and 5, but displays a value for n=3 that is instead an equation (consisting of only numbers, no variables). The solution to the equation is of the same order as the other numerical values, so the issues doesn't seem to be catastrophic cancellation or something of that ilk: Wolfram
Code:
for n = 1:5
k = 50.37; % Constant at start of N
T = 1e16; % Characteristic energy scale
B = 3.495e67; % constant for potential
L1 = (2*n*(2*n-1))^(n/4); %top of log for 1st half of N
L2 = (2*N+2*n-1)/(2*n-1); %second log
VA = (2*n*(2*N+2*n-1))^(n+1); %algebra for V
V = (B*n^2)./VA;
V2 = ((T^2*exp(2*(N-k)))./(L1^2*L2^2))^2;
Nk=solve(V-V2 == 0) %assings solution to N*
VA2 = (2*n*(2*Nk+2*n-1))^(n+1); %redefines VA in terms of Nk
V0 = ((B*Nk^2)./VA2)^(1/4) %solution to V0
end
T=table(Nk,V0)
Output:
1
Nk =
54.587520560507798727717301906819
V0 =
38269419168929696.686353001207992
2
Nk =
53.033456412586034572167306062259
V0 =
5865729109906268.5364512437212995
3
Nk =
log((568392988925525712716364010727016105677005258752*exp(5037/25))/542101086242752217003726400434970855712890625)/4
V0 =
((2184375000000000050682263628770112625380923754565506923392495779840*log((568392988925525712716364010727016105677005258752*exp(5037/25))/542101086242752217003726400434970855712890625)^2)/(3*log((568392988925525712716364010727016105677005258752*exp(5037/25))/542101086242752217003726400434970855712890625) + 30)^4)^(1/4)
4
Nk =
51.408355779435985633476452024765
V0 =
115260533332952.51336375186114595
5
Nk =
50.834875713866133896178560340504
V0 =
14890156979567.434795879619732417
T =
Nk V0
_________ _________
[1x1 sym] [1x1 sym]
I am using R2014a.
Any help would be appreciated,
Thanks

Best Answer

There is no incorrect display. solve() is a symbolic solver and for n=3 there happens to be a closed form solution instead requiring a numeric approximation. If you have require a numeric solution then you should be considering fzero or fsolve, or you should use vpasolve. You should only use solve() when you want the closed form solution if it exists.
Related Question