PGhosh

My Bangla Name Equation Interpolation

Jun 20th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. (* Data Input *)
  2. PointListx = {};
  3. PointListy = {};
  4. LenPointList = {};
  5. (* Seg 1 *)
  6. AppendTo[PointListx, {{0, 6, 0}, {0, 8, 18}, {18, 20}, {0, 18}, {20,
  7. 20}, {20, 17, 13}, {11, 6, 0}}];
  8. AppendTo[PointListy, {{20, 15, 10}, {20, 23, 17}, {17, 15}, {10,
  9. 17}, {0, 25}, {0, 3, 4}, {3, 0, 4}}];
  10. AppendTo[LenPointList, Length[PointListx[[1]]]];
  11. (* Seg 2 *)
  12. AppendTo[PointListx, {{38, 40.5, 38}, {38, 33, 28}, {38, 35, 32}, {38,
  13. 35, 32}}];
  14. AppendTo[PointListy, {{22, 14, 5}, {5, 0, 5}, {22, 19, 22}, {22, 25,
  15. 22}}];
  16. AppendTo[LenPointList, Length[PointListx[[2]]]];
  17. (* Seg 3 *)
  18. AppendTo[PointListx, {{45, 42, 45}, {45, 47, 45}}];
  19. AppendTo[PointListy, {{25, 19, 10}, {10, 5, 2}}];
  20. AppendTo[LenPointList, Length[PointListx[[3]]]];
  21. (* Seg 4 *)
  22. AppendTo[PointListx, {{50, 58, 50}, {50, 61, 67}, {67, 67}}];
  23. AppendTo[PointListy, {{25, 16.5, 7}, {7, 7, 0}, {0, 25}}];
  24. AppendTo[LenPointList, Length[PointListx[[4]]]];
  25. (* Seg 5 *)
  26. AppendTo[PointListx, {{20, 70}}];
  27. AppendTo[PointListy, {{25, 25}}];
  28. AppendTo[LenPointList, Length[PointListx[[5]]]];
  29. (* End Seg *)
  30. NoSubSegment = Length[LenPointList];
  31. (* Working Section *)
  32. (* Most Outer Loop, Works for the segments *)
  33. For[Loop1 = 1, Loop1 <= NoSubSegment, Loop1++,
  34. List1x = PointListx[[Loop1]];
  35. List1y = PointListy[[Loop1]];
  36. Len1 = Length[List1x];
  37. ipol = 0;
  38. (* Second Outer Loop. Works for the points inside a segment *)
  39. For[Loop2 = 1, Loop2 <= Len1, Loop2++,
  40. List2x = List1x[[Loop2]];
  41. List2y = List1y[[Loop2]];
  42. Len2 = Length[List2x];
  43. (* Checking vertical or horizontal line *)
  44. If[Len2 == 2,
  45. If[List2x[[2]] - List2x[[1]] == 0,
  46. Print["x = ", List2x[[1]], " {", Sort[List2y][[1]] ,
  47. "\[LessEqual]y\[LessEqual]", Sort[List2y][[2]], "}"]; ipol = 2,
  48. If[List2y[[2]] - List2y[[1]] == 0,
  49. Print["y = ", List2y[[1]], " {", Sort[List2x][[1]] ,
  50. "\[LessEqual]x\[LessEqual]", Sort[List2x][[2]], "}"]; ipol = 2]
  51. ]
  52. ];
  53. (* Checking orientation of Parabola *)
  54. If[Len2 == 3,
  55. If[(List2x[[2]] - List2x[[1]])*(List2x[[2]] - List2x[[3]]) >= 0,
  56. ipol = 1]
  57. ];
  58. (* Main Interpolation Code Begins *)
  59. If[ipol != 2,
  60. PolynomialMatrix =
  61. Join[ConstantArray[0, {Len2 - 1, Len2}],
  62. ConstantArray[1, {1, Len2}]];
  63. (* y = ax^2 + bx + c type parabola *)
  64. If[ipol == 0,
  65. For[i = 1, i <= (Len2 - 1), i++,
  66. For[j = 1, j <= Len2 , j++,
  67. PolynomialMatrix[[i, j]] = (List2x[[j]] ^ (Len2 - i))
  68. ]
  69. ];
  70. Result = LinearSolve[Transpose[PolynomialMatrix], List2y ],
  71. (* x = ay^2 + by + c type parabola *)
  72. For[i = 1, i <= (Len2 - 1), i++,
  73. For[j = 1, j <= Len2 , j++,
  74. PolynomialMatrix[[i, j]] = (List2y[[j]] ^ (Len2 - i))
  75. ]
  76. ];
  77. Result = LinearSolve[Transpose[PolynomialMatrix], List2x ]
  78. ];
  79. TrResult = Transpose[{Result}];
  80. (* Equation Making *)
  81. sump = 0;
  82. prodp = 1;
  83. If[ipol == 0,
  84. For[i = 1, i <= Len2, i++,
  85. prodp = prodp * (x^(i - 1)) * TrResult[[Len2 - i + 1, 1]];
  86. sump = sump + prodp;
  87. prodp = 1
  88. ],
  89. For[i = 1, i <= Len2, i++,
  90. prodp = prodp * (y^(i - 1)) * TrResult[[Len2 - i + 1, 1]];
  91. sump = sump + prodp;
  92. prodp = 1
  93. ]
  94. ];
  95. F = Simplify[sump];
  96. (* Output Suffix and Prefix finding *)
  97. If[ipol == 0,
  98. pref = "y = ";
  99. lim = {Sort[List2x][[1]], "\[LessEqual]x\[LessEqual]",
  100. Sort[List2x][[Len2]]},
  101. pref = "x =";
  102. lim = {Sort[List2y][[1]], "\[LessEqual]y\[LessEqual]",
  103. Sort[List2y][[Len2]]}
  104. ];
  105. (* Output *)
  106. Print[pref, F , " {", lim[[1]], lim[[2]], lim[[3]], "}"]
  107. ];
  108. ipol = 0;
  109. ]
  110. (* Output to indicate end of a segment *)
  111. Print["End of Segment ", Loop1]
  112. ]
Add Comment
Please, Sign In to add comment