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

Click For Summary
SUMMARY

The discussion focuses on interpolating a 2D array of data in MATLAB to create a function x(t) for solving differential equations. The user initially considers using the polyfit() function for polynomial fitting but seeks alternatives for linear interpolation. The conversation highlights the potential of using interp2 for interpolation and discusses the concept of bandlimited reconstruction for more advanced fitting methods. Ultimately, the user aims to define a function f(t) that can be utilized similarly to built-in MATLAB functions in differential equation solvers like ode45.

PREREQUISITES
  • Understanding of MATLAB programming and syntax.
  • Familiarity with interpolation techniques, specifically interp2.
  • Knowledge of polynomial fitting using polyfit().
  • Basic concepts of differential equations and solvers like ode45.
NEXT STEPS
  • Explore MATLAB's interp1 for 1D interpolation methods.
  • Research bandlimited reconstruction techniques in MATLAB.
  • Learn about alternative fitting functions such as fit() for custom function fitting.
  • Investigate the use of griddatan for multidimensional interpolation in MATLAB.
USEFUL FOR

MATLAB users, engineers, and scientists working with data interpolation and differential equations who need to create custom functions from data arrays.

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 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · 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
3K