Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to Visual Basic 2010 and I've done lots of searching but am really stuck at one point. I thought I'd ask here to see if some kind soul could point me in the right direction!

I have an array of X,Y values where all X are known but only some Y are known. The Y values represent points on straight line segments and I want to be able to fill in the Y gaps. Linear interpolation between the defined Y values looks like it should work and I also want this to linearly extrapolate outside the defined values.

I sort of understand the mathematics, but I find it very difficult putting it in code without it becoming unintelligible.

I also see that in .Net, DrawLine and DrawCurve can plot between points. I've tried it but I don't know how to access the new 'drawn between' values of the line/curve, nor can I get any lines drawn outside of the defined points.

I must be missing something obvious. Any ideas anyone ?

Thanks!

......................................................................

TRK3 - no the points are set in stone so I wouldn't need the least squares fit. I might need x = y * m + b though. Thanks for the help!

Walt Fair, Jr - that was what I was after for the inbetween points - it's obvious when it's shown to you - thanks!

As far as I see it now I will look for the first and last real values of Y in the array and then interpolate between the points with your formula. Then I will have to extrapolate the two end lines, I guess by using the 2 points at each end as the slope. Strange I haven't found any code to do this though.
Anyway, cheers all!
Posted
Updated 29-Jun-11 0:01am
v2
Comments
Dr.Walt Fair, PE 29-Jun-11 23:17pm    
Extrapolation uses the same exact formula. You can go through the math to show that.
codetowns 30-Jun-11 11:12am    
In the end I used the extrapolation formula from Wikipedia and all is well with it. Am very pleased and am learning all the time. Cheers!

The formula for interpolation is straight forward, assuming your values are in X(i) in sorted order and Y(i) corresponds to X(i), Y(i) = Y(i-1) + (X(i) - X(i-1))*(Y(i+1) - Y(i-1))/(X(i+1) - X(i-1)), where the values of both X and Y are known at i-1 and i+1.

As far as the DrawLine and DrawCurve methods, they draw to the screen (or a graphics context). They don't create an array of points that you can readily read.
 
Share this answer
 
Are the Y values something measured that are supposed to be in a straight line but might vary due to measurement errors?

If so, calculating the least-squares-fit line is your best bet. Here's a good link that describes the algorithm and gives you the vb code:

http://www.vb-helper.com/howto_linear_least_squares.html[^]

The algorithm in that link gives you the equation of the best fit line in terms of a slope (m) and intercept (b) such that:

x = y * m + b

So if you have that, you can calculate y for any given x.


If on the other hand the data isn't expected to be a line, but you just want to fill in the missing y's by linearly interpolating, then Walt Fair, Jr.'s solution works great.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900