1-e^x
형태의 모터의 스텝 응답을 모델링하는 방정식에서 계수를 찾으려고합니다. 내가 모델에 사용하고 방정식은[옥타브] fminunc를 사용하여 항상 일관된 솔루션을 제공하지 않습니다
a(1)*t^2 + a(2)*t^3 + a(3)*t^3 + ...
이
때때로 좋아 밖으로 작동 계수를 찾기 위해 fminunc
를 사용하여 (이것은 모터 파라미터에 대한 해결하기 위해 사용되는 연구 논문에서 파생)의 형식이며, 나는 좋은 결과를 얻었고 훈련 데이터를 상당히 잘 일치시킵니다. 다른 번에 반환 된 계수는 끔찍합니다 (출력이 있어야하는 것보다 훨씬 높고 꺼지는 순서입니다). 특히 고차원 용어를 사용하기 시작한 경우에 발생합니다. x^8
이상 (x^9
, x^10
, x^11
등)을 사용하는 모델을 사용할 경우 항상 잘못된 결과가 생성됩니다.
때때로 작동하기 때문에 구현이 잘못 될 수 있다고 생각하지 않습니다. 나는 그라디언트를 제공하는 동안 fminunc
을 시도했지만 그라디언트를 제공하지는 않았지만 차이점은 없습니다. 나는 다른 함수를 사용하여 계수를 풀어 보았습니다. 예를 들어 polyfit
처럼 1
에서 가장 높은 차수의 항으로 제기 된 항이 있어야하지만, 사용하는 모델의 전력은 2
입니다. .
clear;
%Overall Constants
max_power = 7;
%Loads in data
%data = load('TestData.txt');
load testdata.mat
%Sets data into variables
indep_x = data(:,1); Y = data(:,2);
%number of data points
m = length(Y);
%X is a matrix with the independant variable
exps = [2:max_power];
X_prime = repmat(indep_x, 1, max_power-1); %Repeats columns of the indep var
X = bsxfun(@power, X_prime, exps);
%Initializes theta to rand vals
init_theta = rand(max_power-1,1);
%Sets up options for fminunc
options = optimset('MaxIter', 400, 'Algorithm', 'quasi-newton');
%fminunc minimizes the output of the cost function by changing the theta paramaeters
[theta, cost] = fminunc(@(t)(costFunction(t, X, Y)), init_theta, options)
%
Y_line = X * theta;
figure;
hold on; plot(indep_x, Y, 'or');
hold on; plot(indep_x, Y_line, 'bx');
그리고 여기 costFunction
입니다 :
function [J, Grad] = costFunction (theta, X, Y)
%# of training examples
m = length(Y);
%Initialize Cost and Grad-Vector
J = 0;
Grad = zeros(size(theta));
%Poduces an output based off the current values of theta
model_output = X * theta;
%Computes the squared error for each example then adds them to get the total error
squared_error = (model_output - Y).^2;
J = (1/(2*m)) * sum(squared_error);
%Computes the gradients for each theta t
for t = 1:size(theta, 1)
Grad(t) = (1/m) * sum((model_output-Y) .* X(:, t));
end
endfunction
어떤 도움이나 조언을 주시면 감사하겠습니다
다음은 주요 코드입니다. 당신의 costFunction에 정규화를 추가
더 높고 높은 힘을 사용하는 것은 함수에 대한 좋은 _global_ 기초가 아니므로 더 높은 차수의 다항식을 사용하려고 시도 할 때 메서드가 분해 될 것으로 예상됩니다. – stephematician
다항식을 사용하려면 근사치 응답에 다른 기준을 사용합니다. Chebyshev 다항식을 사용하는 것이 좋습니다. – stephematician
고차원 다항식은 다루기가 매우 어렵습니다. 나는'x^4'를 볼 때 이미 불안해합니다. 훨씬 더 제한적인 모델로 시작하고, 가능한 경우 좋은 출발점을 사용하고 추정하려는 매개 변수에 좋은 경계를 적용하십시오 (이 경계를 처리하는 솔버가 필요합니다). –