Clear Filters
Clear Filters

Error using scatteredInterpolant The input points must be specified in column-vector format.

21 views (last 30 days)
This is my code .I am trying to plot a multivariate equation containing 3 independent variable and 1 dependent variable.Getting this error.
X1= double(data.time);% Assuming time, temperature, humidity are columns 3, 4, and 5
X2=double(data.temperature);
X3=double(data.humidity);
Y = double(data.soil_moisture); % Assuming soil moisture is in column 6 -it is the dependent variable
% Define your multivariable equation as an anonymous function
f = @(X1, X2, X3) 5.5899 -0.0142 * X1 -0.0741 * X2 -0.0347 * X3;
% Create a grid of values for x, y, and t
X1V = linspace(min(X1), max(X1), 100); % Adjust the range and resolution as needed
X2V = linspace(min(X2), max(X2), 100);
X3V = linspace(min(X3), max(X3), 100);
[X, Y,T] = ndgrid(X1V, X2V,X3V);
F = scatteredInterpolant(X1V, X2V, X3V);
Z = F(X, Y, T);
% Create a 3D surface plot
figure;
surf(X1, X2, X3, Z);
xlabel('X');
ylabel('Y');
zlabel('T');
title('3D Surface Plot of z = f(x, y, t)');
Error getting is
Error using scatteredInterpolant
The input points must be specified in column-vector format.
Error in Project3 (line 44)
F = scatteredInterpolant(X1V, X2V, X3V);

Answers (1)

Star Strider
Star Strider on 23 Oct 2023
I do not have your data so I am not certain exactly what the problem is.
A relatively easy way to create column vectors is:
F = scatteredInterpolant(X1V(:), X2V(:), X3V(:));
The ‘(:)’ operator subscript convention forces them to become column vectors regardless of their original dimensions, whether vectors or matrices.
  4 Comments
Chhanda
Chhanda on 23 Oct 2023
Its still not working. So let me share some more details. I gave u one part of the code.You can see the equation that i have mentioned.This i have calculated using multivariate linear regression.Then i m trying to plot the equation.At first i have read the data from an excell sheet(.xlsx) file. Then have segregated each data in form of double
This is the X1.X2 and X3 is of similar format.Y (dependent variable) is also of same format.
I am pasting the whole code here including the linear regression part.Hope it helps to recognise the error.
data=readtable('MainData.xlsx');
timeColumn = double(data.time);
X1= double(data.time);% Assuming time, temperature, humidity are columns 3, 4, and 5
X2=double(data.temperature);
X3=double(data.humidity);
% Convert variables to tables
% Extract the dependent variable (soil moisture)
Y = double(data.soil_moisture); % Assuming soil moisture is in column 6
n=height(X1);
onesTable = ones(n,1);
a = [onesTable, X1, X2, X3];
% Perform multiple linear regression
coefficients = a\ Y;
% Display the coefficients
fprintf('Coefficients:\n');
fprintf('Intercept: %.4f\n', coefficients(1));
fprintf('Time: %.4f\n', coefficients(2));
fprintf('Temperature: %.4f\n', coefficients(3));
fprintf('Humidity: %.4f\n', coefficients(4));
% Create the regression equation
intercept = coefficients(1);
time_coeff = coefficients(2);
temp_coeff = coefficients(3);
humidity_coeff = coefficients(4);
equation = sprintf('Soil Moisture = %.4f + %.4f * Time + %.4f * Temperature + %.4f * Humidity', intercept, time_coeff, temp_coeff, humidity_coeff);
fprintf('\nRegression Equation:\n%s\n', equation);
% Define your multivariable equation as an anonymous function
f = @(X1, X2, X3) 5.5899 -0.0142 * X1 -0.0741 * X2 -0.0347 * X3;
% Create a grid of values for x, y, and t
X1V = linspace(min(X1), max(X1), 100); % Adjust the range and resolution as needed
X2V = linspace(min(X2), max(X2), 100);
X3V = linspace(min(X3), max(X3), 100);
[X, Y,T] = ndgrid(X1V, X2V,X3V);
F = scatteredInterpolant(X1V, X2V, X3V);
Z = F(X, Y, T);
% Create a 3D surface plot
figure;
surf(X1, X2, X3, Z);
xlabel('X');
ylabel('Y');
zlabel('T');
title('3D Surface Plot of z = f(x, y, t)');
Star Strider
Star Strider on 23 Oct 2023
Provide MainData.xlsx. Stopping here until I have access to it, since I cannot proceed without it. Use the ‘paperclip’ icon to upload it.
You can plot three dimmensions in MATLAB, not four, which is what you appear to want to do. To plot a fourth dimension (such as time), you will need to stack the 3D plots or plot separate 3D plots, with each ‘layer’ (or colour or axes, depending on how you want to approach this) being a different time instant, depending on how you want to plot the data.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!