I am trying to do a curve fitting using Matlab fminsearch.

Here is what I wrote :

Idata = readtable('Data_batterie.xlsx','Sheet','C(T)','Range','A1:A72');
Idata = table2array(Idata);
Tdata = readtable('Data_batterie.xlsx','Sheet','C(T)','Range','B1:B72');
Tdata = table2array(Tdata);
Cdata = readtable('Data_batterie.xlsx','Sheet','C(T)','Range','E1:E72');
Cdata = table2array(Cdata);

I = Idata
T = Tdata
C = Cdata

Capa = @(x)x(1).*(T+x(2))./(T+x(3)).*(x(4)+x(5).*I)+(x(6)+x(7).*atan(x(8)+x(9).*I))-C;
%x(1) --> x(9) = a1 --> i1

x0 = [ 1.35803674e+00  6.26544343e+00  5.64908765e+00  4.22139110e+01 7.09357003e-01 -3.03109514e+01 -2.97030786e+00 -2.35995343e-01 1.13629716e-02];
bestx = fminsearch(Capa,x0)

Unfortunately, I encounter the error mentionned in the title when I try to run it but I don't know what is my error here.

It probably is a simple syntax error but I can't seem to find it

I'm trying to find the values of the x coefficients, but the problem is that I can't run the fminsearch.

I tried to swap some values of the functions but the error stays the same

1

There are 1 best solutions below

0
Wolfie On

It looks like the function Capa outputs an array the same size as T, I, and C, i.e. 72x1.

fminsearch is trying to minimise a scalar-valued function. Maybe you want to change Capa to something like Capa = @(x)norm( ____ ) or some other function to generate a scalar output which should be minimised instead of an array.

From the docs:

fun: fun is a function that accepts a vector or array x and returns a real scalar f