How to solve equation with ten unknowns with Fourier Analysis
1 view (last 30 days)
Show older comments
Hi Everyone,
I have to compute ten unknowns in one equation and I have no idea how to do. Is there someone can help me? I'm attacching the code and the files below. Thanks
format long g
folderData = 'D:\Valerio\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;
f = ERA5(:,1); %Hs from ERA5
dir_ERA5 = ERA5(:,3);
x1 = IPCC(:,1); %Hs from IPCC
x2 = IPCC(:,3); %Dir from IPCC
f = @(F,x)(F(1)+F(2)*cos(2*pi*(x2/360)-F(3))+F(4)*cos(4*pi*(x2/360)-F(5))*x1^((F(6)+F(7)*cos(2*pi*(x2/360)-F(8))+F(9)*cos(4*pi*(x2/360)-F(10)))));
I have to compute all values of F with Fourier Analysis. Thanks.
3 Comments
Answers (2)
Jon
on 17 Mar 2020
If you have the optimization toolbox you can use fsolve
2 Comments
Jon
on 17 Mar 2020
There are also some nonlinear equation solvers in the MATLAB File Exchange.
If you go to the MATLAB file exchange and search using the terms solve system of nonlinear equation
you will find some possibilities. I've never used any of these so I can not provide any recommendations regarding how well they work.
Here are a few to get you started:
Rik
on 18 Mar 2020
The code below will work in base Matlab (i.e. it doesn't require any toolbox). Note that the fminsearch function is relatively sensitive to the initial condition. It may fail to find the global optimum, or even to find a good local optimum. If you want better performance you should switch to a different algorithm and/or consider purchasing the curve fitting toolbox.
%syntax:
%x - x values
%y - vector with true output
%initial_guess - vector with your initial estimates
%f - function that converts (initial_guess,x) input to the same shape as y
%bounds - matrix with upper and lower limit of each parameter
%
%output:
%fit_val - same shape as initial_guess
% This is an adapted Ordinary Least Squares cost function
OLS=@(b,x,y,f,bounds) sum((f(b,x) - y).^2) + ... % OLS part
inf^(any(b<bounds(:,1))-1) + ...%converts true to inf
inf^(any(b>bounds(:,2))-1);
% If you don't have bounds for your fitted parameters, set them to inf
bounds=inf*ones(numel(initial_guess),2);
bounds(:,1)=-inf;
% Use fminsearch to minimise the OLS function
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
fit_val = fminsearch(OLS, initial_guess(:), opts,x,y,f,bounds);
0 Comments
See Also
Categories
Find more on Least Squares 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!