Linear interpolation in loop
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ancalagon8
el 10 de Sept. de 2019
Editada: Ancalagon8
el 6 de En. de 2025
Hello, i'm trying to create a script for curves which will have as an output several plots. Aassuming that i have a scatter plot with 10 points, i would like to have a first figure with two best fit lines
1 comentario
Bob Thompson
el 10 de Sept. de 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?
Respuesta aceptada
John D'Errico
el 10 de Sept. de 2019
Editada: John D'Errico
el 10 de Sept. de 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.
3 comentarios
John D'Errico
el 10 de Sept. de 2019
Editada: John D'Errico
el 10 de Sept. de 2019
What Bob said. What part of what I explained is too difficult? You know how to use polyfit. Start at the beginning. This is how you write code. One line at a time.
Answers is not here as a software service, where we write code for you to your specs. I'm sorry, but I'll explain what you need to do. You need to take it from there.
Bob Thompson
el 11 de Sept. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!