how to fit non linear equation with ten parameters

2 views (last 30 days)
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). The
model 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 the
corresponding 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 g
folderData = '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);
end
IPCC = 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);
figure
scatter(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 on
plot(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]);

Accepted Answer

Ameer Hamza
Ameer Hamza on 25 Mar 2020
This error is caused because vector created by this line is
x = [x1', x2'];
1*x, however, it seems that you expected it to be x*2 matrix. Changing the line to
x = [x1, x2];
will solve this issue. However, after this, there is another issue related to your function f returning inf and nan. To resolve that, you will need to check the issue with your equation.
  3 Comments
Ameer Hamza
Ameer Hamza on 25 Mar 2020
It has to do with the complexity of your model. MATLAB is having difficulity to find a fit to your dataset. I tried few things but couldn't able to solve this issue. Trying to simplify the model will be helpful.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!