Using least square fitting function lsqr
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
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
sol2 = lsqr(A,b)
sol3 = lsqlin(A,b)
sol4 = lsqminnorm(A,b)
Plus de réponses (0)
Voir également
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!
