Borrar filtros
Borrar filtros

Fminsearch does not work after increasing maxiter

2 visualizaciones (últimos 30 días)
Tung
Tung el 31 de En. de 2024
Comentada: Star Strider el 1 de Feb. de 2024
Hi everyone
I am new to Matlab. I come from Python which is slow for the type of problems that I am working on so I learned Matlab recently.
I am running an fminsearch to find the optimal paramters to fit a model in physiology. When I set the Maxiter to 10, the code ran all right but the result was not good. Naturally, I tried to increase the Maxiter number. The code did not run anymore and I got the following warning
Warning: Failure at t=8.127018e-01. Unable to meet integration tolerances without reducing the step
size below the smallest value allowed (2.887297e-15) at time t.
I have attached below the code that I used. I can provide the data as well if it is neccesary.
Thank you very much.
Tung Nguyen
----
function BSF
clear all
close all
clc
org_data = load("apnea_heart1.mat")
all_data = org_data.data;
ecg = all_data(1:12340);
[peaks, locs] = findpeaks(ecg, 'MinPeakHeight', 1.3*10^-3);
no_data_points = locs(2)-locs(1)+1;
time_frequency = 10^-3;
end_time = no_data_points*time_frequency;
time_span = linspace(0, no_data_points*time_frequency, no_data_points);
pressure = all_data(24681+locs(1):24681+locs(2));
flow = all_data(12341+locs(1):12341+locs(2));
tforward = time_span;
initial_guess = [0.2 0.004 0];
r = initial_guess(1);
l = initial_guess(2);
ic = initial_guess(3);
function dydt = model(t,y, para_fit)
P = interp1(time_span, pressure, t);
R = para_fit(1);
L = para_fit(2);
dydt = [-R/L*y(1)+1/L * P];
end
function error_in_data = moder(k)
% computing the error in the data
% solves the ODE, output is written in t and y
IC = k(3);
[t,y] = ode23s(@(t,y)model(t,y,k), tforward, IC);
time_observed = 2000:500:6000;
error_in_data = sum((y(time_observed)-flow(time_observed)').^2);
end
k = [r l ic];
% k = [paras(1) paras(2)];
IC = k(3);
[t,y] = ode23s(@(t,y)model(t,y,k), tforward, IC);
figure(1)
subplot(1,2,1);
plot(tforward, flow, 'b');
hold on
plot(t, y, 'r'); % plot the solution with the initial data
xlabel('time');
ylabel('Blood flow');
legend("Measured", "Initial guess");
axis([0 end_time -40 200]);
options = optimset('Maxiter', 20);
[opt_paras, fval] = fminsearch(@moder, k, options);
disp(opt_paras);
IC = opt_paras(3);
% para_fit = [2.000792312628345, 0.04934547048449 0];
% k = [para_fit(1) para_fit(2)];
% IC = para_fit(3);
[t,y] = ode23s(@(t,y)model(t,y,opt_paras), tforward, IC);
figure(1)
subplot(1,2,2);
plot(tforward, flow, 'b');
hold on
plot(t, y, 'r'); % plot the solution with the initial data
xlabel('time');
ylabel('Blood flow');
axis([0 end_time -40 200]);
legend("Measured", "Optimized guess");
end
  2 comentarios
Star Strider
Star Strider el 31 de En. de 2024
Warning: Failure at t=8.127018e-01. Unable to meet integration tolerances without reducing the step
size below the smallest value allowed (2.887297e-15) at time t.
This is not a problem with fminsearch. Shortly after the integration begins (at about 0.8 time units) at some point in your code, the ‘model’ function encounters a singularity (±Inf), and stops. The most likely explanation is that ‘L’ becomes zero (or close to it).
Tung
Tung el 1 de Feb. de 2024
Thank you very much for your help. That makes sense!

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 1 de Feb. de 2024
Following up on my Comment, see if this solves the problem —
function dydt = model(t,y, para_fit)
L = max(1,L);
P = interp1(time_span, pressure, t);
R = para_fit(1);
L = para_fit(2);
dydt = [-R/L*y(1)+1/L * P];
end
That essentially constrains ‘L’ to be no less than 1. If it can be less than 1 (and greater than 0), replace that value for 1 in the max call.
  4 comentarios
Tung
Tung el 1 de Feb. de 2024
Thank you! That makes perfect sense.
Star Strider
Star Strider el 1 de Feb. de 2024
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 31 de En. de 2024
Editada: Matt J el 31 de En. de 2024
The warning is not coming from fminsearch. It is coming from the ODE solver. As fminsearch iteratively explores different choices for k, it sometimes lands on one for which the ODE becomes numerically pathological. Only you know what this means for the problem at hand, and how it should be handled. Perhaps you should abort moder() with error_in_data=inf when this occurs, so that fminsearch will recognize such k as a bad parameter choice.
In any case, I don't recommend artificially truncating MaxIter. You should probably abort the ODE solver when it starts to choke.
  1 comentario
Tung
Tung el 1 de Feb. de 2024
Thank you for your answer. That makes sense to me. To deal with the error, I imagine that we should use syntax similar to try/except in Python?

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by