Jeremy Bos
MA4635
Final
5.3)7)b)

Use the collocation method to find the approximate solution for Ex 15 
u(x)=x + (∫_(-1)^1) xtu(
              t) dt in terms of 2 sets of independent functions.

Remove["Global`*"]

n = 8;
ul = 1;
ll = -1;
Array[ph[##][x] &, {1, n}];
cvar = Table[(Subscript[c, i]), {i, n}];
et = Table[0, {n}] ;
as = Table[0, {n}] ;

Create the ϕ's

Clear[k];  k = 0;
ph[1, x_] := 1;
For[k = 3, k ≤ n - 1, 
      k += 2, 
ph[k, x] = 
        Cos[(N[(((k - 1))/2)])*x]
]

Clear[k]; k = 0;
For[k = 2, k ≤ n, 
      k += 2, 
ph[k, x] = 
        Sin[(N[(k/2)])*x]
]

Create S3

sn[x_] = (∑_(i = 1)^8)((Subscript[c, i])*ph[i, x])

Find the error

eu[x_] = 
      sn[x] - (x + (∫_(-1)^1) x*t*
                sn[t] \[DifferentialD]t)

Create a 'n' of points where the error is zero

delta = ((ul - ll)/(n - 1));

Clear[k]
For[k = 1, 
      k ≤ n, k++, 
et[[k]] = 
        eu[ll + ((k - 1)*delta)]
]

et

Solve for the constants c

soln = Solve[et == 0, cvar]

Clear[i, k]
For[k = 1, 
      k ≤ n, k++, 
as[[k]] = 
        (Subscript[c, k]) /. soln; Print[as[[k]]];
]

Approximate the solution

fas[x_] = (∑_(i = 1)^n) as[[i]]*ph[i, x]

Plot[{fas[x], 3*x}, {x, -3, 3}, 
      PlotStyle -> {{Hue[1]}, {Hue[0.55]}}]

Clear[k]
For[k = 1, 
      k ≤ n, k++, 
Print["Actual  ", 
        N[3*(ll + ((k - 1)*
                    delta))], " ", "     Approximate ", 
        N[fas[ll + ((k - 1)*delta)]]]
]

Find the Least Square Error:
  The LSE can be found by squaring the difference between the exact and approximated solutions

LSE = (∫_ll^ul)(((3  x - 
                  fas[x]))^2) \[DifferentialD]x

Abs[LSE]