# Absurd solution by LinearSolve

I am trying to find curve that passes through all the data points.To do this i am doing interpolation using LinearSolve. No. of equations vary from 1000 to 3000. LinearSolve gives me values of coefficient which I assume should be correct but when I plot the curve it does pass through those data points. So I am not sure what Linear Solve is doing.Can anyone help me in this? The red dots are the data points.The black curve is the curve that i got by using LinearSolve

Here's the code

ClearSystemCache[]
ClearAll["Global*"]

SetDirectory[NotebookDirectory[]]
w1 = 13;
w2 = Pi;
nP = 1.0;

(*Value of 'g' varies from 1 to 28*)

g = 12;
p2 = g;
p1 = -p2;
(*freq=frequencies present in the signal*)
freq = Sort[
Drop[Flatten[
N[Table[m*w1 + n*w2, {m, 0, p2}, {n, p1, p2}]]], {Abs[p2] + 1,
Abs[p2] + 1}]];

(*loop deletes the duplicate frequecny*)
x = Length[freq];
For[i = 1, i <= x, i++,
For[k = 1, k <= x, k++,
If[Abs[freq[[i]]] == freq[[k]] && i != k,
freq = Delete[freq, k];
x = Length[freq];
(*i=i-1*)
]
]
]

(*assumed function for Zt*)
zt11 = a11[0] +
Sum[v11[i]*Cos[freq[[i]] t] + p11[i]*Sin[freq[[i]] t], {i, 1,
Length[freq]}];
zt12 = a12[0] +
Sum[v12[i]*Cos[freq[[i]] t] + p12[i]*Sin[freq[[i]] t], {i, 1,
Length[freq]}];
zt21 = a21[0] +
Sum[v21[i]*Cos[freq[[i]] t] + p21[i]*Sin[freq[[i]] t], {i, 1,
Length[freq]}];
zt22 = a22[0] +
Sum[v22[i]*Cos[freq[[i]] t] + p22[i]*Sin[freq[[i]] t], {i, 1,
Length[freq]}];

int = Length[freq]

data11 = Join[Table[Zt[[1, 1]], {y, 0, nP, nP/int}],
Drop[Table[Z2t[[1, 1]], {y, 0, nP, nP/int}], 1]];
data12 = Join[Table[Zt[[1, 2]], {y, 0, nP, nP/int}],
Drop[Table[Z2t[[1, 2]], {y, 0, nP, nP/int}], 1]];
data21 = Join[Table[Zt[[2, 1]], {y, 0, nP, nP/int}],
Drop[Table[Z2t[[2, 1]], {y, 0, nP, nP/int}], 1]];
data22 = Join[Table[Zt[[2, 2]], {y, 0, nP, nP/int}],
Drop[Table[Z2t[[2, 2]], {y, 0, nP, nP/int}], 1]];

datazt11 = Table[zt11, {t, 0, 2 nP, nP/int}];
datazt12 = Table[zt12, {t, 0, 2 nP, nP/int}];
datazt21 = Table[zt21, {t, 0, 2 nP, nP/int}];
datazt22 = Table[zt22, {t, 0, 2 nP, nP/int}];

eq11 = datazt11 - data11;
eq12 = datazt12 - data12;
eq21 = datazt21 - data21;
eq22 = datazt22 - data22;

{b11, m11} = CoefficientArrays[eq11 == 0, Variables[eq11]];
{b12, m12} = CoefficientArrays[eq12 == 0, Variables[eq12]];
{b21, m21} = CoefficientArrays[eq21 == 0, Variables[eq21]];
{b22, m22} = CoefficientArrays[eq22 == 0, Variables[eq22]];

coeff11 = LinearSolve[m11, -b11];
coeff12 = LinearSolve[m12, -b12];
coeff21 = LinearSolve[m21, -b21];
coeff22 = LinearSolve[m22, -b22];

coefv11 = Solve[Variables[eq11] - coeff11 == 0, Variables[eq11]];
coefv12 = Solve[Variables[eq12] - coeff12 == 0, Variables[eq12]];
coefv21 = Solve[Variables[eq21] - coeff21 == 0, Variables[eq21]];
coefv22 = Solve[Variables[eq22] - coeff22 == 0, Variables[eq22]];

zz = {Flatten[{zt11 /. coefv11, zt12 /. coefv12}],
Flatten[{zt21 /. coefv21, zt22 /. coefv22}]};

p1 = Plot[zz[[1, 1]], {t, 0, 2 nP}, PlotRange -> All,
PlotStyle -> {Black, Thick}]
p2 = Plot[Zt[[1, 1]], {y, 0, 1}];
p3 = Plot[q2[[1, 1]], {y, 1, 2}];
Show[p1, p2, p3,
DiscretePlot[Zt[[1, 1]], {y, 0, nP, nP/int},
PlotStyle -> {Red, Dashed}, Joined -> False, Filling -> None,
PlotMarkers -> {Automatic, 5}],
DiscretePlot[q2[[1, 1]], {y, 1, 2 nP, nP/int},
PlotStyle -> {Red, Dashed}, Joined -> False, Filling -> None,
PlotMarkers -> {Automatic, 5}]]

• What would be wrong with Interpolation? – Verbeia Oct 13 '15 at 0:51
• It's not what you're thinking; otherwise you'd know that Interpolation[]` is a built-in function. – J. M. will be back soon Oct 13 '15 at 1:31