fit line to 3D scatterplot

54 visualizaciones (últimos 30 días)
Emma Jacobs
Emma Jacobs el 20 de Mayo de 2023
Editada: Askic V el 21 de Mayo de 2023
I'm trying to add a linear fit to my 3d scatterplot, is there an easy function that let's me do this? Thanks!
Here's my code:
sessions = [12
12
12
12
12
12
12
12
21
21
21
21
12
12
12
12];
VASdelta = [50.5
50.5
50.5
50.5
9
9
9
9
50
50
50
50
40
40
40
40];
relPower = [NaN
13.39162848
-5.151906173
22.74039589
-6.608297044
-11.53044932
-8.162916339
-9.332208193
9.440958586
44.6072671
NaN
NaN
-2.527864912
9.2589486
-11.9690337
-27.36570581];
personC = [[0.9 0.1 0];
[0.9 0.1 0];
[0.9 0.1 0];
[0.9 0.1 0];
[0 0.5 0];
[0 0.5 0];
[0 0.5 0];
[0 0.5 0];
[0 0 1];
[0 0 1];
[0 0 1];
[0 0 1];
[0 0 0];
[0 0 0];
[0 0 0];
[0 0 0];
];
scatter3(VASdelta,relPower,sessions, 50, personC, 'filled');
xlabel("Change in VAS scores")
ylabel("Change in relative delta band power (%)")
zlabel("Number of sessions")
scatter(VASdelta,relPower,(sessions/21)*50, personC, 'filled');
xlabel("Change in VAS scores")
ylabel("Change in relative delta band power (%)")

Respuestas (1)

Askic V
Askic V el 21 de Mayo de 2023
Editada: Askic V el 21 de Mayo de 2023
I'm not really sure what you want to achieve here, but if you have X and Y data and want to do a linear fit, then you can construct a line, but if you have a 3D data, then you can construct a plane to be a linear fit in case
Z = function (X,Y).
This is a small example how to construct plane as a result of linear square fit method:
clear; clc; close all
% Input random data
X = randi(10,1,10); % Replace x1, x2, ..., x10 with the actual x values
Y = randi(10,1,10); % Replace y1, y2, ..., y10 with the actual y values
Z = randi(5,1,10); % Replace z1, z2, ..., z10 with the actual z values
% Perform least squares fit
A = [X(:), Y(:), ones(10, 1)];
coefficients = lsqlin([X(:), Y(:), ones(10, 1)],Z(:))
coefficients = 3×1
0.0351 0.2803 0.6492
% Extract the calculated parameters
a = coefficients(1); b = coefficients(2); c = coefficients(3);
% Generate points as a mesh grid
x_data = linspace(min(X), max(X), 100);
y_data = linspace(min(Y), max(Y), 100);
[X_data, Y_data] = meshgrid(x_data, y_data);
Z_line = a*X_data + b*Y_data + c;
% Plot the data points and the line
scatter3(X, Y, Z, 50, 'filled', 'MarkerFaceColor', 'b');
hold on;
mesh(X_data, Y_data, Z_line, 'EdgeColor', 'r');
xlabel('X'); ylabel('Y'); zlabel('Z'); zlim([0 10]);
title('3D Linear Fit');
grid on;
hold off;

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by