Linear Interpolation without interp1

So I have a range of data values (in asending orders of x) for x and y coordinates. I ask the user to enter an x value between the range of values. My code should basically caculate the linear gradient from the nearest data points at either side of the entered value. This is part of a big script but this part is causing difficulties. I thought there would be some way to identify the previous/next element from the entered x value in the array but I can''t manage to do this. Any help would be greatly appreciated.

6 Comments

I already have relevant code regarding if the entered x value is a data point
That sounds like something interp1 can do. Why not use it?
A requirement for the project is to not use in built functions
Stephen23
Stephen23 on 25 Nov 2018
Edited: Stephen23 on 25 Nov 2018
So this is homework?
Linear interpolation is quite simple. What have you tried so far?
Then you need to do some thinking. After all, it is your homework. Surely you can just use a loop going through the data. If you want, sort them first. And, if you are not allowed to use sort, or max, or tools like that, nothing stops you from writing the necessary tools. If x is sorted already, then it is a simple search to find the largest value in x that does not exceed the point in question. It is still easy even if not sorted. I might even guess that along the way, you may have had to write a sorting tool at some point.
So, yes, there are lots of ways to do this, but you yourself admit that you cannot use the functions that would make your problem easy.

(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 25 Nov 2018
Edited: Stephen23 on 25 Nov 2018
"I thought there would be some way to identify the previous/next element from the entered x value in the array but I can't manage to do this"
Of course this is easy with some logical comparisons:
>> Xv = 1:5
Xv =
1 2 3 4 5
>> Xq = 2.5;
>> ida = find(Xv<Xq,1,'last')
ida = 2
>> idb = find(Xv>Xq,1,'first')
idb = 3
Of course you will also need to hande some special cases, e.g. when Xq is equal to one of the values in Xv, or e.g. Xq is outside the range of values in Xv.

7 Comments

This is very useful, thank you. Ok so my data does not go up in regular interval as yours does. How would I alter this?
Stephen23
Stephen23 on 25 Nov 2018
Edited: Stephen23 on 25 Nov 2018
"Ok so my data does not go up in regular interval as yours does. How would I alter this?"
The interval is irrelevant to this method, as long as your data is strictly monotonically increasing it will work just fine.
What does first and last mean?
Stephen23
Stephen23 on 25 Nov 2018
Edited: Stephen23 on 25 Nov 2018
'What does first and last mean?"
The purpose of first and last is explained in the find help. I presume that you have read the find help: which parts of the explanation did you not understand?
>>ida = find(t<tval,1,'last')
>>idb = find(t>tval,1,'first')
where t is 300:100:1600
tval is entered t value e.g. t=340
e.g. 340 should produce values of ida=300 and idb=400. And I've no idea why it does not
"e.g. 340 should produce values of ida=300 and idb=400. And I've no idea why it does not"
Not it should not. If you had read the find documentation you would know that for its first output argument find returns indices of the non-zero elements, not values of the elements. Reading the find documentation is a good way to know what find actually does.
Using those indices is easy:
t(ida)
t(idb)

Sign in to comment.

Categories

Asked:

on 25 Nov 2018

Edited:

on 28 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!