Advertisement
Tooster

Untitled

Dec 19th, 2018
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.56 KB | None | 0 0
  1. using Plots
  2. runge = x->1/(1+x^2)
  3. f = x->1/(1+25x^2)
  4. #P2D to macierz Nx2 o wierszach [x y]
  5. # współczynniki ilorazów dla interp. Newtona
  6. function _diff(P2D)
  7.     b, n = Array(P2D[:, 2]), length(P2D[:, 1])
  8.     for  i = 2:n
  9.         for j = n:-1:i
  10.             b[j] = (b[j] - b[j-1])/(P2D[j, 1] - P2D[j-i+1, 1])
  11.         end
  12.     end
  13.     return b
  14. end
  15.  
  16. # Zwraca wartość ilorazu.
  17. function diff(P2D)
  18.     last(_diff(P2D))
  19. end
  20.  
  21. function interp_newton(P2D)
  22.     return function(x)
  23.         b, n = _diff(P2D), length(P2D[:, 1])-1
  24.         sum, p = b[1], 1
  25.         for i in 1:n
  26.             p *= (x - P2D[i, 1])
  27.             sum += b[i+1]*p
  28.             end # nie chce mi się robić Hornera
  29.         sum
  30.     end
  31. end;
  32.  
  33. err = f_interp->x->abs(f(x)-f_interp(x))
  34. w9_eq = interp_newton(reduce(vcat, map(x->[x f(x)], collect(range(-1,stop=1, length=10)))))             # w równoodległych
  35. w9_T10_zeros = interp_newton(reduce(vcat, map(x->[x f(x)], map(j->cos(pi*((2*j-1)/(2*10))), [1:10;])))) # w zerach Czebyszewa
  36. equal_plot = begin
  37.     plot(f, -1, 1, line=(color=:blue), label="f", title="equal")
  38.     plot!(w9_eq, line=(color=:orange), label="equal")
  39.     plot!(err(w9_eq), fill=(0, 0.1, :red), label="EQ error")
  40. end
  41. Chebyshev_zeros_plot = begin
  42.     plot(f, -1, 1, line=(color=:blue), label="f", title="Chebyshev's zeros")
  43.     plot!(w9_T10_zeros, line=(color=:orange), label="Ch. zeros")
  44.     plot!(err(w9_T10_zeros), fill=(0, 0.1, :red), label="CH. zeros error")
  45. end
  46. row_plot = plot(equal_plot, Chebyshev_zeros_plot, layout=(1,2), fmt=:png, size=(900, 300), ylims=(-0.5, 2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement