while and for cicle

1 view (last 30 days)
alessio morabito
alessio morabito on 2 May 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 Comments
Image Analyst
Image Analyst on 2 May 2021
It seems you forgot to attach a .mat file with s, x, y, etc. in it.
alessio morabito
alessio morabito on 3 May 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)

Sign in to comment.

Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!