Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.57 KB | None | 0 0
  1. y = lineal_regress(V, b2f);
  2. %y = kvadr_regress(V, b1f);
  3. %y = stepen_poly(V, b1f);
  4. k = plot_disp_loss(V, b2f, y);
  5.  
  6. function y=lineal_regress(V, bf)
  7.     A = [sum(V) length(V);
  8.         sum(V.^2) sum(V)];
  9.     B = [sum(bf) sum(V .* bf)]';
  10.     x = A\B;
  11.     a = x(1);
  12.     b = x(2);
  13.     y = a*V + b;
  14. end
  15.  
  16. function y = kvadr_regress(V, bf)
  17.     A = [sum(V.^2) sum(V) length(V);
  18.          sum(V.^3) sum(V.^2) sum(V);
  19.          sum(V.^4) sum(V.^3) sum(V.^2)];
  20.     B = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
  21.     x = A\B;
  22.     a = x(1);
  23.     b = x(2);
  24.     c = x(3);
  25.     y = a * V.^2 + b * V + c;
  26. end
  27.  
  28. function y = stepen_poly(V, bf)
  29.     A = [length(V) sum(log(V));
  30.         sum(log(V)) sum(log(V).^2)];
  31.     B = [sum(log(bf)) sum(log(bf).*log(V))]';
  32.     x = A\B;
  33.     a = exp(x(1));
  34.     b = x(2);
  35.     y = a * V.^b;
  36. end
  37.  
  38. function y = loss_fn(bf, res)
  39.     y = 1/length(bf) * sum(abs((bf - res)./bf));
  40. end
  41.  
  42. function loss = plot_disp_loss(V, bf, res)
  43.     loss = loss_fn(bf, res);
  44.     disp("loss is: " + loss * 100 + "%");
  45.     figure('Color', 'w');
  46.     plot(V, bf, 'r');
  47.     hold on;
  48.     plot(V, res, 'b');
  49. end
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  51.  
  52.  
  53. %disp(V);
  54. %disp(b1f);
  55.  
  56. %disp(a);
  57. %disp(b);
  58. y = regr_n_degree(tt(2:length(tt))', xx(2:length(tt))', 10);
  59. k = build_plot_disp_loss(tt(2:end)', xx(2:end)', y);
  60.  
  61. function y=lineal_regress(V, bf)
  62.     A = [sum(V) length(V);
  63.         sum(V.^2) sum(V)];
  64.     B = [sum(bf) sum(V .* bf)]';
  65.     x = A\B;
  66.     a = x(1);
  67.     b = x(2);
  68.     y = a*V + b;
  69. end
  70.  
  71. function y = kvadr_regress(V, bf)
  72.     A = [sum(V.^2) sum(V) length(V);
  73.          sum(V.^3) sum(V.^2) sum(V);
  74.          sum(V.^4) sum(V.^3) sum(V.^2)];
  75.     B = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
  76.     x = A\B;
  77.     a = x(1);
  78.     b = x(2);
  79.     c = x(3);
  80.     y = a * V.^2 + b * V + c;
  81. end
  82.  
  83. function y = regr_n_degree(V, bf, degree)
  84.     A = [];
  85.     pow = [0:degree];
  86.     for k=0:degree
  87.         tmp = V' .^ pow;
  88.         A = [sum(tmp, 1)' A];
  89.         pow = pow + 1;
  90.     end
  91.     A(1, end) = length(V);
  92.     pow = [0:degree];
  93.     B = sum((V' .^ pow).* bf', 1)';
  94.     pow = [degree:-1:0];
  95.     coeffs = A\B;
  96.     tmp = V' .^ pow;
  97.     y = sum(tmp .* coeffs', 2)';
  98. end
  99.  
  100.  
  101. function y = stepen_poly(V, bf)
  102.     A = [length(V) sum(log(V));
  103.         sum(log(V)) sum(log(V).^2)];
  104.     B = [sum(log(bf)) sum(log(bf).*log(V))]';
  105.     x = A\B;
  106.     a = exp(x(1));
  107.     b = x(2);
  108.     y = a * V.^b;
  109. end
  110.  
  111. function y = loss_fn(bf, res)
  112.     y = 1/length(bf) * sum(abs((bf - res)./(bf)));
  113.     %disp(bf);
  114. end
  115.  
  116. function loss = build_plot_disp_loss(V, bf, res)
  117.     loss = loss_fn(bf, res);
  118.     disp("loss is: " + loss * 100 + "%");
  119.     figure('Color', 'w');
  120.     plot(V, bf, 'r');
  121.     hold on;
  122.     plot(V, res, 'b');
  123. end
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. x = tt(2:end);
  126. y = xx(2:end);
  127. A = [ (exp(-5*x).*cos(5*x)) (exp(-5*x).*sin(5*x)) ones(length(tt) - 1, 1)];
  128. %disp(A);
  129. res = A \ y;
  130. %disp(res);
  131. x1 = @(x) exp(-5*x).*cos(5*x).*res(1) + exp(-5*x).*sin(5*x).*res(2) + res(3);
  132. %values1 = exp(-5*tt').*cos(5*tt').*res(1) + exp(-5*tt').*sin(5*tt').*res(2) + res(3);
  133. values1 = x1(tt);
  134. c4 = yy(3) - res(2).*exp(-5*tt(3)).*cos(5*tt(3)) + exp(-5*tt(3)).*res(1).*sin(5*tt(3));
  135. x2 = @(x) -exp(-5*x).*sin(5*x).*res(1) + exp(-5*x).*cos(5*x).*res(2) + c4;
  136. %values2 = -exp(-5*tt').*sin(5*tt').*res(1) + exp(-5*tt').*cos(5*tt').*res(2) + c4;
  137. values2 = x2(tt);
  138.  
  139. plot(tt', values2, 'r');
  140. hold on;
  141. plot(tt', yy', 'b*');
  142.  
  143. %plot(x1(tt)', x2(x1(tt)), 'r');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement