function [CST_coefficients_upper, CST_coefficients_lower, mse_upper, mse_lower] = CST_parameterization(airfoil_file, N) % CST_parameterization - 实现翼型CST参数化,分别拟合上下表面 % 输入: % airfoil_file - 翼型数据文件名(包含路径) % N - CST参数化的阶数 % 输出: % CST_coefficients_upper - 上表面CST拟合系数 % CST_coefficients_lower - 下表面CST拟合系数 % mse_upper - 上表面拟合均方误差 % mse_lower - 下表面拟合均方误差
% 读取翼型数据,跳过第一行标题 airfoil_data = readmatrix(airfoil_file, 'NumHeaderLines', 1); % 跳过1行标题 x = airfoil_data(:, 1); % x坐标 y = airfoil_data(:, 2); % y坐标
% 找到前缘点(x最小的点) [~, leading_edge_index] = min(x);
% 将数据分为上表面和下表面 x_upper = x(1:leading_edge_index); % 上表面x坐标 y_upper = y(1:leading_edge_index); % 上表面y坐标 x_lower = x(leading_edge_index:end); % 下表面x坐标 y_lower = y(leading_edge_index:end); % 下表面y坐标
% 归一化x坐标 x_norm_upper = x_upper / max(x_upper); x_norm_lower = x_lower / max(x_lower);
% 定义类函数参数 N1 = 0.5; % 前缘形状参数 N2 = 1; % 后缘形状参数
% 构造类函数 C_upper = (x_norm_upper).^N1 .* (1 - x_norm_upper).^N2; C_lower = (x_norm_lower).^N1 .* (1 - x_norm_lower).^N2;
% 构造形状函数基函数(Bernstein 多项式) A_upper = zeros(length(x_norm_upper), N+1); A_lower = zeros(length(x_norm_lower), N+1); for i = 0:N A_upper(:, i+1) = nchoosek(N, i) * (x_norm_upper).^i .* (1 - x_norm_upper).^(N - i); A_lower(:, i+1) = nchoosek(N, i) * (x_norm_lower).^i .* (1 - x_norm_lower).^(N - i); end
% 使用最小二乘法拟合CST系数 CST_coefficients_upper = (A_upper .* C_upper) \ y_upper; CST_coefficients_lower = (A_lower .* C_lower) \ y_lower;
% 计算拟合值 y_fit_upper = (A_upper .* C_upper) * CST_coefficients_upper; y_fit_lower = (A_lower .* C_lower) * CST_coefficients_lower;
% 计算均方误差 (MSE) mse_upper = mean((y_upper - y_fit_upper).^2); mse_lower = mean((y_lower - y_fit_lower).^2);
% 输出拟合系数和均方误差 disp('上表面CST拟合系数:'); disp(CST_coefficients_upper); disp('上表面拟合均方误差 (MSE):'); disp(mse_upper); disp('下表面CST拟合系数:'); disp(CST_coefficients_lower); disp('下表面拟合均方误差 (MSE):'); disp(mse_lower);
% 绘制拟合结果 figure; subplot(2, 1, 1); plot(x_upper, y_upper, 'b-', 'LineWidth', 2); hold on; plot(x_lower, y_lower, 'g-', 'LineWidth', 2); plot(x_upper, y_fit_upper, 'r--', 'LineWidth', 2); plot(x_lower, y_fit_lower, 'm--', 'LineWidth', 2); legend('上表面原始数据', '下表面原始数据', '上表面CST拟合', '下表面CST拟合'); xlabel('x'); ylabel('y'); title('CST参数化拟合结果'); grid on;
% 绘制误差随x的变化曲线 subplot(2, 1, 2); plot(x_upper, y_upper - y_fit_upper, 'b-', 'LineWidth', 2); hold on; plot(x_lower, y_lower - y_fit_lower, 'g-', 'LineWidth', 2); legend('上表面拟合误差', '下表面拟合误差'); xlabel('x'); ylabel('误差'); title('拟合误差随x的变化'); grid on;end