%hf3. egyenletek
%1 linear equations:
A=[3 6 4; 1 5 0; 0 7 7];
b=[1; 2; 3];
rank(A);
x=A\b
%x=inv(A)*b
error=abs(A*x-b)

%2 numerical integration:
analytical=-24*exp(-5/3)+9;

L=0:5/10000:5;
Y=(L.*exp(-L/3));
T=trapz(L,Y)
errorT=abs(analytical-T)

F=@(x1) x1.*exp(-x1/3);
Q=quad(F, 0, 5)
errorQ=abs(analytical-Q)

%3 Computing the invers:
M=[1 2; 3 4];
IM=inv(M)
MxIM=M*IM

%4 Fitting polinomials:
ndata=load('randomData.mat');
x=ndata.x;
y=ndata.y;

[p, s, m]=polyfit(x, y, 1);
y1=polyval(p, x, s, m);
[p, s, m]=polyfit(x, y, 2);
y2=polyval(p, x, s, m);
[p, s, m]=polyfit(x, y, 3);
y3=polyval(p, x, s, m);
[p, s, m]=polyfit(x, y, 4);
y4=polyval(p, x, s, m);
[p, s, m]=polyfit(x, y, 5);
y5=polyval(p, x, s, m);

figure;
plot(x, y, 'ko', 'MarkerSize', 3, 'MarkerFaceColor', 'k');
hold on;
plot(x, y1, 'g', x, y2, 'b', x, y3, 'c', x, y4, 'm', x, y5, 'r');
xlabel('X');
ylabel('Y');
title('Polynomial fits to noisy data');
legend('Data', 'Order 1', 'Order 2', 'Order 3', 'Order 4', 'Order 5');