Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- y = lineal_regress(V, b2f);
- %y = kvadr_regress(V, b1f);
- %y = stepen_poly(V, b1f);
- k = plot_disp_loss(V, b2f, y);
- function y=lineal_regress(V, bf)
- A = [sum(V) length(V);
- sum(V.^2) sum(V)];
- B = [sum(bf) sum(V .* bf)]';
- x = A\B;
- a = x(1);
- b = x(2);
- y = a*V + b;
- end
- function y = kvadr_regress(V, bf)
- A = [sum(V.^2) sum(V) length(V);
- sum(V.^3) sum(V.^2) sum(V);
- sum(V.^4) sum(V.^3) sum(V.^2)];
- B = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
- x = A\B;
- a = x(1);
- b = x(2);
- c = x(3);
- y = a * V.^2 + b * V + c;
- end
- function y = stepen_poly(V, bf)
- A = [length(V) sum(log(V));
- sum(log(V)) sum(log(V).^2)];
- B = [sum(log(bf)) sum(log(bf).*log(V))]';
- x = A\B;
- a = exp(x(1));
- b = x(2);
- y = a * V.^b;
- end
- function y = loss_fn(bf, res)
- y = 1/length(bf) * sum(abs((bf - res)./bf));
- end
- function loss = plot_disp_loss(V, bf, res)
- loss = loss_fn(bf, res);
- disp("loss is: " + loss * 100 + "%");
- figure('Color', 'w');
- plot(V, bf, 'r');
- hold on;
- plot(V, res, 'b');
- end
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- %disp(V);
- %disp(b1f);
- %disp(a);
- %disp(b);
- y = regr_n_degree(tt(2:length(tt))', xx(2:length(tt))', 10);
- k = build_plot_disp_loss(tt(2:end)', xx(2:end)', y);
- function y=lineal_regress(V, bf)
- A = [sum(V) length(V);
- sum(V.^2) sum(V)];
- B = [sum(bf) sum(V .* bf)]';
- x = A\B;
- a = x(1);
- b = x(2);
- y = a*V + b;
- end
- function y = kvadr_regress(V, bf)
- A = [sum(V.^2) sum(V) length(V);
- sum(V.^3) sum(V.^2) sum(V);
- sum(V.^4) sum(V.^3) sum(V.^2)];
- B = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
- x = A\B;
- a = x(1);
- b = x(2);
- c = x(3);
- y = a * V.^2 + b * V + c;
- end
- function y = regr_n_degree(V, bf, degree)
- A = [];
- pow = [0:degree];
- for k=0:degree
- tmp = V' .^ pow;
- A = [sum(tmp, 1)' A];
- pow = pow + 1;
- end
- A(1, end) = length(V);
- pow = [0:degree];
- B = sum((V' .^ pow).* bf', 1)';
- pow = [degree:-1:0];
- coeffs = A\B;
- tmp = V' .^ pow;
- y = sum(tmp .* coeffs', 2)';
- end
- function y = stepen_poly(V, bf)
- A = [length(V) sum(log(V));
- sum(log(V)) sum(log(V).^2)];
- B = [sum(log(bf)) sum(log(bf).*log(V))]';
- x = A\B;
- a = exp(x(1));
- b = x(2);
- y = a * V.^b;
- end
- function y = loss_fn(bf, res)
- y = 1/length(bf) * sum(abs((bf - res)./(bf)));
- %disp(bf);
- end
- function loss = build_plot_disp_loss(V, bf, res)
- loss = loss_fn(bf, res);
- disp("loss is: " + loss * 100 + "%");
- figure('Color', 'w');
- plot(V, bf, 'r');
- hold on;
- plot(V, res, 'b');
- end
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////
- x = tt(2:end);
- y = xx(2:end);
- A = [ (exp(-5*x).*cos(5*x)) (exp(-5*x).*sin(5*x)) ones(length(tt) - 1, 1)];
- %disp(A);
- res = A \ y;
- %disp(res);
- x1 = @(x) exp(-5*x).*cos(5*x).*res(1) + exp(-5*x).*sin(5*x).*res(2) + res(3);
- %values1 = exp(-5*tt').*cos(5*tt').*res(1) + exp(-5*tt').*sin(5*tt').*res(2) + res(3);
- values1 = x1(tt);
- c4 = yy(3) - res(2).*exp(-5*tt(3)).*cos(5*tt(3)) + exp(-5*tt(3)).*res(1).*sin(5*tt(3));
- x2 = @(x) -exp(-5*x).*sin(5*x).*res(1) + exp(-5*x).*cos(5*x).*res(2) + c4;
- %values2 = -exp(-5*tt').*sin(5*tt').*res(1) + exp(-5*tt').*cos(5*tt').*res(2) + c4;
- values2 = x2(tt);
- plot(tt', values2, 'r');
- hold on;
- plot(tt', yy', 'b*');
- %plot(x1(tt)', x2(x1(tt)), 'r');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement