Fitting method with multiple response variables (y1, y2, y3).
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Torsten
2023년 3월 17일
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)) ?
답변 (1개)
Torsten
2023년 3월 17일
편집: Torsten
2023년 3월 17일
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
sol2 = A\Y2
sol3 = A\Y3
%or
sol = A\[Y1,Y2,Y3]
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];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!