MATLAB Answers


Linear interpolation in loop

Asked by Sirius8 on 10 Sep 2019
Latest activity Answered by Sirius8 on 19 Sep 2019 at 9:02
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);
P = polyfit(x,y,1);
yfit = P(1)*x+P(2);
hold on;

  1 Comment

Bob Nbob
on 10 Sep 2019
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?

Sign in to comment.




2 Answers

Answer by John D'Errico
on 10 Sep 2019
Edited by John D'Errico
on 10 Sep 2019
 Accepted Answer

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:
The point is, make large problems into small problems. Eat a programming elephant one byte at time.


2019 年 9 月 11 日
i just do not know how to implement "Fit line 1 through points 1:n".
Bob Nbob
2019 年 9 月 11 日
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.
2019 年 9 月 11 日
Thank you so much!!

Sign in to comment.

回答者: Sirius8
2019 年 9 月 19 日 9:02

Any ideas how to calculate and plot two R2 values (one for each line) in every figure?
for n = 1:24
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')


Sign in to comment.