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

조회 수: 1(최근 30일)
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 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표시숨기기 이전 댓글 수: 1
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. :-)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!