MATLAB MATLAB help: Interpolating an array of data to create a function

Click For Summary
To create a function x(t) from a 2D array of data in MATLAB, interpolation is a viable approach. The user seeks a method to define a function f(t) that performs linear interpolation between the array points, allowing for the use of this function in MATLAB's differential equation solvers like ode45. While polyfit() can generate a polynomial fit, the user prefers a simpler interpolation method. They attempted to use interp2 but encountered difficulties. The discussion suggests that defining f(t) for linear interpolation would enable seamless integration with built-in MATLAB functions, facilitating the solution of different differential equations without needing to explicitly determine the form of the function.
McLaren Rulez
Messages
289
Reaction score
3
Hi,

As the title suggests, I have a 2D array of data. Essentially, I solved a DE using MATLAB and it gave me the array of x against t. I want MATLAB to create a function for me by interpolation so I can use it again (to solve another different set of DEs). I've been stuck on this for a while now.

How do I go from an array of x vs t to the point where I have a function x(t)?

Thank you :)
 
Physics news on Phys.org
polyfit()?

http://www.mathworks.com/help/matlab/ref/polyfit.html

the function x(t) will be a polynomial, so you may or may not like that.

you may want to choose another function basis, like adding up exponentials to fit your data. you might want to apply polyfit() to the logarithm of your data and then exponentiate the result. there is an unlimited number of methods to fit a deterministic function to given data.

another way of looking at it is that this x(t) is ideally sampled (which means x(t) was bandlimited before sampling) and the x[n] in the array are the samples. then you want to do bandlimited reconstruction. it might look like:

x(t) = \sum_{n==\infty}^{+\infty} x[n] \ \operatorname{sinc}((t - nT)/T)

where

\operatorname{sinc}(u) = \frac{\sin(\pi u)}{\pi u}
 
Hi rbj,

Thanks for the reply. I did see polyfit but I was hoping for something slightly different. I didn't quite understand the last part of your post but I'm guessing it's a more advanced way of finding the equation of the best fit curve. The thing is, I don't need to know what this function is (it can be polynomial, exponential or anything).

I was hoping I could define a function f(t) such that if the argument is some t between my array points, MATLAB will do a linear interpolation and give me an output. I tried working with interp2 but I couldn't get what I want.

Once I do this, I think (please correct me if I am wrong) I can simply use this f(t) in the same way I use one of MATLAB's built in functions like sin(t) or exp(t) which are accepted by ode45 and other differential equation solvers. Is this possible?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K