(MMA v. 14.0, and 13.0)
This is a strange behaviour I encountered when trying to determine Fourier coefficients in the most naive way:
n = 17;
eqv = Table[c[0] + Sum[ccos[k] Cos[Pi t k] + csin[k] Sin[Pi t k], {k, 1, n}] +
E^t == 0, {t, Range[0, 1, 1/(2 n)]}];
coeffv = NSolve[eqv, Join[Flatten@Table[{ccos[k], csin[k]}, {k, 1, n}], {c[0]}]][[1]]
Length@coeffv
Length@Join[Flatten@Table[{ccos[k], csin[k]}, {k, 1, n}], {c[0]}]
Length@eqv
(* Output:
{ccos[1] -> 1.07451 + 0.00353982 c[0], ...}
34
35
35
*)
Every solution in coeffv has a linear dependence on c[0], and no solution is given for c[0]. Clearly, this can't be right... somehow it doesn't solve the linear system of equations, even though there are as many variables as equations.
However, for $n=16$
(* Output:
{ccos[1] -> 1.0643, csin[1] -> 0.331322, ...}
33
33
33
*)
everything is ok. This holds for any number smaller than 16, and any number larger than 17 doesn't work either.
Solve alone does not give a result for $n\geq 4$ in reasonable time. (Because it refuses to make the coefficients numerical, which is fine by me. Doing a N around everything invalidates my variable names, but I can fix that...)
lfor variable name, it looks like1on the screen and hard to read your code. you can choose any other letter.Lis not used by Mathematica so you can use that. Or usen– Nasser Mar 25 '24 at 11:47WorkingPrecision(for exampleNSolve[..., WorkingPrecision -> 30]) seems to solve the issue :) – Domen Mar 25 '24 at 12:27LinearSolve, for example:LinearSolve[ Table[{1}~Join~Flatten@Table[{Cos[Pi t k], Sin[Pi t k]}, {k, 1, n}], {t, Range[0, 1, 1/(2 n)]}], Table[-E^t, {t, Range[0, 1, 1/(2 n)]}]](wrap withNfor higher $n$ when badly conditioned matrices start to appear). If you don't have a linear system, then it will be best to ask a question with your actual problem, not some other, made up one :) – Domen Mar 25 '24 at 14:17LinearSolve: Don't use exact numbers. It takes only 0,3 s (or 1,4 s with higher precision) for $n=100$. – Domen Mar 25 '24 at 15:12n=17is the start of trouble due to matrix conditioning and machine arithmetic. At that value the smallest singular value in the coefficient matrix is around10^(-15)(so within an order of magnitude of the machine precision epsilon), whereas atn=16it is around another order of magnitude larger (near10^(-14)). – Daniel Lichtblau Mar 25 '24 at 16:59Out[200]= {0.012747, Null}`
– Daniel Lichtblau Mar 25 '24 at 17:35FourierSeries, but it is good to know that I'm not the only one weirded out. – Confuse-ray30 Mar 25 '24 at 18:01