0

我编写了一些代码来实现一种算法,该算法将q实数向量作为输入,并作为输出返回一个复矩阵R。下面的 Matlab 代码生成了一个显示输入向量q和输出矩阵的图R

仅给定复数矩阵输出R,我想获得输入向量q。我可以使用最小二乘优化来做到这一点吗?由于代码中存在递归运行和 (rs_rrs_i),因此输出矩阵的某一列的计算取决于前一列的计算。

也许可以设置非线性优化来q从输出矩阵重构输入向量R

换一种方式来看,我使用了一种算法来计算矩阵R。我想“反向”运行算法以q从输出矩阵中获取输入向量R

如果无法从输出中重构起始值,从而将问题视为“黑匣子”,那么模型本身的数学是否可以用于优化?该程序评估以下等式:

方程

Utilde(tau, omega) 是输出矩阵R。tau(时间)变量包含响应矩阵 的列R,而 omega(频率)变量包含响应矩阵 的行R。积分作为从 tau = 0 到当前 tau 时间步长的递归运行总和执行。

以下是由下面发布的程序创建的图:

q值输入 矩阵输出

这是完整的程序代码:

N = 1001;
q = zeros(N, 1); % here is the input
q(1:200) = 55;
q(201:300) = 120;
q(301:400) = 70;
q(401:600) = 40;
q(601:800) = 100;
q(801:1001) = 70;
dt = 0.0042;
fs = 1 / dt;
wSize = 101;
Glim = 20;
ginv = 0;
R = get_response(N, q, dt, wSize, Glim, ginv); % R is output matrix
rows = wSize; 
cols = N;

figure; plot(q); title('q value input as vector'); 
ylim([0 200]); xlim([0 1001])

figure; imagesc(abs(R)); title('Matrix output of algorithm')
colorbar

这是执行计算的函数:

function response = get_response(N, Q, dt, wSize, Glim, ginv)

fs = 1 / dt; 
Npad = wSize - 1; 
N1 = wSize + Npad;
N2 = floor(N1 / 2 + 1);
f = (fs/2)*linspace(0,1,N2);
omega = 2 * pi .* f';
omegah = 2 * pi * f(end);
sigma2 = exp(-(0.23*Glim + 1.63));

sign = 1;
if(ginv == 1)
    sign = -1;
end

ratio = omega ./ omegah;
rs_r = zeros(N2, 1);  
rs_i = zeros(N2, 1);   
termr = zeros(N2, 1);
termi = zeros(N2, 1);
termr_sub1 = zeros(N2, 1);
termi_sub1 = zeros(N2, 1);
response = zeros(N2, N);

 % cycle over cols of matrix
for ti = 1:N               

    term0 = omega ./ (2 .* Q(ti));
    gamma = 1 / (pi * Q(ti));

    % calculate for the real part
    if(ti == 1)
        Lambda = ones(N2, 1);
        termr_sub1(1) = 0;  
        termr_sub1(2:end) = term0(2:end) .* (ratio(2:end).^-gamma);  
    else
        termr(1) = 0; 
        termr(2:end) = term0(2:end) .* (ratio(2:end).^-gamma); 
        rs_r = rs_r - dt.*(termr + termr_sub1);
        termr_sub1 = termr;
        Beta = exp( -1 .* -0.5 .* rs_r );

        Lambda = (Beta + sigma2) ./ (Beta.^2 + sigma2);  % vector
    end 

    % calculate for the complex part  
    if(ginv == 1)  
        termi(1) = 0;
        termi(2:end) = (ratio(2:end).^(sign .* gamma) - 1) .* omega(2:end);
    else
        termi = (ratio.^(sign .* gamma) - 1) .* omega;
    end
    rs_i = rs_i - dt.*(termi + termi_sub1);
    termi_sub1 = termi;
    integrand = exp( 1i .* -0.5 .* rs_i );

    if(ginv == 1) 
        response(:,ti) = Lambda .* integrand;
    else        
        response(:,ti) = (1 ./ Lambda) .* integrand;
    end  
end % ti loop
4

2 回答 2

3

不,除非您知道此过程的“模型”本身,否则您不能这样做。如果您打算将该过程视为一个完整的黑匣子,那么一般来说这是不可能的,尽管在任何特定情况下,任何事情都可能发生。

即使您确实知道底层过程,但它可能仍然不起作用,因为任何最小二乘估计器都依赖于起始值,因此如果您在那里没有很好的猜测,它可能会收敛到错误的参数集。

于 2012-08-22T23:43:47.200 回答
0

事实证明,通过使用模型的数学,可以估计输入。一般来说,这不是真的,但对于我的问题,它似乎有效。通过偏导数消除累积积分。

N = 1001;
q = zeros(N, 1);
q(1:200) = 55;
q(201:300) = 120;
q(301:400) = 70;
q(401:600) = 40;
q(601:800) = 100;
q(801:1001) = 70;
dt = 0.0042;
fs = 1 / dt;
wSize = 101;
Glim = 20;
ginv = 0;
R = get_response(N, q, dt, wSize, Glim, ginv);
rows = wSize; 
cols = N;
cut_val = 200;

imagLogR = imag(log(R));

Mderiv = zeros(rows, cols-2);
for k = 1:rows
   val = deriv_3pt(imagLogR(k,:), dt);
   val(val > cut_val) = 0;
   Mderiv(k,:) = val(1:end-1);
end

disp('Running iteration');
q0 = 10;
q1 = 500;
NN = cols - 2;
qout = zeros(NN, 1);
for k = 1:NN
    data = Mderiv(:,k); 
    qout(k) = fminbnd(@(q) curve_fit_to_get_q(q, dt, rows, data),q0,q1);
end

figure; plot(q); title('q value input as vector'); 
ylim([0 200]); xlim([0 1001])

figure;
plot(qout); title('Reconstructed q')
ylim([0 200]); xlim([0 1001])

以下是支持功能:

function output = deriv_3pt(x, dt)

% Function to compute dx/dt using the 3pt symmetrical rule
% dt is the timestep

N = length(x);
N0 = N - 1;
output = zeros(N0, 1);
denom = 2 * dt;

for k = 2:N0 
   output(k - 1) = (x(k+1) - x(k-1)) / denom;  
end


function sse = curve_fit_to_get_q(q, dt, rows, data)

fs = 1 / dt;
N2 = rows;
f = (fs/2)*linspace(0,1,N2);  % vector for frequency along cols
omega = 2 * pi .* f';
omegah = 2 * pi * f(end);
ratio = omega ./ omegah;

gamma = 1 / (pi * q);

termi = ((ratio.^(gamma)) - 1) .* omega;

Error_Vector =  termi - data;
sse = sum(Error_Vector.^2);

原来的 估计的

于 2012-08-24T02:39:39.890 回答