Using least square fitting function lsqr
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I need an example how to apply Matlab's built in lsqr function to solve the following task.
let's say I have a sequence of square pulses which is transformed in the following way:
rng(0)
n = 20;
s = rand(1, n) > 0.5;
s = repmat(s', 1, 100)';
s = s(:)';
t = linspace(0, n, numel(s));
subplot(2, 1, 1)
plot(t, s, 'Linewidth',2)
scale_factor = 1.05;
offset = 1.5;
t2 = t * scale_factor + offset;
subplot(2, 1, 2)
plot(t2, s, 'LineWidth',2)
Now if that previous transformation is unknown and I need to estimate scale_factor and offset i.e. my initial condition is actually only a plot data:

or my only inputs are:
t1_n = t(s==1);
T1 = [t1_n; ones(size(t1_n))];
t2_n = t2(s==1);
T2 = [t2_n; ones(size(t2_n))];
How to use lsqr function to calculate scale_factor and offset. I know that for this particular case, I only need to points to calculate coefficients of line equation y = a*x+b, but in general, data can be noisy, so more points are needed and thus the obvious choise would be to use least square fitting.
Thank you!
P.S. I forgot to mention, I can solve this problem using the function lsqcurvefit, but I would like to see how lsqr can be applied.
0 个评论
采纳的回答
Torsten
2022-11-22
编辑:Torsten
2022-11-22
rng(0)
n = 20;
s = rand(1, n) > 0.5;
s = repmat(s', 1, 100)';
s = s(:)';
t = linspace(0, n, numel(s));
subplot(2, 1, 1)
plot(t, s, 'Linewidth',2)
scale_factor = 1.05;
offset = 1.5;
t2 = t * scale_factor + offset;
subplot(2, 1, 2)
plot(t2, s, 'LineWidth',2)
t1_n = t(s==1).';
t2_n = t2(s==1).';
A = [t1_n,ones(size(t1_n))];
b = t2_n;
sol1 = A\b
sol2 = lsqr(A,b)
sol3 = lsqlin(A,b)
sol4 = lsqminnorm(A,b)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
