# while and for cicle

3 visualizzazioni (ultimi 30 giorni)
alessio morabito il 2 Mag 2021
hi guys.
I'm working on a data set of points (x,y) and i have to write an algorithm that compute the linear regression of this points till a lower and/or an upper bound are exceeded.
So i use lsqlin with upper and lower bounds..... but i have to write a for cicle to work in this way:
-Starting from the first 3 points, if the linear regression respect the bounds i add them the fourth point, and so on.... till the constraints are not satisfied; if for example the last point for which the constraints are satisfied is the 10-th, the next iteration must restart from the 11-th point to the 13-th and start over, till the constraints continue to be satisfied. (if the first 3 points of a group doesn't respect the constraints, i exclude the first point and try with 2, 3 and 4, and so on)
i wrote this part of code but i have some problems, because i need to restart the value of jj when i restart the iteration.
for jj=2:length(s)-i
k=i+jj;
S=s(i:k);
Y=y(i:k);
C=[S,ones(length(S),1)];
[X,resnorm,residual,exitflag]=lsqlin(C,Y,A,b,Aeq,beq,lb,ub); %ad ogni I-esima iterazione vado ad allocare nella I-esima colonna di X il coeff angolare K e l'intercetta b della I-esima tangente
if exitflag>0
xprog=S;
yprog=X(1)*xprog+X(2);
DELTA_Y=Y-yprog;
plot(xprog,yprog,'r--',C(:,1),Y,'.b')
else if k+1<length(s)-2
i=k+1;
hold on
end
end
end
##### 2 CommentiMostra NessunoNascondi Nessuno
Image Analyst il 2 Mag 2021
It seems you forgot to attach a .mat file with s, x, y, etc. in it.
alessio morabito il 3 Mag 2021
Thanks for your answer! I posted Only the cicle for, the code Is longer than that. I extrapolate s,y from an .xls file. But this cicle for doesn't work as i Need, because i Need to restart JJ Every time the constraints are not satisfied (so, i Need to restart the iteration)

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by