圆柱爆炸径向线绘图:数据线条混乱,meshgrid与scatteredInterpolant使用受阻
问题根源与解决方案
你的绘图混乱核心原因是:模拟数据p.dat是二维散乱点(X,Y,P),且X值未按顺序排列,直接用plot(xRusBJ, Pressure_Rus_BJ)时,Matlab会严格按照数据在数组中的原始顺序连接每个点,导致线条交叉混乱;而解析解radial.dat是一维有序的X-P数据,所以能画出光滑曲线。
下面给两种针对性解决方法:
方法一:提取Y=0剖面并排序X(适合对比一维剖面)
如果你的解析解是沿Y=0的径向结果,直接筛选Y接近0的点,再按X排序后绘图:
clear all ; close all; clc; referenceData = importdata('C:\Users\...\Radial.dat'); referenceData = referenceData.data; xRef = referenceData(:,1); pRef = referenceData(:,4); %% 处理模拟数据 BJRusP = importdata('C:\Users\...\p.dat'); BJRusP = BJRusP.data; xRusBJ = BJRusP(1:14093,1); yRusBJ = BJRusP(1:14093,2); Pressure_Rus_BJ = BJRusP(1:14093,3); % 1. 筛选Y接近0的点(容差可根据数据调整) tol = 1e-3; valid_idx = abs(yRusBJ) < tol; x_profile = xRusBJ(valid_idx); p_profile = Pressure_Rus_BJ(valid_idx); % 2. 按X值从小到大排序 [sorted_x, sort_order] = sort(x_profile); sorted_p = p_profile(sort_order); % 3. 绘图 figure(1); hold on plot(xRef,pRef, '-'); plot(sorted_x, sorted_p, '-') grid on xlabel('x') ylabel('Pressure ') legend('Analytical', 'Rusanov', 'location', 'northwest') title('Pressure ');
方法二:用scatteredInterpolant插值到解析解网格(适合精准对比)
如果需要让模拟数据和解析解的X点完全对应,用散乱点插值工具把二维数据映射到一维X网格上:
clear all ; close all; clc; referenceData = importdata('C:\Users\...\Radial.dat'); referenceData = referenceData.data; xRef = referenceData(:,1); pRef = referenceData(:,4); %% 处理模拟数据 BJRusP = importdata('C:\Users\...\p.dat'); BJRusP = BJRusP.data; xRusBJ = BJRusP(1:14093,1); yRusBJ = BJRusP(1:14093,2); Pressure_Rus_BJ = BJRusP(1:14093,3); % 1. 创建散乱点插值对象 interp_fun = scatteredInterpolant(xRusBJ, yRusBJ, Pressure_Rus_BJ); % 2. 在解析解的X位置、Y=0处插值得到压力值 p_interpolated = interp_fun(xRef, zeros(size(xRef))); % 3. 绘图 figure(1); hold on plot(xRef,pRef, '-'); plot(xRef, p_interpolated, '-') grid on xlabel('x') ylabel('Pressure ') legend('Analytical', 'Rusanov', 'location', 'northwest') title('Pressure ');
补充说明
- 你同事提到的
meshgrid一般用于将二维散乱点转成规则网格(比如画等高图),但这里你是和一维解析解对比,scatteredInterpolant更直接。 - 如果你的模拟是轴对称问题,也可以把(X,Y)转换成径向坐标
r = sqrt(X.^2 + Y.^2),再按r排序后和解析解的径向数据对比。
内容的提问来源于stack exchange,提问作者Xray25
相关产品推荐
相关产品推荐

