Using least square fitting function lsqr

조회 수: 3 (최근 30일)
Askic V
Askic V 2022년 11월 22일
댓글: Askic V 2022년 11월 22일
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.

채택된 답변

Torsten
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
sol1 = 2×1
1.0500 1.5000
sol2 = lsqr(A,b)
lsqr converged at iteration 2 to a solution with relative residual 5e-14.
sol2 = 2×1
1.0500 1.5000
sol3 = lsqlin(A,b)
sol3 = 2×1
1.0500 1.5000
sol4 = lsqminnorm(A,b)
sol4 = 2×1
1.0500 1.5000

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by