필터 지우기
필터 지우기

Nonlinear least squares fit of custom function to data

조회 수: 2 (최근 30일)
Fotaras Sitof
Fotaras Sitof 2015년 11월 24일
Dear all,
I'm working on describing a physics model using the following function: [y(i)] = f ([x(i)], c1, c2, c3, c4, c5, c6, P) where P is a struct that contains even more parameters.The function is written in its own .m file and contains quite a body of calculations. What I want to do is a nonlinear least squares fit on a given dataset x-y using the function f and determine c1, c2, c3, c4, c5, c6. The struct P contains known parameters. I can't seem to get my head around passing f to fittype or lsqcurvefit for that matter.
My 1st approach:
fitting = @(c1, c2, c3, c4, c5, c6,x) f(x,c1, c2, c3, c4, c5, c6,P); ft = fittype(fitting, 'independent', {'x'}, 'dependent', {'y'}); opts = fitoptions( ft ); opts.Display = 'Off'; opts.Lower = [-Inf -Inf -Inf -Inf -Inf -Inf]; % lower bound of coefficients to be fitted opts.StartPoint = [0 1 2 2 (10^-4) 1]; % starting points of coefficients opts.Upper = [Inf Inf Inf Inf Inf Inf]; % upper bound of coefficients to be fitted % Fit model to data. [fitresult, gof] = fit( [x, y], ft, opts );
This code fails on the fittype call producing the error: 'Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data.This custom equation fails to meet that requirement' I don't see why is this the case since f was defined to produce a vector y for a given vector x and some parameters.
My 2nd approach:
Define f as: f'([x(i)], p, P) replacing c1, c2, c3, c4, c5, c6 in the function's body as elements of vector p: p(1), p(2), p(3), p(4),p(5), p(6). Then called:
[p,error] = lsqcurvefit(@f',[0 1 2 2 1 1],x) but the execution of f' fails with error 'not enough input parameters'. It seems the struct P is not passed in f'
This post got long as I tried to be slightly thorough, but hopefully someone can give a pointer or two to help me get unstuck.
Thanks in advance!

답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by