Jeremy Bos
MA4635
Final
5.3)7)a)

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.

Clear[n, x, t, ϕ, i, j, k, u, zu, s3, eu]

n = 3;
ul = 1;
ll = -1;
Array[ph, n];
cvar = Table[(Subscript[c, i]), {i, n}];

ph[1, x_] := 1; ph[2, x_] := Sin[x]; ph[3, x_] := Cos[x];

s3[x_] := 0;

Create (Subscript[ϕ, i]) s

For[i = 1, 
      i <= n, i++, 
s3[x_] = 
          s3[x] + (Subscript[c, i])*ph[i, x];
]

s3[t]

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

eu[1]

Solve[eu[1] == 0, cvar]

eu[0]

eu[-1]

Solve[{eu[0] == 0, eu[1] == 0, eu[-1] == 0}, 
      cvar]

Approximate solution is:

fas[x_] = N[-(1/(-2 Cos[1] + Sin[1]))*Sin[x]]

Compare to the exact solution 3x

Plot[{4.18* Sin[x], 3*x}, {x, -3, 3},  
      PlotStyle -> {{Hue[1]}, {Hue[0.7]}}]

Table of Results

Clear[k]
For[k = 1, 
      k ≤ n, k++, 
Print["Actual  ", 
        3*(-2 + k), " ", "     Approximate ", 
        4.18*Sin[(-2 + k)]]
]

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