Eenoku
Eenoku

Reputation: 2977

MATLAB - least square curve fit for multiple independent variables

I have a following function:

I'd like to get the coefficients by least squares method with MATLAB function lsqcurvefit.

The problem is, I don't know, if it's even possible to use the function when my function t has multiple independent variables and not just one. So, according to the link I should have multiple xData vectors - something like this:

lsqcurvefit(f, [1 1 1], nprocs, ndoms, nDOF, measuredVals)

Do you know how to do it?


My attempt

I've tried to define my objective function like this

f = @(c, x) c(1)*x(2).^(c(2)*x(1).^c(3)) + (c(4) + c(5)*x(1))/x(3);

and to use lsqcurvefit like this

lsqcurvefit(f, [1 1 1], [ndoms nDOF nprocs], measuredVals),

but there's a problem, that measuredVals is a vector of size 56x1, but my "xData" is a matrix of size 56x3, so I'm getting this error:

Index exceeds matrix dimensions.

Error in factorizatonKGlobRegr>@(c,x)c(1)*x(2).^(c(2)*x(1).^c(3))+(c(4)+c(5)*x(1))/x(3)

Error in lsqcurvefit (line 202)
            initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});

Caused by:
    Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.

But how am I supposed to do this, when $t: \mathbb{R}^3 \rightarrow \mathbb{R}$?


Second attempt

I've slightly changed the objective function to

f = @(c, x) c(1)*x(:,2).^(c(2)*x(:,1).^c(3)) + (c(4) + c(5)*x(:,1))/x(:,3);,

but the error remains.


My data

measuredVals = [
0.1647815
0.06300775
0.05769325
0.04803725
0.04290825
0.0405065
0.03807525
0.03487725
0.284112
0.13495675
0.12740075
0.11109725
0.105036
0.11022575
0.100587
0.09803775
0.48695475
0.30563525
0.30084925
0.283312
0.2745085
0.271998
0.27472625
0.27103925
0.89953925
0.68234025
0.6783635
0.65540225
0.64421475
0.64214725
0.63949875
0.623119
1.588605
1.37335275
1.36082075
1.35097375
1.34813125
1.34932025
1.3519095
1.34521625
2.820884
2.63251325
2.640659
2.6338805
2.636361
2.62748
2.6233345
2.63821
4.81472975
4.65116425
4.664892
4.64225625
4.6734825
4.63981675
4.635483
4.6280245];

n = 56;

ndoms = [];
for i=1:n
    ndoms = [ndoms; 288];
end

tmp = [
        375
        1029
        2187
        3993
        6591
        10125
        14739];
nDOF = [];
for i=1:7
    for j=1:8
        nDOF = [
            nDOF
            tmp(i)];
    end
end

nprocs = [];
for i=1:7
    nprocs = [nprocs; [1 2 3 4 6 8 12 24]'];
end

Upvotes: 0

Views: 1193

Answers (1)

Greg Petersen
Greg Petersen

Reputation: 121

When I tested your objective function with a simulated c and x input, it returned a NxN matrix the size of your data set. This shouldn't be the case. You should just get a single value for each data set so I would expect a Nx1 matrix. Try updating your function to this

f = @(c, x) c(1).*x(:,2).^(c(2).*x(:,1).^c(3)) + (c(4) + c(5)*x(:,1))./x(:,3)

I added a period to the divisor.

Upvotes: 0

Related Questions