Using least square fitting function lsqr

4 vues (au cours des 30 derniers jours)
Askic V
Askic V le 22 Nov 2022
Commenté : Askic V le 22 Nov 2022
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.

Réponse acceptée

Torsten
Torsten le 22 Nov 2022
Modifié(e) : Torsten le 22 Nov 2022
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
  1 commentaire
Askic V
Askic V le 22 Nov 2022
Perfect, thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics and Optimization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by