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

1 view (last 30 days)
john birt on 26 Apr 2012
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 on 26 Apr 2012
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)
john birt on 26 Apr 2012
Thank you, it works like a dream!, now to apply this to my huge function and see what happens.
Big thank you. :-)