Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%% odboji na kolobarju
- function bces = bounces(pos, direction, N)
- % bces = []
- for x = 1:N
- % finds coordinates of next bounce
- temp = next_bounce(pos, direction);
- % finds angle
- angle = bounce(temp, direction);
- % rotates for that angle
- direction = rotate_vec(direction, 2*angle);
- pos = temp;
- pos(1)^2 + pos(2)^2;
- obj.pog = pos;
- obj.direction = direction;
- obj.norm = pos(1)^2 + pos(2)^2;
- bces(x) = obj;
- endfor
- endfunction
- function coor = next_bounce(pos, v)
- r = 3;
- R = 4;
- 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)]);
- 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)]);
- allroot = transpose(cat(1, res1, res2));
- %allroot
- allroot = filter(allroot, @(x) (x > eps && isreal(x)));
- t = min(allroot);
- coor(1) = pos(1) + t*v(1);
- coor(2) = pos(2) + t*v(2);
- endfunction
- function temp = filter(arr, cond)
- temp = [];
- i = 1;
- for x = arr
- if cond(x)
- temp(i) = x;
- i++;
- endif
- endfor
- endfunction
- function angle = bounce(coor, v)
- angle = acos((coor * v')/(norm(coor)*norm(v)));
- endfunction
- function next = rotate_vec(vec, angle)
- rotm = [cos(angle) sin(angle); -sin(angle) cos(angle)];
- next = transpose(-1 * (rotm * vec'));
- endfunction
- % horner
- function res = horner(poli, val, prec)
- rounder = @(v, x) round(v*10^(x-1 - floor(log10(abs(v)))))/(10^(x-1- floor(log10(abs(v))))
- koefs = length(poli);
- vmesno = zeros(1, koefs);
- results = zeros(1, koefs);
- for x = 1:koefs
- results(x) = rounder(poli(x) + vmesno(x), prec);
- vmesno(x+1) = rounder(val * results(x), prec);
- endfor
- res = results(end);
- endfunction
- % regula falsi
- function results = regula_falsi(a, b, f, tol, N)
- % isci nicle na [a,b] funkcije f
- % max N korakov, rezultati razlikujejo za tol
- k = (f(b)-f(a))/(b-a);
- n = f(a) - k*a;
- prev_zero = -n/k;
- if sign(f(prev_zero)*f(a)) > 0
- a = prev_zero;
- else
- b = prev_zero;
- endif
- results = [prev_zero];
- for x = 2:N
- k = (f(b)-f(a))/(b-a);
- n = f(a) - k*a;
- zero = -n/k;
- results(x) = zero;
- if sign(f(zero)*f(a)) > 0
- a = zero;
- else
- b = zero;
- endif
- if abs(prev_zero - zero) < tol
- break;
- endif
- prev_zero = zero;
- endfor
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement