You are on page 1of 6

%-------------------------------------------------------------------------

% 學 號 :1112846
% 姓 名 :張榮楷
% 完成日期 :2023/12/24
% 檔 名 :D2846CAEexc13.docx
%------------------------------------------------------------------------
6-1(1)
%-------------------------------------------------------------------------
程式功能: 請自行上網 download 一資料檔”Sugar_RG.txt”,檔案內有
三欄的資料分別為 125 顆蕃茄的糖度(s)、紅色值(r)與綠色
值(g),
1) 請利用此資料建立迴歸模式 s= a0 + a1 r + a2 g,以建立蕃
茄與表皮顏色的關係
2) 並將求得之迴歸模式繪出其 3D 平面圖,並將原始資料
點(以圓形符號標示)同時繪於圖軸上(sz 軸、rx 軸
、gy 軸) , 其中 x 軸, y 軸的分佈範圍取自該資料檔中
的 r 與 g 值中之最小值及最大值。
• 於圖形的 x、y、z 軸標示每一個的意義,同時計算模型的 Rsquare,並將它標
示在在圖形的標題顯示 R-square
%-------------------------------------------------------------------------
data = load('SugarRG.txt');
s = data(:, 1);
r = data(:, 2);
g = data(:, 3);
s = a0 + a1*r + a2*g
X = [ones(size(r)), r, g];
coefficients = X\s;
a0 = coefficients(1);
a1 = coefficients(2);
a2 = coefficients(3);
y_predicted = a0 + a1*r + a2*g;
SS_total = sum((s - mean(s)).^2);
SS_residual = sum((s - y_predicted).^2);
R_square = 1 - (SS_residual / SS_total);
figure;
scatter3(r, g, s, 'o'); % 原始資料點
hold on;
[X_surface, Y_surface] = meshgrid(min(r):0.1:max(r), min(g):0.1:max(g));
Z_surface = a0 + a1*X_surface + a2*Y_surface;
surf(X_surface, Y_surface, Z_surface, 'FaceAlpha', 0.5);
xlabel('紅色值 (r)');
ylabel('綠色值 (g)');
zlabel('糖度 (s)');
title(['迴歸模型: s = ' num2str(a0) ' + ' num2str(a1) 'r + ' num2str(a2) 'g, R-square = '
num2str(R_square)]);
legend('原始資料', '迴歸平面');
hold off;
[Run]

6-2(2)
%-------------------------------------------------------------------------
程式功能:下表是台灣 72~82 年的進口值(z),消費(x)及國內生產毛額(y)(單位:兆):
進口值 0.760 0.865 0.833 0.999 1.269 1.521 1.690 1.792 2.062 2.313 2.504 消
費 1.268 1.394 1.482 1.595 1.773 2.011 2.274 2.456 2.635 2.869 3.104 國民生產
毛額 2.489 2.752 2.889 3.225 3.636 3.921 4.244 4.473 4.811 5.136 5.460 1) 請將
上表資料建檔,然後以開檔方式將資料讀至程式之中 2) 若進口值與消費、國內生
產毛額之間的關係模式為 z = a0+a1x+a2y , 請利用表 列資料進行迴歸,求出模
式的係數 3) 若關係模式改為 z = (a0+a1*x^2-a2y)/(a3*x^2+a4*y^2) , 求出模式
的係數,係數的初始設定值均為 1 4) 在同一圖軸上,同時繪出上表之資料的
3D 分佈圖(請用圖形記號標示)及(3) 模式的曲面圖。而且其中 x-y 的範圍分別
取自該資料檔中的 x 與 y 值中之最小 值及最大值 • 於圖形的 x、y、z 軸標示每
一個的意義,同時計算模型的 R-square,並將它標示在在圖形的標題顯示 R-
square
%-------------------------------------------------------------------------
data = load('Taiwan_data.txt');
z = data(:, 1);
x = data(:, 2);
y = data(:, 3);
X = [ones(size(x)), x, y];
coefficients_linear = X\z; % \ 為矩陣左除,求解線性方程組
disp('線性迴歸模型的係數:');
disp(coefficients_linear);
z_predicted_linear = X * coefficients_linear;
SS_total_linear = sum((z - mean(z)).^2);
SS_residual_linear = sum((z - z_predicted_linear).^2);
R_square_linear = 1 - (SS_residual_linear / SS_total_linear);
disp(['線性迴歸模型的 R-square: ' num2str(R_square_linear)]);
z = (a0+a1*x^2-a2*y)/(a3*x^2+a4*y^2)
X_new = [ones(size(x)), x.^2, -y, x.^2, y.^2];
coefficients_new = X_new\z;
disp('新模型的係數:');
disp(coefficients_new);
z_predicted_new = X_new * coefficients_new;
SS_total_new = sum((z - mean(z)).^2);
SS_residual_new = sum((z - z_predicted_new).^2);
R_square_new = 1 - (SS_residual_new / SS_total_new);
disp(['新模型的 R-square: ' num2str(R_square_new)]);
figure;
scatter3(x, y, z, 'filled', 'Marker', 'd');
hold on;
[X_surface, Y_surface] = meshgrid(min(x):0.1:max(x), min(y):0.1:max(y));
Z_surface = (coefficients_new(1) + coefficients_new(2) * X_surface.^2 -
coefficients_new(3) * Y_surface) ./ (coefficients_new(4) * X_surface.^2 +
coefficients_new(5) * Y_surface.^2);
surf(X_surface, Y_surface, Z_surface, 'FaceAlpha', 0.5);
xlabel('消費');
ylabel('國內生產毛額');
zlabel('進口值');
title(['資料的 3D 分佈與新模型的曲面圖, R-square = ' num2str(R_square_new)]);
legend('資料點', '新模型曲面');
hold off;
[Run]
線性迴歸模型的係數:
-0.6372
0.6596
0.1984
線性迴歸模型的 R-square: 0.99302

新模型的係數:
0.1022
-0.0259
-0.0704
0
0.0773
新模型的 R-square: 0.99495
6-3(3)
%-------------------------------------------------------------------------
程式功能:請讀取 reaction.txt 資料檔,檔中的內 容如右表 (不含欄位名稱),為
三種化 學反應物(氫、n-戊烷、同位戊烷)的分 壓(partial pressure)和相應的反應
速 率,若反應速率的預測符合下列迴歸 模型,請求得該模型的係數,並計算
模型的 R-square 。(係數的初始設定值 均為 1)Hougen-Watson 的催化反應動力
學模型
%-------------------------------------------------------------------------
data = dlmread('reaction.txt');
partial_pressure = data(:, 1:3);
reaction_rate = data(:, 4);
X = [ones(size(partial_pressure, 1), 1), log(partial_pressure)];
coefficients_hw = X\log(reaction_rate);
reaction_rate_predicted = exp(X * coefficients_hw);
SS_total_hw = sum((reaction_rate - mean(reaction_rate)).^2);
SS_residual_hw = sum((reaction_rate - reaction_rate_predicted).^2);
R_square_hw = 1 - (SS_residual_hw / SS_total_hw);
disp('Hougen-Watson 催化反應動力學模型的係數:');
disp(coefficients_hw);
disp(['模型的 R-square: ' num2str(R_square_hw)]);
[Run]
Hougen-Watson 催化反應動力學模型的係數:
0.8235
-0.5031
-0.4333
1.1187
模型的 R-square: 0.14378

You might also like