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

• MATLAB
• McLaren Rulez
In summary, the person is looking for a function that MATLAB can interpolate between points in an array to give a desired output.
McLaren Rulez
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 :)

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?

## 1. How do I interpolate an array of data in MATLAB?

To interpolate an array of data in MATLAB, you can use the built-in function interp1. This function takes in the original array of data and the desired number of interpolation points and returns a new array with the interpolated values.

## 2. What is the purpose of interpolating an array of data?

The purpose of interpolating an array of data is to estimate values between the existing data points. This is useful when you have data points that are not evenly spaced, and you want to create a smooth function that can be used to make predictions or fill in missing data.

## 3. Can I choose the type of interpolation method to use in MATLAB?

Yes, the interp1 function in MATLAB allows you to specify the type of interpolation method to use, such as linear, nearest neighbor, spline, or cubic. This allows you to choose the method that best fits your data and the desired level of accuracy.

## 4. Can I visualize the interpolated function in MATLAB?

Yes, you can use the plot function in MATLAB to visualize the interpolated function. This will plot the original data points as well as the interpolated points, giving you a visual representation of the smooth function created from the interpolation.

## 5. Is it possible to interpolate data in higher dimensions in MATLAB?

Yes, MATLAB also has functions for interpolating data in higher dimensions, such as interp2 for 2D data and interp3 for 3D data. These functions work in a similar way to interp1 and allow for interpolation in multiple dimensions.

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