Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % аппроксимируем зависимости
- % полиномиальная регрессия дает лучший результат
- y = polynomial_reg(tt(2:length(tt))', xx(2:length(tt))', 35);
- k = build_plot_disp_loss(tt(2:end)', xx(2:end)', y);
- % находим коэффициенты в заданных функциях
- x = tt(2:4);
- y = xx(2:4);
- A = [(exp(-5*x).*cos(5*x)) (exp(-5*x).*sin(5*x)) ones(3, 1)];
- disp(A);
- res = A \ y;
- disp(res);
- c1 = res(1)
- c2 = res(2)
- c3 = res(3)
- c4 = yy(3) - c2.*exp(-5*tt(3)).*cos(5*tt(3)) + exp(-5*tt(3)).*c1.*sin(5*tt(3));
- values1 = exp(-5*tt').*cos(5*tt').*c1 + exp(-5*tt').*sin(5*tt').*c2 + c3;
- % вычисляем значение функции х2 в заданных точках, и строим по ним график
- values2 = -exp(-5*tt').*sin(5*tt').*c1 + exp(-5*tt').*cos(5*tt').*c2 + c4;
- figure(2)
- plot(tt', values2, 'r');
- hold on;
- % отмечаем на графике значения, данные заранее
- plot(tt', yy', 'bo');
- % зависимость х2(х1)
- val = -exp(-5*values1').*cos(5*values1').*res(1) + exp(-5*values1').*sin(5*values1').*res(2) + res(3);
- figure(3)
- plot(values1, val, 'r');
- % линейная регрессия
- function y=lin_reg(V, bf)
- a = (sum(V) * sum(bf) - length(V) * sum(V .* bf))/(sum(V)^2 - length(V)*sum(V.^2));
- b = (sum(V .* sum(bf.*V)) - sum(V.^2 .* sum(bf)))/(sum(V)^2 - length(V)*sum(V.^2));
- y = a*V + b;
- end
- % квадратичная регрессия
- function y = square_reg(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 = degree_reg(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 = build_plot_disp_loss(V, bf, res)
- loss = loss_fn(bf, res);
- disp("loss is: " + loss * 100 + "%");
- figure(1);
- plot(V, bf, 'r');
- hold on;
- plot(V, res, 'b');
- end
- % полиномиальная регрессия
- function y = polynomial_reg(V, bf, degree)
- A = [];
- list_of_degrees = [0:degree];
- for k=0:degree
- new_column = V' .^ list_of_degrees;
- A = [sum(new_column, 1)' A];
- list_of_degrees = list_of_degrees + 1;
- end
- A(1, end) = length(V);
- list_of_degrees = [0:degree];
- B = sum((V' .^ list_of_degrees).* bf', 1)';
- list_of_degrees = [degree:-1:0];
- coeffs = A\B;
- new_column = V' .^ list_of_degrees;
- y = sum(new_column .* coeffs', 2)';
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement