I have 744 hourly wind data points for the month of january i.e 24 data points for each day (744 for 31 days) For each day I need to fit the 24 hourly data points with the non linear function C(1)+C(2).​*sin(2*pi/​24.*(x-C(3​)))+C(4).*​sin(2*pi/1​2.*(x-

1 view (last 30 days)
I have 744 hourly wind data points for the month of january i.e 24 data points for each day (744 for 31 days) for 7 different heights(basically I have a wind data matrix of 744X 8 dimension). For each day and for a particular height I need to fit the 24 hourly data points with the non linear function C(1)+C(2).*sin(2*pi/24.*(x-C(3)))+C(4).*sin(2*pi/12.*(x-C(5)))+C(6).*sin(2*pi/8.*(x-C(7))). These 7 value of coefficients which I am getting after using fitnlm needs to be stored in a matrix, as I want to apply a for loop to find and store the coefficient value of each day and a particular height in a 3d matrix , so that I get everything in one place for for easy analysis. Please help me out in applying the for loop and storing the coefficients in best way possible. Thanks in advance.

Answers (2)

Ameer Hamza
Ameer Hamza on 13 Jun 2020
Edited: Ameer Hamza on 13 Jun 2020
You can do something like this
M = % 744x8 matrix
M_parts = mat2cell(M, 24*ones(31, 1), ones(1, 8)); % divide the matrix M for each day an height in a cell array
Coeffs = zeros(31, 8, 7);
for day=1:31
for height=1:8
data = Coeffs{day, height};
c = % apply fitlm
Coeffs(day, height, :) = c;
end
end
Coeffs is a 3D array with days along the first dimension (rows), heights along 2nd dimension (columns) and coefficients along the 3rd (pages).

David Goodmanson
David Goodmanson on 13 Jun 2020
Hi Gourav,
this is not really a nonlinear problem. That's because dropping some parenthesis)
C2*sin(2*pi/24*(x-C3)) = -C2*[ sin(2*pi/24*C3)*cos(2*pi/24*x) +cos(2*pi/24*C3)*sin(2*pi/24*x) ]
Sine and cosine are linearly independent functions, so if you can do a linear fit
data = D2*cos(2*pi/24*x) + D3*sin(2*pi/24*x)
then
D2 = -C2*sin(2*pi/24*C3)
D3 = C2*cos(2*pi/24*C3)
meaning that
C2 = sqrt(D2^2 + D3^2)
2*pi/24*C3 = atan2(-D2,D3)
so if you know C you know D and vice versa. Same for the other two sine functions as well, using D4,D5 and D6,D7.
If you set up a wind data matrix that is 24x31 (hours down, days across) x 8 for heights then you can fit the entire month in one go, and loop over heights. In the code below, C1 and D1 are the same.
D = zeros(7,31,8);
x = (0:23)'; % 1:24 instead?
% set up matrix of 7 linearly independent columns
M = [ones(size(x)) cos(2*pi/24*x) sin(2*pi/24*x)...
cos(2*pi/12*x) sin(2*pi/12*x)...
cos(2*pi/ 8*x) sin(2*pi/ 8*x)];
Wind = rand(24,31,8);
for k = 1:8
D(:,:,k) = M\Wind(:,:,k);
end
The result is a 7x31x8 matrix of coefficients.

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!