Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. function [y, err] = regressao_linear(xp, yp, x, tol)
  2. y=''
  3. if size(xp)(2)==0 then
  4. err='Erro de entrada: quantidade de pontos é 0'
  5. return
  6. end
  7. if size(xp)~=size(yp) then
  8. err='Erro de entrada: vetores de tamanhos diferentes'
  9. return
  10. end
  11. if tol<0 then
  12. err='Erro de entrada: tolerancia < 0'
  13. return
  14. end
  15. for i=1:size(xp)(2)
  16. for j=i+1:size(xp)(2)
  17. if xp(i)==xp(j) then
  18. err='Erro de entrada: existem pontos com o mesmo x'
  19. return
  20. end
  21. end
  22. end
  23. mediax=0
  24. mediay=0
  25. for i=1:size(xp)(2)
  26. mediax=mediax+xp(i)/(size(xp)(2))
  27. mediay=mediay+yp(i)/(size(yp)(2))
  28. end
  29. for i=1:size(xp)(2)
  30. b=(xp(i)*(yp(i)-mediay))/(xp(i)*(xp(i)-mediax))
  31. end
  32. a=mediay-b*mediax
  33. y=a+b*x
  34. err=''
  35. endfunction
  36.  
  37. function y = calcula(f,x)
  38. y=evstr(f)
  39. endfunction
  40.  
  41. function [A, b, err] = montar_sistema(xp, yp, g, tol)
  42. //A*x=b
  43. for i=1:size(g)(2)
  44. for j=1:size(g)(2)
  45. temp=0
  46. for k=1:size(xp)(2)
  47. temp=temp+calcula(g(i),xp(k))*calcula(g(j),xp(k))
  48. end
  49. A(i)(j)=temp
  50. end
  51. end
  52. for i=1:size(g)(2)
  53. temp=0
  54. for j=1:size(yp)(2)
  55. temp=temp+yp(j)*calcula(g(i),xp(j))
  56. end
  57. b(i)=temp
  58. end
  59. x=inv(A)*b
  60. disp(x)
  61. err=''
  62. endfunction
  63.  
  64. [A, b, err]=montar_sistema([2 3 5], [7 13 31], ['x' 'x^2' '2'], 0)
  65.  
  66. [y, err]=regressao_linear([1 5 3], [4.3 20.3 12.3], 4, 0.001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement