Solving equations, Numerical Integration, MSE, best fit overall parameter 'c'

1 回表示 (過去 30 日間)
john birt
john birt 2012 年 4 月 26 日
Note: My actual functions are huge, the question uses a simple y=c*x just for illustrative purpose.
With equation y=x*c where 'x' and 'y' are vectors find 'c' a single numeric value that minimizes the sum of squared residues (y-x*c)^2.
This I can do
x = [1.4 1.5 1.6]; y = [0.2 0.31 0.43];
c = 0.1; %initial guess
f = @(c,x) x*c;
cfit = nlinfit(x,y,f,c)
which gives c=0.2217, exactly what I am looking for, all is good. But I really want to have the variable 'y' equal to an integral, like
y = int_0^1 x*c*t^2 dt.
So coded
x = [1.4 1.5 1.6]; y = [0.2 0.31 0.43];
c = 0.1; %initial guess
t=0.1:0.1:1;
f = @(c,x) trapz(t,x*c*t.^2);
cfit = nlinfit(x,y,f,c)
But this does not work.
I do not understand how to use 'trapz' (numerical integration) in this estimation setting.
.......................................
p.s. I get the error message
??? Error using ==> nlinfit at 120
Error evaluating model function '@(c,x)trapz(t,x*c*t.^2)'.
Error in ==> test88 at 7
cfit = nlinfit(x,y,f,c)
Caused by:
Error using ==> mtimes
Inner matrix dimensions must agree.

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 4 月 26 日
x = [1.4 1.5 1.6]; y = [0.2 0.31 0.43];
c = 0.1; %initial guess
t=0.1:0.1:1;
f = @(c,x) arrayfun(@(x)trapz(t,x.*c.*t.^2),x);
cfit = nlinfit(x,y,f,c)
  2 件のコメント
john birt
john birt 2012 年 4 月 26 日
thanks will look at this
john birt
john birt 2012 年 4 月 26 日
Thank you, it works like a dream!, now to apply this to my huge function and see what happens.
Big thank you. :-)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by