# How do I perform state linear interpolation?

376 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (1)

Vidhi Agarwal
on 9 Jun 2023

To perform state linear interpolation in MATLAB, you can use the interp1 function. This function performs linear interpolation between points in a table based on the input data.

We first define the input state X as a 6-element vector. We then load the data table from a file or generate it in MATLAB as a variable dataTable. The input and output states are extracted from the table and stored in variables inputStates and outputStates, respectively.

Finally, the interp1 function is called to compute the best estimate of the output state Y based on linear interpolation between the points in the table. The function takes the input states, output states, and the desired input state X as arguments, along with the interpolation method (in this case, 'linear'). The resulting output state Y is a vector with 8 elements corresponding to the corresponding output state for the given input state.

% Define input state

X = [a, b, c, d, e, f];

% Load data table

dataTable = load('data.mat'); % Load data from file or generate the table

% Extract input and output states from the table

inputStates = dataTable(:, 1:6);

outputStates = dataTable(:, 7:14);

% Compute best estimate of output state using linear interpolation

Y = interp1(inputStates, outputStates, X, 'linear');

##### 3 Comments

Vidhi Agarwal
on 12 Jun 2023

The error you're encountering occurs when the input vector X in the interp1 function is not a vector, but instead is an invalid input. The X vector argument in the interp1 function must be a one-dimensional vector.

Looking at the example code you provided, it seems that the input vector inputStates is a 2D matrix. To use interp1 with a 2D matrix inputStates as input, you need to transpose the matrix to ensure that the interpolation is performed along the correct dimension.

inputStates = [1 2 3; 4 5 6; 7 8 10]; % Must be transposed

outputStates = [10 20 30 40 50 60 70 80; 20 30 40 50 60 70 80 90; 30 40 50 60 70 80 90 100];

% Define Desired State

Desired = [2.5 6.5 9];

% Perform Linear interpolation

Y = interp1(inputStates', outputStates', Desired, 'linear');

disp(Y);

In this example, before calling the interp1 function, we have transposed the input matrix inputStates using the apostrophe (') to ensure that inputStates is a mxn matrix, where m is the number of points on the x-axis and n is the length of X. This ensures that the interp1 interpolation is performed along the correct dimension.

Transposing the input matrix does not change the values' order within the input matrix; rather, it swaps the rows and columns' positions.

Hence, by transposing the input matrix inputStates to inputStates', the values of the input matrix remain consistent with the original input matrix for handling the input values of the examples.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!