excel是實驗數據,第一列:轉速rpm,第二列:應變值measurement,第三列:distance距離單位mm,第四列:torque扭力單位kgf-cm,第五列:板厚thickness 需要一個matlab的程序可以讓我用我做的實驗數據建立一個預測模型程序需要顯示出最後的公式他是可以準確預測的現在自變量是Rpm,distance,torque,thickness 因變量是measurement 做出一個非線性回歸模型使用fitnlm函數擬合模型預測出應變值讓我可以輸入電批轉速,輸入鎖附距離,輸入電批扭力,輸入板子厚度準確的預測出應變值 以下是我現在程式 但現在預測值誤差很大 沒有辦法正負值誤差在5%~10% 我需要都與實驗數據的數值差不多 有沒有置專業人士可以幫忙我修改程式並與我需求符合 可以跟我聯絡 我真的需要各位專業人士的幫忙
clear all; % 清除所有變量
clc; % 清除命令行窗口
% 開啟檔案選擇對話框
[filename, pathname] = uigetfile({'*.xlsx'; '*.xls'}, '選擇要導入的 Excel 文件');
% 检查用户是否取消了選擇
if isequal(filename, 0) || isequal(pathname, 0)
disp('未選擇任何文件。');
else
% 獲取文件的完整路径
fullpath = fullfile(pathname, filename);
% 導入 Excel 文件中的数據
[num, txt, raw] = xlsread(fullpath);
% 數據導入成功提示
disp('成功導入 Excel 文件。');
end
% 读取数据
data = readtable('篩選統整實驗數據.xlsx');
rpm = data{:, 1};
measurement = data{:, 2};
distance = data{:, 3};
torque = data{:, 4};
thickness = data{:, 5};
% 定义非线性模型(需要根据具体数据调整)
% 例如:measurement = a + b*rpm + c*distance + d*torque + e*thickness + f*distance^2
nonlinearModel = @(b,x) b(1) + b(2)*x(:,1) + b(3)*x(:,2) + b(4)*x(:,3) + b(5)*x(:,4) + b(6)*x(:,2).^2;
% 初始猜测的参数值
initialGuess = [0, 0, 0, 0, 0, 0];
% 拟合非线性回归模型
mdl = fitnlm([rpm, distance, torque, thickness], measurement, nonlinearModel, initialGuess);
% 获取拟合的参数值
coefficients = mdl.Coefficients.Estimate;
% 显示拟合方程
fprintf('非线性回归方程:\n');
fprintf('Measurement = %.4f + %.4f*rpm + %.4f*distance + %.4f*torque + %.4f*thickness + %.4f*distance^2\n', ...
coefficients(1), coefficients(2), coefficients(3), coefficients(4), coefficients(5), coefficients(6));
% 获取用户输入的螺丝头大小并调整距离
prompt = {'輸入資料庫實驗鎖附螺絲頭大小='};
dlgtitle = 'Input';
dims = [1 35];
definput = {'0'};
answer = inputdlg(prompt, dlgtitle, dims, definput);
new_test_screw_size = str2double(answer{1});
% 获取用户输入的电批转速
prompt = {'輸入電批轉速='};
dlgtitle = 'Input';
dims = [1 35];
definput = {'0'};
answer = inputdlg(prompt, dlgtitle, dims, definput);
new_rpm = str2double(answer{1});
% 获取用户输入的其他参数
prompt_dist = {'輸入鎖附距離:'};
prompt_torque = {'輸入電批扭力:'};
prompt_screw = {'輸入螺絲頭大小:'};
prompt_thickness = {'輸入板子厚度:'};
dist_title = 'New Distance Input';
torque_title = 'New Torque Input';
screw_title = 'New Screw Input';
thickness_title = 'New Thickness Input';
dist_answer = inputdlg(prompt_dist, dist_title);
torque_answer = inputdlg(prompt_torque, torque_title);
screw_answer = inputdlg(prompt_screw, screw_title);
thickness_answer = inputdlg(prompt_thickness, thickness_title);
new_distance = str2double(dist_answer{1});
new_torque = str2double(torque_answer{1});
screw_size = str2double(screw_answer{1});
new_thickness = str2double(thickness_answer{1});
new_distance = new_distance - 0.5 * screw_size;
% 使用非线性回归模型进行预测
new_X = [new_rpm, new_distance, new_torque, new_thickness];
new_measurement = nonlinearModel(coefficients, new_X); % 非线性回归模型的预测
disp('預測的測量值:');
disp(new_measurement);
% 绘制预测曲线
rpm_range = linspace(min(rpm), max(rpm), 100);
distance_range = linspace(min(distance), max(distance), 100);
torque_range = linspace(min(torque), max(torque), 100);
thickness_range = linspace(min(thickness), max(thickness), 100);
% 生成预测值
[mesh_rpm, mesh_distance] = meshgrid(rpm_range, distance_range);
mesh_measurement = nonlinearModel(coefficients, [mesh_rpm(:), mesh_distance(:), repmat(mean(torque), numel(mesh_rpm), 1), repmat(mean(thickness), numel(mesh_rpm), 1)]);
mesh_measurement = reshape(mesh_measurement, size(mesh_rpm));
% 绘制曲线图
figure;
surf(mesh_rpm, mesh_distance, mesh_measurement);
xlabel('RPM');
ylabel('Distance');
zlabel('Measurement');
title('非线性回归模型预测曲线');