I totally agree with Bob. (By the way, this is not linear interpolation. Interpolation passes exactly through ALL points.)

You know what you want to do, so do it. I think your problem is you have devised a task that is too large for your current programming capability to know how to do. So you gave up. Break it down into SMALL tasks. Solve each part. Put it together.

You want to fit TWO lines on each pass, but on each pass you will need to create a new plot. That is the overview of things.

What you do not say, but I have a funny feeling, is you want those lines to be connected. That is because you claim to want to do linear interpolation. Remember, if you just do two independent fits, the lines may intersect, but you have no idea where they will intersect. If this is not important, then figure out how to solve the set of sub-problems where you...

- Fit line 1 through points 1:n
- Fit line 2 through points n+1:10
- Create a new figure
- Plot all of the points.
- Use the hold function to allow you to plot new things on this same figure
- Add the line for line 1 to the plot
- Add the line for line 2 to the plot

Each of those tasks is by itself, trivial. So do them. ONE AT A TIME.

When you are done with that, then at the very beginning of your code, add the line:

That way, it works no matter how many points you have. At the end of your code, add the line:

The point is, make large problems into small problems. Eat a programming elephant one byte at time.

## 1 Comment

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/479686-linear-interpolation-in-loop#comment_744308

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/479686-linear-interpolation-in-loop#comment_744308

Sign in to comment.