## Linear interpolation in loop

### Sirius8 (view profile)

on 10 Sep 2019 at 9:47
Latest activity Answered by Sirius8

### Sirius8 (view profile)

about 12 hours ago
Accepted Answer by John D'Errico

### John D'Errico (view profile)

Hello, i'm trying to create a script for traveltime curves which will have as an output several plots with 2 best fit lines. To be more specific, assuming that i have a scatter plot with 10 points, i would like to have a first figure with two best fit lines: the first line created from points 1,2 and the second line from points 3 to 10. The second plot will be consisted from points 1,2,3 and the second from points 4 to 10 etc.. In the following code i have the general script which plots only one line. Thanks in advance!
x = 1:10;
y = x + randn(1,10);
scatter(x,y,40,'b')
P = polyfit(x,y,1);
yfit = P(1)*x+P(2);
hold on;
plot(x,yfit,'r-.');

Bob Nbob

### Bob Nbob (view profile)

on 10 Sep 2019 at 10:58
I'm a little confused what exactly you need help with? Are you wondering how to loop through all of the different lines you want created?

R2018a

### John D'Errico (view profile)

Answer by John D'Errico

### John D'Errico (view profile)

on 10 Sep 2019 at 11:18
Edited by John D'Errico

### John D'Errico (view profile)

on 10 Sep 2019 at 12:01

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...
1. Fit line 1 through points 1:n
2. Fit line 2 through points n+1:10
3. Create a new figure
4. Plot all of the points.
5. Use the hold function to allow you to plot new things on this same figure
6. Add the line for line 1 to the plot
7. 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:
for n = 2:numel(x)-2
That way, it works no matter how many points you have. At the end of your code, add the line:
end
The point is, make large problems into small problems. Eat a programming elephant one byte at time.

Sirius8

### Sirius8 (view profile)

on 11 Sep 2019 at 10:55
i just do not know how to implement "Fit line 1 through points 1:n".
Bob Nbob

### Bob Nbob (view profile)

on 11 Sep 2019 at 11:22
You can limit the range of the poly fit by specifying the range of values in the command with indexing.
Pbeg = polyfit(x(1:n),y(1:n),1); % First line, 1:n values
Pend = polyfit(x(n+1:end),y(n+1:end),1); % Second line for rest of points
You can put those two inside a loop and advance n to get the different lines you're looking for.
Sirius8

### Sirius8 (view profile)

on 11 Sep 2019 at 11:28
Thank you so much!!

### Sirius8 (view profile)

about 12 hours ago

Any ideas how to calculate and plot two R2 values (one for each line) in every figure?
x=[1:0.5:12.5];
y=[0.2455,0.247,0.2485,0.25,0.2516,0.253,0.254497,0.254997,0.255897,0.256497,0.257897,0.258897,0.260394,0.260884,0.261374,0.261864,0.262834,0.263814,0.265311,0.265841,0.266811,0.267301,0.267791,0.268301];
for n = 1:24
a=x(1:n);
b=y(1:n);
c=x(n+1:end);
d=y(n+1:end);
figure
plot(a,b,'.','MarkerSize', 15)
hold on
plot(c,d,'.','MarkerSize', 15)
coeffs = polyfit(a, b, 1);
fittedXa = linspace(min(a), max(a), 200);
fittedYa = polyval(coeffs, fittedXa);
hold on;
plot(fittedXa, fittedYa, 'b--', 'LineWidth', 1);
coeffs_n = polyfit(c, d, 1);
fittedXc = linspace(min(c), max(c), 200);
fittedYc = polyval(coeffs_n, fittedXc);
hold on;
plot(fittedXc, fittedYc, 'r--', 'LineWidth', 1);
legend('Data1','Data2','Linear Fit 1','Linear Fit 2')
end