Defining non-coefficient constant in fittype

1 回表示 (過去 30 日間)
Ida Ekmark
Ida Ekmark 2019 年 10 月 1 日
コメント済み: Ida Ekmark 2019 年 10 月 2 日
Hello,
I'm trying to fit a curve of a specific type to an array of data-points, but I can't get it to work. The curve is defined by
where K1, K2, K3, K4, v0 and are constants. I know K1, K2, K3, K4 and v0 are defined as coefficients, but I think my problem is that and cannot be defined as such. The fittype is as follows;
myfittype = fittype('K1*cos(omega1*t) + K2*sin(omega1*t) + K3*cos(omega2*t) + K4*sin(omega2*t) + v0','dependent',{'v'},'independent',{'t'},'coefficients',{'K1','K2','K3','K4','v0'},'constants',{'omega1','omega2'})
but I cannot figure out what it should be instead of 'constants' in front of {'omega1','omega2'}.
Can anyone help me with this problem?
Thanks in advance

採用された回答

John D'Errico
John D'Errico 2019 年 10 月 1 日
編集済み: John D'Errico 2019 年 10 月 1 日
While this is trivial to solve using the CFTB (that is, fit) it is even simpler to solve using backslash.
Assume that omega1 and omega2 are known constants, and t is a vector of length greater than 5. (Well, 5 will give a result, but it will be a purely interpolating one, any noise will be amplified in the coefficients.) I'd suggest a good idea is to have at least twice as many data points as unknowns, but the magnitude of the noise and quality of your data will be a factor there.
n = numel(t);
K1234v0 = [cos(omega1*t(:)) , sin(omega1*t(:)) , cos(omega2*t(:)) , sin(omega2*t(:)) , ones(n,1)]\Y(:);
That is all it takes to solve the problem. You can use tools like regress in the stats toolbox to get more information about the parameter estimates. K1234v0 will be a vector of length 5, containing the parameter estimates for a least squares fit to the data.
If you insist on using fit, then do it like this, using a function handle for the function:
% I've made up some random data,
% then chosen an arbitrary value for omega1
t = rand(10,1);
y = rand(10,1);
omega1 = 3;
G = fittype(@(a,v0,t) a*sin(omega1*t) + v0,'independent','t','coefficients',{'a','v0'})
G =
General model:
G(a,v0,t) = a*sin(omega1*t)+v0
fit(t,y,G)
Warning: Start point not provided, choosing random start point.
> In curvefit.attention.Warning/throw (line 30)
In fit>iFit (line 307)
In fit (line 116)
ans =
General model:
ans(t) = a*sin(omega1*t)+v0
Coefficients (with 95% confidence bounds):
a = 0.2102 (-0.6246, 1.045)
v0 = 0.2409 (-0.2874, 0.7691)
Did it work? Yes. Compare the results to that from backslash.
n = numel(t);
K1v0 = [sin(omega1*t(:)), ones(n,1)]\y(:)
K1v0 =
0.210233778410521
0.240893437973739
As you can see in my example, I used a simpler model, but yours is essentially the same, it just has more terms.
The backslash solution will be far more efficient too.
  3 件のコメント
John D'Errico
John D'Errico 2019 年 10 月 2 日
What is an unknown constant? Sorry, but that is meaningless. K1, K2, K3, K4, v0, are all constants too. They are unknown to you.
You implied that omega1 and omega2 were known constants. Now you say they are unknowns. Make up your mind.
That makes them coefficients, unknown parameters, just like the others. As such the answer is trivial. You already know how to use fittype. Your list of coefficients is now simply:
{'K1','K2','K3','K4','v0','omega1','omega2'}
Are you confused that omega1 and omega2 do not enter the problem linearly like the others? They are unknown parameters, still COEFFICIENTS as the CFTB defines them.
Ida Ekmark
Ida Ekmark 2019 年 10 月 2 日
I'm sorry for the confusion. I tried with and as coefficients, and the curve did not at all fit the datapoints provided. I then realised that constants inside sine-functions might not be defined as coefficients, and tried to find a definition of coefficient and other examples of how fittype was used. I didn't find anything in support of constants inside sine-function being defined as coefficients, so I assumed they had to be defined as something else and therefore I asked this question. If I understand you correctly, they are defined as coefficients and something else is the problem?
Thank you for your answers

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by