Linear interpolation in loop
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ancalagon8
le 10 Sep 2019
Réponse apportée : Ancalagon8
le 19 Sep 2019
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-.');
1 commentaire
Bob Thompson
le 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?
Réponse acceptée
John D'Errico
le 10 Sep 2019
Modifié(e) : John D'Errico
le 10 Sep 2019
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:
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.
6 commentaires
Bob Thompson
le 11 Sep 2019
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.
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!