Effacer les filtres
Effacer les filtres

Fitting method with multiple response variables (y1, y2, y3).

6 vues (au cours des 30 derniers jours)
peter huang
peter huang le 17 Mar 2023
Modifié(e) : Torsten le 17 Mar 2023
Assuming I have the following data representing water level trends at different stations in this area:
X = linspace(0, 10, 100)';
Y1 = 2X.^2 - 3X + randn(size(X))*0.5;
Y2 = 2.5X.^2 - 3X + randn(size(X))*0.5;
Y3 = 4.1X.^2 - 3X + randn(size(X))*0.5;
These data are self-generated, and I want to create a fitting line or regression line in Matlab using the fitlm command to represent these three sets of data. Chatgpt has suggested the following code to solve my problem:
Y1 = 2X.^2 - 3X + randn(size(X))*0.5;
Y2 = 2.5X.^2 - 3X + randn(size(X))*0.5;
Y3 = 4.1X.^2 - 3X + randn(size(X))*0.5;
% Create tables of the sea level data for each region
data1 = table(X, Y1, 'VariableNames', {'X', 'Y'});
data2 = table(X, Y2, 'VariableNames', {'X', 'Y'});
data3 = table(X, Y3, 'VariableNames', {'X', 'Y'});
% Merge the data for all regions merged_data = [data1; data2; data3];
% Fit a linear trend to the data model = fitlm(merged_data, 'Y~ X');
% Plot the results plot(model);hold on % plot(X ,mean_Y) legend({'Region 1', 'Region 2', 'Region 3', 'Overall Trend'}, 'Location', 'Northwest');
However, I am not sure about the instruction "merged_data = [data1; data2; data3];" in the code. Can fitlm fit the data in this way?
Also, what does "YX" in "model = fitlm(merged_data, 'Y X');" mean?
  2 commentaires
Torsten
Torsten le 17 Mar 2023
You want to fit 6 parameters (two for each data set) or only 4 (one parameter for each X.^2 (= 3) and the same parameter for the X (= 1)) ?
peter huang
peter huang le 17 Mar 2023
I want to fit a fitted line representing these three trends (y1 y2 y3)

Connectez-vous pour commenter.

Réponses (1)

Torsten
Torsten le 17 Mar 2023
Modifié(e) : Torsten le 17 Mar 2023
rng("default")
X = linspace(0, 10, 100)';
Y1 = 2*X.^2 - 3*X + randn(size(X))*0.5;
Y2 = 2.5*X.^2 - 3*X + randn(size(X))*0.5;
Y3 = 4.1*X.^2 - 3*X + randn(size(X))*0.5;
A = [X.^2 X];
sol1 = A\Y1
sol1 = 2×1
1.9942 -2.9563
sol2 = A\Y2
sol2 = 2×1
2.5075 -3.0601
sol3 = A\Y3
sol3 = 2×1
4.0979 -2.9944
%or
sol = A\[Y1,Y2,Y3]
sol = 2×3
1.9942 2.5075 4.0979 -2.9563 -3.0601 -2.9944
If you really want to fit a linear function to the quadratic data, use
A = [X ones(size(X))];
instead of
A = [X.^2 X];

Catégories

En savoir plus sur Fit Postprocessing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by