Advertisement
shadrin121

%nonlinear/multivariable_linea%linear matrix/linear regressi

Jan 21st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. %nonlinear regresion
  2.  
  3. clc; clear; close all;
  4.  
  5. g = @(x) exp(x);
  6. s = @(x) 0.25*exp(x/2);
  7. X = [1, g(2), s(2);
  8. 1, g(3), s(3);
  9. 1, g(5), s(5);
  10. 1, g(10), s(10)];
  11.  
  12. Y = [2;
  13. 5;
  14. 10;
  15. 17]
  16.  
  17. a = ((((X.')*X)^(-1))*(X.'))*Y
  18.  
  19. f = @(x) a(1) + a(2)*g(x) + a(3)*s(x);
  20. x = 0:0.01:10;
  21.  
  22. xs = [2,3,5,10];
  23.  
  24. figure(1)
  25. plot(x,f(x)); hold on; grid on;
  26. plot(xs,Y,'ro'); hold off;
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. ```````````````````````````
  46. %multivariable_linear_regression
  47. clc; clear; close all;
  48.  
  49. xL=[2 4 6 8 10];
  50. X=[1, 2, 3;
  51. 1, 4, 5;
  52. 1, 5, 7;
  53. 1, 6, 8 ]
  54. Y=[5 ;7; 11; 15]
  55.  
  56. a = ((((X.')*X)^-1)*(X.'))*Y
  57.  
  58. k = 1;
  59. f = @(x) a(3)*x + a(2)*x + 1;
  60. x=0:0.01:15;
  61.  
  62. figure(1)
  63. plot(x,f(x))
  64.  
  65. -1.8 + (-1.4)*4+3*5
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. ~`````````````````````````````````````````````
  81. %linear regresion matrix form
  82. clc; clear; close all;
  83.  
  84. xL=[2 4 6 8 10];
  85. X=[2, 1;
  86. 4, 1;
  87. 6, 1;
  88. 8, 1;
  89. 10, 1]
  90. Y=[5 ;7; 11; 40; 32]
  91.  
  92. a = ((((X.')*X)^-1)*(X.'))*Y
  93.  
  94. f = @(x) a(1)*x + a(2)
  95.  
  96. x = 0:0.1:10;
  97.  
  98. figure(1)
  99. plot(x, f(x)); grid on; hold on;
  100. plot(xL, Y, 'ro'); hold off;
  101. axis([-1,11,-15,50])
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. `````````````````````````````````````````````````````````````````````````````
  111. %liear regression
  112. clc; clear; close all;
  113.  
  114. x=[2 4 6 8 10];
  115. y=[5 7 11 40 32];
  116. mx=(sum(x))/length(x);
  117. my=(sum(y))/length(y);
  118. for i=1:length(x)
  119. ap(i)=(x(i)-mx)*(y(i)-my);
  120. an=sum(ap);
  121. end
  122. for k=1:length(x)
  123. il(k)=(x(k)-mx)^2;
  124. ilo=sum(il);
  125. end
  126. a=(an)/(ilo)
  127. b=my-a*mx
  128. wsp=[a,b];
  129. f=a*x+b;
  130. plot(x,y,'ro');
  131. hold on;
  132. plot(x,f,'green'); % rysowanie i kolory
  133. axis([0 20 0 50]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement