Writing a function, when each argument is dependent on previous argument while the first argument is known

Hello,
I am new to MATLAB and not able to troubleshoot my attached code. I initially received the error “conversion to cell from function_handle is not possible” and I believe I managed to resolve that error. Below is part of the script:
% Main optimization and parameter fitting starts here:
% Boundary conditions for parameters C (constant(1)) and D (constant(2)):
lb = [0.000000000001,0.05]; ub = [1,50]; constant0 = [0.0000000031,15];
i_dt=zeros(n,length(S)); % S dimensions are (2,145)
i_dt(:,2:length(S))=dt.*hg_new;
L=zeros(n,length(S)); % Height of elements
L(:,1)=Linitial; % Height of elements for day zero
for jj=1:length(S)
if jj==1
continue
end
for j=1:n % n is 101 (previously defined...)
L = @(constant,i_dt) L(j,jj-1)-(i_dt.*constant(1).*((L(j,jj-1)./hs)-1)^constant(2)); %i_dt is (101,145)
end
height_pred = @(constant,i_dt) sum(L(jj)); %heigh_pred(1) is known and previously defined
end
options = optimoptions('lsqcurvefit','FinDiffType','central',...
'TolFun',1e-10);
constant = lsqcurvefit(height_pred,constant0,i_dt,height_meas,lb,ub,options);
Currently I receive error “Index exceeds matrix dimensions.” for below line:
L = @(constant,i_dt) L(j,jj-1)-(i_dt.*constant(1).*((L(j,jj-1)./hs)-1)^constant(2));
My guess is that I have not defined the function properly and thus MATLAB cannot proceed.
The main script is much longer and I only copied the problematic section, so please let me know if more of the main script is needed. As you can see, I am trying to fit two parameters into an equation using lsqcurvefit function. The functionheight_pred is sum of L function. In L function, each argument is relying on the value from the previous argument while the first argument is known.
I am stuck at getting this code work or at least knowing where the problem is coming from. Any help is much appreciated.

Antworten (1)

You start off with L being a matrix but then you turn it into an anonymous function. Which should it be?
If it should be a matrix, have the line where you currently make it an anonymous function instead compute what the next iteration's L matrix should be and assign to it.
L = 1;
while L < 100
L = 2*L; % Not L = @(something) 2*L;
end
If it should always be an anonymous function you probably want to rename your L matrix.

1 Kommentar

Thank you very much Steven Lord for your quick reply.
L is a 101 by 145 matrix that only its first column is known. The rest of columns are unknown and can be calculated using the formula:
L = @(constant,i_dt) L(j,jj-1)-(i_dt.*constant(1).*((L(j,jj-1)./hs)-1)^constant(2)); %i_dt is (101,145)
As you see, value of argument L(j,jj) depends on the value of argument L(j,jj-1).
However, to calculate L values (using above formula, I need to optimize two constants (constant(1) and constant(2)). That is why I wrote L in a function, to be able to use lsqcurvefit.
I am confused how to code this.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by