n5321 | 2025年3月30日 19:51

Tags: Matlab


clear
clc
%% Pseudocode
% A1.输入实际测功曲线数据,存为矩阵。
% A2.对测功曲线拟合,获得表达式
% A3.设定rmse值表达式,拟合曲线对比测试曲线
% 导入测功数据为基准_自excel读取表数据,存储在矩阵中
[~, ~, raw] = xlsread('D:\experiment\Demos\80W.xls','test','A2:H263');
    test_curve = reshape([raw{:}],size(raw));
    clearvars raw;
        test_curve_size=size(test_curve);
%%  拟合测功曲线,并且计算root mean square.
% 电流曲线用三阶多项式曲线拟合,获得表达式
fitting_current=polyfit(test_curve(:,4),test_curve(:,1),3);
    fit_I=polyval(fitting_current,test_curve(:,4));
    rms_Current=sqrt(mean(test_curve(:,1)-fit_I).^2);
        str_current=strcat('Rmse of Current=',num2str(rms_Current));
%  扭力曲线用四阶多项式曲线拟合,获得表达式
fitting_torque=polyfit(test_curve(:,4),test_curve(:,6),4);
    fit_T=polyval(fitting_torque,test_curve(:,4));
    rms_Torque=sqrt(mean(test_curve(:,6)-fit_T).^2);
        str_torque=strcat('Rmse of Torque=',num2str(rms_Torque));
%  输出功率用扭力与转速的乘积获得
Outputpower=fit_T.*test_curve(:,4)./9.54929659;
        rms_Outputpower=sqrt(mean(test_curve(:,5)-Outputpower).^2);
        str_outputpower=strcat('Rmse of Power=',num2str(rms_Outputpower));
%  效率曲线,计算输出功率除以输入功率(设考虑功率因数为0.999997)。
Input_power=220.*fit_I;
        Eff_calculated=(Outputpower./Input_power).*100.*0.999997;
        rms_Eff=sqrt(mean(test_curve(:,7)-Eff_calculated).^2);
            str_Eff=strcat('Rmse of EFF=',num2str(rms_Eff));
clearvars    rms_Outputpower  rms_Current rms_Outputpower rms_Torque rms_Current  rms_Eff ...
           %fit_current  fit_torque  test_curve
%%   Import data from calculated results。
% B1.导入计算数据
% B2.创建电流,扭力,输出功率,效率等的空矩阵,
% B3.将导入数据整理存储至相应矩阵之中
    % 设置文件夹路径
    p1='D:/experiment/004_Data/';
    %  选取所有文件
    File=dir(fullfile(p1,'*.csv'));
    % 获得文件名列表
    Fn={File.name};
    FNames = {File.name}';
    %获取文件名的列表,
    Length_Names = size(FNames,1);                      %获取文件数量
    result_1=zeros(Length_Names,1);                     %创建一个对应文件数量个数的矢量用来存结果
    calculated_InputCurrent=zeros(151,Length_Names);    %存储电流的空矩阵。
    calculated_Efficiency=zeros(151,Length_Names);      %存储效率的空矩阵。
    calculated_OutputPower=zeros(151,Length_Names);     %存储输出功率的空矩阵。
    calculated_OutputTorque=zeros(151,Length_Names);    %存储扭力的空矩阵。
    rmse_check=zeros(Length_Names,4);                   %存储Rmse值的空矩阵。       
    
%  B3.将所有计算数据整理入相应矩阵中
for k=1:Length_Names;
    path=strcat(p1,char(Fn(k)));
    data = fopen(path,'r');
    delimiter = ',';
    startRow = 1;
    formatSpec = '%f%f%f%f%f%[^\n\r]';
    dataArray = textscan(data, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow, 'ReturnOnError', false);
    calculated_InputCurrent(:,k)=dataArray{:,2};
    calculated_Efficiency(:,k)=dataArray{:,3};
    calculated_OutputPower(:,k)=dataArray{:,4};
    calculated_OutputTorque(:,k)=dataArray{:,5};      
end
    calculated_Speed=dataArray{:,1};                %数据矢量,以作为X轴
    clearvars raw
%数据导入完成。

%% 误差计算
% C1.对所有的电流,扭力,输出功率,效率等数据计算Rmse,
% C2.将Rmse数据存储入rems_check之中
        res=0;                                         %自动筛选用计步器                
        gt={ };                                         %文件名存储器
for k=1:Length_Names;         
        base_Current=polyval(fitting_current,calculated_Speed);
        rms_Current_verification=sqrt(mean(calculated_InputCurrent(:,k)-base_Current).^2);
        
        base_Torq=polyval(fitting_torque,calculated_Speed);        
        rms_Torque_verification=sqrt(mean(calculated_OutputTorque(:,k)-base_Torq).^2);
        
        base_Outputpower=base_Torq.*calculated_Speed./9.54929659;  
        rms_Outputpower_verification=sqrt(mean(calculated_OutputPower(:,k)-base_Outputpower).^2);       
        
        base_Input_power=220.*base_Current;       
        base_Eff=(base_Outputpower./base_Input_power).*100.*0.997;
        rms_Eff_rms_verification=sqrt(mean(calculated_Efficiency(:,k)-base_Eff).^2);  
        
        rmse_check(k,:)=[rms_Current_verification,rms_Torque_verification,rms_Outputpower_verification, rms_Eff_rms_verification];        
        if   rms_Current_verification<10&& rms_Eff_rms_verification<25.24&&Outputpower_rms<10&&rms_Torque<10;
            res=res+1;            
            gt(k)=Fn(k);            
        end        
end
xlswrite('Rmse_result',rmse_check)  %应该要存得更漂亮一点   
xlswrite('Rmse_result',FNames,1,'E')  %应该要存得更漂亮一点   
clear   rms_Current_verification  ...
        rms_Torque_verification ...
        rms_Outputpower_verification ...
        rms_Eff_rms_verification
%% 2D空间查看所有方案电流的RMSE值及电流对比图
%Rmse值
figure('Position',[50 100 1800 900],'Color',[1 1 1]);         
    hold on
%     ax(2) = axes('XLim',[0  Length_Names],'LineWidth',0.7,...
%         'YLim',[0  0.3],'XTick', [0:5:Length_Names],'YTick', [linspace(0,0.3,11)]);
    subplot(1,2,1)
    plot([1:Length_Names],rmse_check(:,1),'o');         
        xlabel('CASES');
        title('RMSE of Current');
    grid
    subplot(1,2,2)
    plot([1:Length_Names],rmse_check(:,2),'o'); 
        xlabel('CASES');
        title('RMSE of Torque');
    grid    
    
    
%% 电流&扭力曲线
hF =figure('Position',[50 100 1200 900],'Color',[1 1 1]);            
        set(hF,'name','Color Wheel');
        le=gobjects(Length_Names);
        val = 1;
        sat = 1;
        h = linspace(0,1-1/Length_Names,Length_Names);
        s = sat*ones(1,Length_Names);
        v = val*ones(1,Length_Names);
        colors = hsv2rgb([h;s;v]');              
        %定X轴
        ax(1) = axes('XLim',[min(test_curve(:,4)) 1500],...
        'YLim',[0 1.5], 'LineWidth',0.7, 'XTick' , [linspace(0,1500,16)],...
        'YTick', [linspace(0,1.5,16)]); 
        hold on
        xlabel('Speed(rpm)');
        title('Test Performance VS Caculated Performance @ Speed');
        plot(test_curve(:,4),test_curve(:,1))
        grid
        hold on      
    for  m=1:Length_Names;
      plot(calculated_Speed,calculated_InputCurrent(:,m),'LineWidth',1,'color',colors(m,:));
      hold on  
    end       
      hold off
      
% 扭力曲线      
      tF =figure('Position',[50 100 1200 900],'Color',[1 1 1]);            
        set(tF,'name','Color Wheel');
        le=gobjects(Length_Names);
        val = 1;
        sat = 1;
        h = linspace(0,1-1/Length_Names,Length_Names);
        s = sat*ones(1,Length_Names);
        v = val*ones(1,Length_Names);
        colors = hsv2rgb([h;s;v]');              
        %定X轴
        ax(1) = axes('XLim',[min(test_curve(:,4)) 1500],...
        'YLim',[0 1.5], 'LineWidth',0.7, 'XTick' , [linspace(0,1500,16)],...
        'YTick', [linspace(0,1.5,16)]); 
        hold on
        xlabel('Speed(rpm)');
        title('Test Performance VS Caculated Performance @ Speed');
        plot(test_curve(:,4),test_curve(:,6))
        grid
        hold on      
    for  m=1:Length_Names;
      plot(calculated_Speed,calculated_OutputTorque(:,m),'LineWidth',1,'color',colors(m,:));
      hold on  
    end       
      hold off