Monday 15 September 2014

curve fitting - Find values of constants in equation using MATLAB -



curve fitting - Find values of constants in equation using MATLAB -

i have equation f(f)=a*f^3+b*f+c. have known vectors of data, p, independent variable, 'f'. need find values of a, b, c. tried:

function [ val ] = myfunc(par_fit,f,p) % gives me a,b,c % p= af^3 +bf +c val = norm(p - (par_fit(1)*(f.^3))+ (par_fit(2)*f) + (par_fit(3))); end my_par = fminsearch(@(par_fit) myfunc(par_fit,f,p),rand(1,3));

this gives me my_par = [1.9808 -2.2170 -24.8039], or a=1.9808, b=-2.2170, , c=-24.8039, require b should larger 5, , c should larger zero.

i think problem might because objective function incorrect:

val = norm(p - (par_fit(1)*(f.^3))+ (par_fit(2)*f) + (par_fit(3)));

should be:

val = norm(p-(par_fit(1)*f.^3+par_fit(2)*f+par_fit(3)));

but can constrain values of variables when minimisation using fmincon rather fminsearch. setting lb input [-inf -inf 0], first 2 coefficients allowed real number, 3rd coefficient must greater or equal zero. example: (i've shown how solve problem (without non-negativity constraint) using matrix method)

% sample info f=(0:.1:1).'; p=2*f.^3+3*f+1+randn(size(f)) % create van der monde matrix m=[f.^3 f f.^0]; c=m\p; % solve matrix problem in to the lowest degree squares sense if size(f)>size(f) my_par=fmincon(@(c) norm(p-(c(1)*f.^3+c(2)*f+c(3))),rand(1,3),[],[],[],[],[-inf 5 0],[]) c.' plot(f,p,'o',f,m*c,f,my_par(1)*f.^3+my_par(2)*f+my_par(3))

matlab curve-fitting linear-regression

No comments:

Post a Comment