Asked by alesmaz
on 1 Nov 2019

Hi, I would like to know how to save at each iteration of a parametric ODE the vector of my parameters 'p'.

function f=bernardode(p,t)

t=temposp;

options=odeset('AbsTol',1e-6,'RelTol',1e-6);

[T,Z]=ode45(@bernard2,t,z0,options);

function dz = bernard2(t,z)

dzdt=zeros(4,1);

dzdt(1)=-(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*z(1);

dzdt(2)=(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*z(2);

dzdt(3)=p(2)*z(4)+(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))*(1-p(1)-z(3));

dzdt(4)=(p(1)-z(4))*(p(3)*(1-qmin/z(1)))*(((Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2))))/(p(4)+(Io/(sigma*L*z(2)))*(1-exp(-sigma*L*z(2)))))-p(2)*z(4);

dz=dzdt;

end

f=Z;

end

In particular the optimal parameters are found by simulated annealing algorithm.

I tried to add a string parameter=p before the second 'end' but the code overwrite p at each cycle.

I tried also to use 'save' command but it doesn't work.

Thank you in advance.

Answer by Samatha Aleti
on 5 Nov 2019

As per my understanding, you want to save the vector "p" of each iteration. To do this, you can initialize a matrix, let “store” of size (number of iterations) x (length of vector p) as follows:

% numIterations : number of iteration

% len : length of vector p

store = zeros(numIterations, len); % Initialization

idx = 0; % Row index

Then save each ‘p’ in a separate row of “store” using “idx” as follows:

% Before end in your code

idx = idx+1;

store(idx,:) = p;

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## darova (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/488688-save-parameters-at-each-ode-iteration#comment_762518

## alesmaz (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/488688-save-parameters-at-each-ode-iteration#comment_762538

Sign in to comment.