Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ClearAll[DirectSmoothCubicFit];
  2.  
  3. DirectSmoothCubicFit[y_, m_] :=
  4. Module[{n, F1, F2, F, a, b, k, , exp1, exp2, exp3, exp4, exp5, exp6,
  5. eqns, c, coffarray, coff, finalvals = {}},
  6. n = Length[y];
  7. F1 = Sum[(y[[j]] - a[0] - a[1]*(j - m) -
  8. Sum[a[k]*(j - m)^k, {k, 2, 3}])^2, {j, 1, m}]/2; (*
  9. Definition of the Error Function *)
  10. F2 = Sum[(y[[j]] - a[0] - a[1]*(j - m) -
  11. Sum[b[k]*(j - m)^k, {k, 2, 3}])^2, {j, m, n}]/2;
  12. F = F1 + F2;
  13.  
  14. exp1 = D[F, a[0]]; (* [PartialD]F/[PartialD]a[0] *)
  15. exp2 = D[F, a[1]]; (* [PartialD]F/[PartialD]a[1] *)
  16. exp3 = D[F, a[2]]; (* [PartialD]F/[PartialD]a[2] *)
  17. exp4 = D[F, a[3]]; (* [PartialD]F/[PartialD]a[3] *)
  18. exp5 = D[F, b[2]]; (* [PartialD]F/[PartialD]b[2] *)
  19. exp6 = D[F, b[3]]; (* [PartialD]F/[PartialD]b[3] *)
  20.  
  21. eqns = {exp1 == 0, exp2 == 0, exp3 == 0, exp4 == 0, exp5 == 0,
  22. exp6 == 0};
  23. {c, coffarray} =
  24. Normal[
  25. CoefficientArrays[
  26. eqns, {a[0], a[1], a[2], a[3], b[2], b[3]}]]; (*
  27. Calculation of the Coefficient Matrix for the six equations *)
  28.  
  29. coff = LinearSolve[coffarray, -c];
  30. finalvals =
  31. Join[finalvals,
  32. Table[coff[[1]] + coff[[2]]*(j - m) + coff[[3]]*(j - m)^2 +
  33. coff[[4]]*(j - m)^3, {j, 1, m}]];
  34. finalvals =
  35. Join[finalvals,
  36. Table[coff[[1]] + coff[[2]]*(j - m) + coff[[5]]*(j - m)^2 +
  37. coff[[6]]*(j - m)^3, {j, m + 1, n}]];
  38.  
  39. finalvals
  40. ]
  41.  
  42. then the loop where the problem is shown
  43.  
  44. y = RandomVariate[NormalDistribution[0, 1]]*
  45. Table[i^3, {i, 1, 20}] + RandomVariate[NormalDistribution[0, 1]];
  46. fact = 1./0.000001^2;
  47. For[o = 5, o <= 16, o++, fit1 = DirectSmoothCubicFit[y, o];
  48. residuals1 = Abs[(fit1)] // N ;
  49. res1 = Total [residuals1^2]*fact // N;
  50. r1 = Join[r1, {res1}]];
  51. r1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement