Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [y, err] = regressao_linear(xp, yp, x, tol)
- y=''
- if size(xp)(2)==0 then
- err='Erro de entrada: quantidade de pontos é 0'
- return
- end
- if size(xp)~=size(yp) then
- err='Erro de entrada: vetores de tamanhos diferentes'
- return
- end
- if tol<0 then
- err='Erro de entrada: tolerancia < 0'
- return
- end
- for i=1:size(xp)(2)
- for j=i+1:size(xp)(2)
- if xp(i)==xp(j) then
- err='Erro de entrada: existem pontos com o mesmo x'
- return
- end
- end
- end
- mediax=0
- mediay=0
- for i=1:size(xp)(2)
- mediax=mediax+xp(i)/(size(xp)(2))
- mediay=mediay+yp(i)/(size(yp)(2))
- end
- for i=1:size(xp)(2)
- b=(xp(i)*(yp(i)-mediay))/(xp(i)*(xp(i)-mediax))
- end
- a=mediay-b*mediax
- y=a+b*x
- err=''
- endfunction
- function y = calcula(f,x)
- y=evstr(f)
- endfunction
- function [A, b, err] = montar_sistema(xp, yp, g, tol)
- //A*x=b
- for i=1:size(g)(2)
- for j=1:size(g)(2)
- temp=0
- for k=1:size(xp)(2)
- temp=temp+calcula(g(i),xp(k))*calcula(g(j),xp(k))
- end
- A(i)(j)=temp
- end
- end
- for i=1:size(g)(2)
- temp=0
- for j=1:size(yp)(2)
- temp=temp+yp(j)*calcula(g(i),xp(j))
- end
- b(i)=temp
- end
- x=inv(A)*b
- disp(x)
- err=''
- endfunction
- [A, b, err]=montar_sistema([2 3 5], [7 13 31], ['x' 'x^2' '2'], 0)
- [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