2

I am able to run the code for any equations but when I introduce an integral the command won't run:

    t=dataset_TK1(:,1);
dataset_TK4=xlsread('Akis','Sheet1','AG491:AR725');
y_4=dataset_TK4(:,12);

Kg=1.76717865712934;
N0=1.08E+05;

fun1=@(Z) Z^(-1+(X(1)-Kg)/X(3))*exp(Z);

Ntotal=@(X,t)integral(fun1,X(2)*exp(-X(3)*t),X(2));

X0=[10,10,10];
Fsumsquares=@(X)sum((Ntotal(X,t)-y_4).^2);
opts = optimoptions('fminunc','Algorithm','quasi-newton');
[xunc,ressquared,eflag,outputu] =   fminunc(Fsumsquares,X0,opts)

Any suggestions?

Thank you

  • I suppose the limits of the integral should be scalars, cannot be computed runtime with a function. – Giogre Oct 01 '20 at 22:41
  • 1
    `fun1` should be `fun1 = @(X,Z)` maybe ? Your code lacks the `X` there is just `Z` under the handle – Giogre Oct 01 '20 at 23:08
  • But I have to take every point in time. Is there a suggested approach? – Akis Kesisoglou Oct 01 '20 at 23:09
  • 1
    `X(1)` and `X(3)` in `fun1`, but `X` is not defined, at least not in your code snippet. – Giogre Oct 01 '20 at 23:11
  • It is defined in the way the Fsumsquares function operates. – Akis Kesisoglou Oct 01 '20 at 23:29
  • 1
    but also `t` is in the handle of `Ntotal`, but is also defined in the first line of the code snippet. `X` is not defined instead, but treated the same as `t`. `Ntotal` has its parameters defined when called in `Fsumsquares`, while `fun1` has no parameter defined when it is called by `Ntotal`. I think function parameters should all be treated the same. – Giogre Oct 01 '20 at 23:36
  • If you read the theory of the function you will see what I mean. – Akis Kesisoglou Oct 01 '20 at 23:49

1 Answers1

0

The MWE (minimum working example) code below works, in the sense it doesn't spurt out errors: to obtain it, I have substituted your excel data with dummy arrays.

I limited myself to remove unnecessary handles from functions in your snippet, and adjust operators to element-wise .^ and .* in fun1. Also, in the Ntotal integral, the limits should be scalars, not vectors. That is why I took only one element out of t:

% dummies, put your data back
t=ones(1,10);
y_4=[1,2,3,4,5,6,7,8,9,10];
X=[11,3,4,6,2,3,55,22,89,6];

Kg=1.76717865712934;
N0=1.08E+05;

fun1=@(Z) Z.^(-1+(X(1)-Kg)/X(3)).*exp(Z);               % changed
Ntotal=integral(fun1,X(2)*exp(-X(3)*t(1)),X(2));        % changed

X0=[10,10,10];
Fsumsquares=@(X) sum((Ntotal-y_4).^2);                  % changed
opts = optimoptions('fminunc','Algorithm','quasi-newton');
[xunc,ressquared,eflag,outputu] =   fminunc(Fsumsquares,X0,opts)
Giogre
  • 1,444
  • 7
  • 19