SHARE
TWEET

Untitled

a guest Dec 13th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % аппроксимируем зависимости
  2. % полиномиальная регрессия дает лучший результат
  3. y =  polynomial_reg(tt(2:length(tt))', xx(2:length(tt))', 35);
  4. k = build_plot_disp_loss(tt(2:end)', xx(2:end)', y);
  5. % находим коэффициенты в заданных функциях
  6. x = tt(2:4);
  7. y = xx(2:4);
  8. A = [(exp(-5*x).*cos(5*x)) (exp(-5*x).*sin(5*x)) ones(3, 1)];
  9. disp(A);
  10. res = A \ y;
  11. disp(res);
  12. c1 = res(1)
  13. c2 = res(2)
  14. c3 = res(3)
  15. c4 = yy(3) - c2.*exp(-5*tt(3)).*cos(5*tt(3)) + exp(-5*tt(3)).*c1.*sin(5*tt(3));
  16. values1 = exp(-5*tt').*cos(5*tt').*c1 + exp(-5*tt').*sin(5*tt').*c2 + c3;
  17. % вычисляем значение функции х2 в заданных точках, и строим по ним график
  18. values2 = -exp(-5*tt').*sin(5*tt').*c1 + exp(-5*tt').*cos(5*tt').*c2 + c4;
  19. figure(2)
  20. plot(tt', values2, 'r');
  21. hold on;
  22. % отмечаем на графике значения, данные заранее
  23. plot(tt', yy', 'bo');
  24. % зависимость х2(х1)
  25. val = -exp(-5*values1').*cos(5*values1').*res(1) + exp(-5*values1').*sin(5*values1').*res(2) + res(3);
  26. figure(3)
  27. plot(values1, val, 'r');
  28.  
  29. % линейная регрессия
  30. function y=lin_reg(V, bf)
  31.     a = (sum(V) * sum(bf) - length(V) * sum(V .* bf))/(sum(V)^2 - length(V)*sum(V.^2));
  32.     b = (sum(V .* sum(bf.*V)) - sum(V.^2 .* sum(bf)))/(sum(V)^2 - length(V)*sum(V.^2));
  33.     y = a*V + b;
  34. end
  35. % квадратичная регрессия
  36. function y = square_reg(V, bf)
  37.     A = [sum(V.^2) sum(V) length(V);
  38.          sum(V.^3) sum(V.^2) sum(V);
  39.          sum(V.^4) sum(V.^3) sum(V.^2)];
  40.     B = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
  41.     x = A\B;
  42.     a = x(1);
  43.     b = x(2);
  44.     c = x(3);
  45.     y = a * V.^2 + b * V + c;
  46. end
  47. % степенная регрессия
  48. function y = degree_reg(V, bf)
  49.     a = [length(V) sum(log(V));
  50.          sum(log(V)) sum(log(V).^2)];
  51.     b = [sum(log(bf)) sum(log(bf).*log(V))]';
  52.     x = a\b;
  53.     a = exp(x(1));
  54.     b = x(2);
  55.     y = a * V.^b;
  56. end
  57.  
  58. function y = loss_fn(bf, res)
  59.     y = 1/length(bf) * sum(abs((bf - res)./(bf)));
  60. end
  61.  
  62. function loss = build_plot_disp_loss(V, bf, res)
  63.     loss = loss_fn(bf, res);
  64.     disp("loss is: " + loss * 100 + "%");
  65.     figure(1);
  66.     plot(V, bf, 'r');
  67.     hold on;
  68.     plot(V, res, 'b');
  69. end
  70. % полиномиальная регрессия
  71. function y = polynomial_reg(V, bf, degree)
  72.     A = [];
  73.     list_of_degrees = [0:degree];
  74.     for k=0:degree
  75.         new_column = V' .^ list_of_degrees;
  76.         A = [sum(new_column, 1)' A];
  77.         list_of_degrees = list_of_degrees + 1;
  78.     end
  79.     A(1, end) = length(V);
  80.     list_of_degrees = [0:degree];
  81.     B = sum((V' .^ list_of_degrees).* bf', 1)';
  82.     list_of_degrees = [degree:-1:0];
  83.     coeffs = A\B;
  84.     new_column = V' .^ list_of_degrees;
  85.     y = sum(new_column .* coeffs', 2)';
  86. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top