Advertisement
tadejpetric

matlab worst language

Nov 8th, 2020 (edited)
4,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 2.40 KB | None | 0 0
  1. %%% odboji na kolobarju
  2. function bces = bounces(pos, direction, N)
  3.   % bces = []
  4.   for x = 1:N
  5.     % finds coordinates of next bounce
  6.     temp = next_bounce(pos, direction);
  7.     % finds angle
  8.     angle = bounce(temp, direction);
  9.     % rotates for that angle
  10.     direction = rotate_vec(direction, 2*angle);
  11.     pos = temp;
  12.     pos(1)^2 + pos(2)^2;
  13.    
  14.     obj.pog = pos;
  15.     obj.direction = direction;
  16.     obj.norm = pos(1)^2 + pos(2)^2;
  17.    
  18.     bces(x) = obj;
  19.   endfor
  20. endfunction
  21.  
  22. function coor = next_bounce(pos, v)
  23.   r = 3;
  24.   R = 4;
  25.   res1 = roots([(v(1)^2 + v(2)^2), (2*pos(1)*v(1) + 2*pos(2)*v(2)), (pos(1)^2+pos(2)^2-16)]);
  26.   res2 = roots([(v(1)^2 + v(2)^2), (2*pos(1)*v(1) + 2*pos(2)*v(2)), (pos(1)^2+pos(2)^2-9)]);
  27.   allroot = transpose(cat(1, res1, res2));
  28.   %allroot
  29.   allroot = filter(allroot, @(x) (x > eps && isreal(x)));
  30.   t = min(allroot);
  31.  
  32.  
  33.   coor(1) = pos(1) + t*v(1);
  34.   coor(2) = pos(2) + t*v(2);
  35. endfunction
  36.  
  37. function temp = filter(arr, cond)
  38.   temp = [];
  39.   i = 1;
  40.   for x = arr
  41.     if cond(x)
  42.       temp(i) = x;
  43.       i++;
  44.     endif
  45.   endfor
  46. endfunction
  47.  
  48. function angle = bounce(coor, v)
  49.   angle = acos((coor * v')/(norm(coor)*norm(v)));
  50. endfunction
  51.  
  52. function next = rotate_vec(vec, angle)
  53.   rotm = [cos(angle) sin(angle); -sin(angle) cos(angle)];
  54.   next = transpose(-1 * (rotm * vec'));
  55. endfunction
  56.  
  57.  
  58.  
  59.  
  60.  
  61. % horner
  62. function res = horner(poli, val, prec)
  63.   rounder = @(v, x) round(v*10^(x-1 - floor(log10(abs(v)))))/(10^(x-1- floor(log10(abs(v))))
  64.   koefs = length(poli);
  65.   vmesno = zeros(1, koefs);
  66.   results = zeros(1, koefs);
  67.   for x = 1:koefs
  68.     results(x) = rounder(poli(x) + vmesno(x), prec);
  69.     vmesno(x+1) = rounder(val * results(x), prec);
  70.   endfor
  71.   res = results(end);
  72. endfunction
  73.  
  74.  
  75.  
  76.  
  77. % regula falsi
  78. function results = regula_falsi(a, b, f, tol, N)
  79.   % isci nicle na [a,b] funkcije f
  80.   % max N korakov, rezultati razlikujejo za tol
  81.   k = (f(b)-f(a))/(b-a);
  82.   n = f(a) - k*a;
  83.   prev_zero = -n/k;
  84.  
  85.   if sign(f(prev_zero)*f(a)) > 0
  86.     a = prev_zero;
  87.   else
  88.     b = prev_zero;
  89.   endif
  90.  
  91.   results = [prev_zero];
  92.  
  93.   for x = 2:N
  94.     k = (f(b)-f(a))/(b-a);
  95.     n = f(a) - k*a;
  96.     zero = -n/k;
  97.    
  98.     results(x) = zero;
  99.    
  100.     if sign(f(zero)*f(a)) > 0
  101.       a = zero;
  102.     else
  103.       b = zero;
  104.     endif
  105.     if abs(prev_zero - zero) < tol
  106.       break;
  107.     endif
  108.     prev_zero = zero;
  109.   endfor
  110.  
  111. endfunction
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement