Hi everyone,
I have to computed ten parameters in non linear equation, I tryied to use 'nlinfit' command but I obtain the following error:
Error using nlinfit (line 219)MODELFUN must be a function that returns a vector of fitted values the same size as Y (30615-by-1). Themodel function you provided returned a result that was 1-by-1.One common reason for a size mismatch is using matrix operators (*, /, ^) in your function instead of thecorresponding elementwise operators (.*, ./, .^).Error in C_Fourier_Analysis (line 58)F_fitted = nlinfit(x,y,f,[1 1 2*pi*rand 1 2*pi*rand 1 1 2*pi*rand 1 2*pi*rand]);
I'm attacching the complete code and the data. Thanks.
format long gfolderData = 'C:\Users\Valerio\Desktop\TRAINEESHIP\data\ACCESS1.0';filePattern = fullfile(folderData, '*.xlsx');xlsFiles = dir(filePattern);nFiles = length(xlsFiles);for ii = 1:nFiles filename = fullfile(xlsFiles(ii).folder, xlsFiles(ii).name); files{ii} = xlsread(filename);endIPCC = files(1);ERA5 = files(2);IPCC_data = unique(IPCC{:,1},'rows');ERA5_data = unique(ERA5{:,1},'rows');dt_IPCC = datetime([IPCC_data(:,1:3) IPCC_data(:,4)/1E4 repmat([0 0],size(IPCC_data,1),1)]);dt_ERA5 = datetime([ERA5_data(:,1:4) repmat([0 0],size(ERA5_data,1),1)]);[~,ia,ie] = intersect(dt_IPCC,dt_ERA5);tt_IPCC_ERA5 = timetable(dt_IPCC(ia),IPCC_data(ia,5:end),ERA5_data(ie,5:end));tt_IPCC_ERA5.Properties.VariableNames = {'IPCC','ERA5'};IPCC = tt_IPCC_ERA5.IPCC;ERA5 = tt_IPCC_ERA5.ERA5;y = ERA5(:,1); %Hs from ERA5
dir_ERA5 = ERA5(:,3);x1 = IPCC(:,1); %Hs from IPCC
x2 = IPCC(:,3); %Dir from IPCC
Tm_IPCC = IPCC(:,2);Tm_ERA5 = ERA5(:,2);figurescatter(dir_ERA5,x2,'g');xlabel('Dir ERA5');ylabel('Dir IPCC');title('Linear regression between directions');%Plot regression linear line
dir_ERA5(isnan(x2)) = [] ;x2(isnan(x2)) = [] ;P = polyfit(dir_ERA5,x2,1);x0 = min(dir_ERA5) ; x3 = max(dir_ERA5) ;xi = linspace(x0,x3) ;yi = P(1)*xi+P(2);hold onplot(xi,yi,'b');%Computing linear regression parameters
lin_param = fitlm(dir_ERA5,x2);Pearson = lin_param.Rsquared;Error_R = lin_param.RMSE;%Fourier Analysis
x = [x1', x2'];f = @(F,x)(F(1)+F(2)*cos(2*pi*(x(:,2)./360)-F(3))+F(4)*cos(4*pi*(x(:,2)./360)-F(5))).*x(:,1).^(F(6)+F(7)*cos(2*pi*(x(:,2)./360)-F(8))+F(9)*cos(4*pi*(x(:,2)./360)-F(10)));F_fitted = nlinfit(x,y,f,[1 1 2*pi*rand 1 2*pi*rand 1 1 2*pi*rand 1 2*pi*rand]);
Best Answer