Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. function [ output_args ] = lab3( input_args )
  2. clc, clf, clear
  3. format long g
  4.  
  5. hold on
  6. axis equal
  7.  
  8. plot([-100, 100], [0, 0])
  9. plot([0, 0], [-100, 100])
  10.  
  11. % ORIGINAL DOTS START
  12. figure(1) % figure showing the cross cut
  13. testpunkterx = [0 8 12 6.5 2];%[0 1 3 4 2]
  14. testpunktery = [0 -3 2 9.5 5.5];%[0 7 8 5 2]
  15. % plotting original dots
  16. plot(testpunkterx, testpunktery, 'x')
  17.  
  18. % guessing the b and c values
  19. b = [8 27.5]; c = [22.5 -14];
  20. % ORIGINAL DOTS END
  21.  
  22. % TEST SHIT
  23. % testpunkterx = [0 1 3 4 2]
  24. % testpunktery = [0 7 8 5 2]
  25. %
  26. % % b = [3 13]; c = [12 8];
  27. %
  28. % b = [13 7]; c = [12 -4];
  29. % tempx = cos(-pi/3) * testpunkterx - sin(-pi/3) * testpunktery;
  30. % tempy = sin(-pi/3) * testpunkterx + cos(-pi/3) * testpunktery;
  31. % testpunkterx = tempx;
  32. % testpunktery = tempy;
  33.  
  34. %
  35. % testpunkterx = [0 1 1 0 0];
  36. % testpunktery = [0 0 0 1 1];
  37. % b = [1 0]; c = [0 1];
  38. % tempx = cos(-pi/3) * testpunkterx - sin(-pi/3) * testpunktery;
  39. % tempy = sin(-pi/3) * testpunkterx + cos(-pi/3) * testpunktery;
  40. % testpunkterx = tempx;
  41. % testpunktery = tempy;
  42. % TEST SHIT
  43.  
  44.  
  45. % plotting original dots
  46. plot(testpunkterx, testpunktery, 'x')
  47. t = 0 : 0.01 : 1;
  48. p1 = [0 0]; p2 = [0 0];
  49. crosscut = cubicbezier(p1, p2, b, c, t');
  50. % plotting my best guess
  51. plot(crosscut(:, 1), crosscut(:, 2), 'red')
  52.  
  53. % guessing the t values
  54. t = [0.1 0.3 0.5 0.9];
  55. dxnorm = 1;
  56. iter = 0;
  57. x = [b c t]';
  58.  
  59. % symbolisk lösning ser ut såhär:
  60. % syms bx by cx cy t1 t2 t3 t4
  61. % Fsymb = [3*t1*bx*(1 - t1)^2 + 3*cx*(1 - t1)*t1^2 - testpunkterx(2)
  62. % 3*t1*by*(1 - t1)^2 + 3*cy*(1 - t1)*t1^2 - testpunktery(2)
  63. % 3*t2*bx*(1 - t2)^2 + 3*cx*(1 - t2)*t2^2 - testpunkterx(3)
  64. % 3*t2*by*(1 - t2)^2 + 3*cy*(1 - t2)*t2^2 - testpunktery(3)
  65. % 3*t3*bx*(1 - t3)^2 + 3*cx*(1 - t3)*t3^2 - testpunkterx(4)
  66. % 3*t3*by*(1 - t3)^2 + 3*cy*(1 - t3)*t3^2 - testpunktery(4)
  67. % 3*t4*bx*(1 - t4)^2 + 3*cx*(1 - t4)*t4^2 - testpunkterx(5)
  68. % 3*t4*by*(1 - t4)^2 + 3*cy*(1 - t4)*t4^2 - testpunktery(5)];
  69. % symbolicJ = jacobian(Fsymb, [bx by cx cy t1 t2 t3 t4])
  70. % och detta nedan ska göras i varje iteration för att uppdatera f och J
  71. % f = subs(Fsymb, [bx by cx cy t1 t2 t3 t4], x)
  72. % J = subs(symbolicJ, [bx by cx cy t1 t2 t3 t4], x)
  73.  
  74. while dxnorm > 1e-14 && iter < 15
  75. f = [ 3*x(5)*x(1)*(1 - x(5)).^2 + 3*x(3)*(1 - x(5))*x(5)^2 - testpunkterx(2)
  76. 3*x(5)*x(2)*(1 - x(5)).^2 + 3*x(4)*(1 - x(5))*x(5)^2 - testpunktery(2)
  77. 3*x(6)*x(1)*(1 - x(6)).^2 + 3*x(3)*(1 - x(6))*x(6)^2 - testpunkterx(3)
  78. 3*x(6)*x(2)*(1 - x(6)).^2 + 3*x(4)*(1 - x(6))*x(6)^2 - testpunktery(3)
  79. 3*x(7)*x(1)*(1 - x(7)).^2 + 3*x(3)*(1 - x(7))*x(7)^2 - testpunkterx(4)
  80. 3*x(7)*x(2)*(1 - x(7)).^2 + 3*x(4)*(1 - x(7))*x(7)^2 - testpunktery(4)
  81. 3*x(8)*x(1)*(1 - x(8)).^2 + 3*x(3)*(1 - x(8))*x(8)^2 - testpunkterx(5)
  82. 3*x(8)*x(2)*(1 - x(8)).^2 + 3*x(4)*(1 - x(8))*x(8)^2 - testpunktery(5) ];
  83.  
  84. J = [ 3*x(5)*(1 - x(5))^2, 0, 3*(1 - x(5))*x(5)^2, 0, 3*(x(1)*((1 - x(5))^2 - 2*x(5)*(1-x(5))) + x(3)*(2*x(5) - 3*x(5)^2)), 0, 0, 0
  85. 0, 3*x(5)*(1 - x(5))^2, 0, 3*(1 - x(5))*x(5)^2, 3*(x(2)*((1 - x(5))^2 - 2*x(5)*(1-x(5))) + x(4)*(2*x(5) - 3*x(5)^2)), 0, 0, 0
  86. 3*x(6)*(1 - x(6))^2, 0, 3*(1 - x(6))*x(6)^2, 0, 0, 3*(x(1)*((1 - x(6))^2 - 2*x(6)*(1-x(6))) + x(3)*(2*x(6) - 3*x(6)^2)), 0, 0
  87. 0, 3*x(6)*(1 - x(6))^2, 0, 3*(1 - x(6))*x(6)^2, 0, 3*(x(2)*((1 - x(6))^2 - 2*x(6)*(1-x(6))) + x(4)*(2*x(6) - 3*x(6)^2)), 0, 0
  88. 3*x(7)*(1 - x(7))^2, 0, 3*(1 - x(7))*x(7)^2, 0, 0, 0, 3*(x(1)*((1 - x(7))^2 - 2*x(7)*(1-x(7))) + x(3)*(2*x(7) - 3*x(7)^2)), 0
  89. 0, 3*x(7)*(1 - x(7))^2, 0, 3*(1 - x(7))*x(7)^2, 0, 0, 3*(x(2)*((1 - x(7))^2 - 2*x(7)*(1-x(7))) + x(4)*(2*x(7) - 3*x(7)^2)), 0
  90. 3*x(8)*(1 - x(8))^2, 0, 3*(1 - x(8))*x(8)^2, 0, 0, 0, 0, 3*(x(1)*((1 - x(8))^2 - 2*x(8)*(1-x(8))) + x(3)*(2*x(8) - 3*x(8)^2))
  91. 0, 3*x(8)*(1 - x(8))^2, 0, 3*(1 - x(8))*x(8)^2, 0, 0, 0, 3*(x(2)*((1 - x(8))^2 - 2*x(8)*(1-x(8))) + x(4)*(2*x(8) - 3*x(8)^2)) ];
  92.  
  93. dx = -J\f
  94.  
  95. plot(x(1), x(2), '*')
  96. plot(x(3), x(4), '.')
  97.  
  98. % uppdaterar x med nya fina värden
  99. x = x + dx;
  100.  
  101. % den här ska bli liten och fin
  102. dxnorm = norm(dx, inf)
  103. iter = iter + 1;
  104. end, x
  105.  
  106. % plotting the result of the maximization
  107. t = 0 : 0.01 : 1;
  108. crosscut = cubicbezier(p1, p2, [x(1) x(2)], [x(3) x(4)], t');
  109. plot(crosscut(:, 1)+12, crosscut(:, 2), 'green')
  110.  
  111. figure(2) % rotation of the bezier around z axis
  112.  
  113.  
  114. X = [], Y = [], Z = []
  115. x = crosscut(:, 1)+12;
  116. y = crosscut(:, 2);
  117. for angle = 0 : pi/64 : 2*pi;
  118. X = [X, x * cos(angle)];
  119. Y = [Y, x * sin(angle)];
  120. Z = [Z crosscut(:, 2)];
  121. end
  122.  
  123. colormap('copper')
  124. surf(X, Y, Z, 'EdgeColor', 'none')
  125. axis equal
  126.  
  127. hold on
  128.  
  129. plotGuideCircles()
  130. end
  131.  
  132. function out = cubicbezier(P1, P2, b, c, t)
  133. F3 = [(1-t).^3 3*t.*(1-t).^2 3*(1-t).*t.^2 t.^3];
  134.  
  135. out = F3 * [P1; b; c; P2];
  136. end
  137.  
  138. function out = plotGuideCircles()
  139.  
  140. circs = [40, -3; 48, 2; 37, 9.5; 28, 5.5];
  141. for i = 1 : length(circs)
  142. [I, J, K] = circle(pi/20, circs(i, 1)/2, circs(i, 2));
  143. h = plot3(I, J, K, 'black')
  144. set(h, 'linewidth', 1.2)
  145. end
  146. end
  147.  
  148. function [X, Y, Z] = circle(accuracy, x, y)
  149. X = [], Y = [], Z = []
  150. for angle = 0 : accuracy : 2*pi;
  151. X = [X, x * cos(angle)];
  152. Y = [Y, x * sin(angle)];
  153. Z = [Z y];
  154. end
  155. end
  156.  
  157. function out = funcBez(p1, p2, b, c, t)
  158.  
  159. end
  160.  
  161. function out = redbez(b, c, t)
  162. F = [3*t.*(1 - t).^2 3*(1 - t).*t.^2];
  163.  
  164. out = F * [b; c];
  165. end
Add Comment
Please, Sign In to add comment