Fit 2 parameters with lsqcurvefit including an integral term
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Alfredo Scigliani
il 12 Apr 2022
Modificato: Torsten
il 13 Apr 2022
Hi, I am trying to fit this function to my data. The function has 2 unknown parameter D1 and D2, everything else is known. I want to use lsqcurvefit.
I believe that my error lies in the syntax of the function handles when I want to include D_1 and D_2. If anyone can assist me trying to figure out why it isn't working properly I would really appreciate it.
I will include my code and data below.
clear; clc; clf; close all;
xdata = [10.30, 29.88, 59.64, 99.58, 149.66, 209.96, 280.44, 361.03, 451.87, 552.89, 664.10, 785.38, 916.94, 1058.68, 1210.48, 1372.58, 1544.86, 1727.33, 1919.81, 2122.64, 2335.65, 2558.64, 2792.01, 3035.55, 3289.29, 3552.97, 3827.05, 4111.33, 4405.52, 4710.15, 5024.96, 5349.96];
ydata = [1, 0.9825, 0.9389, 0.9003, 0.8492, 0.8011, 0.738, 0.6873, 0.639, 0.5807, 0.533, 0.4901, 0.4471, 0.4202, 0.3894, 0.3668, 0.3531, 0.3278, 0.3199, 0.29, 0.2965, 0.2875, 0.2764, 0.276, 0.2655, 0.2524, 0.2495, 0.2474, 0.2404, 0.2394, 0.237, 0.2242];
D0 = [0.01 0.001]; %initial guess
fun = @(x,B,D) exp(B.*(D(1)-D(2))*x.^2);
fun_2 = @(D_1, D_2, xdata) exp(-xdata.*D_2).*integral(@(x) fun(x,xdata),0,1,'ArrayValued',true);
D = lsqcurvefit(fun_2, D0, xdata, ydata);
D_1 = D(1);
D_2 = D(2);
semilogy(xdata, ydata,'ko', xdata,fun_2(D_1, D_2,xdata),'r-')
1 Commento
Risposta accettata
Star Strider
il 13 Apr 2022
Using your posted revised code (lightly edited) —
xdata = [10.30, 29.88, 59.64, 99.58, 149.66, 209.96, 280.44, 361.03, 451.87, 552.89, 664.10, 785.38, 916.94, 1058.68, 1210.48, 1372.58, 1544.86, 1727.33, 1919.81, 2122.64, 2335.65, 2558.64, 2792.01, 3035.55, 3289.29, 3552.97, 3827.05, 4111.33, 4405.52, 4710.15, 5024.96, 5349.96];
ydata = [1, 0.9825, 0.9389, 0.9003, 0.8492, 0.8011, 0.738, 0.6873, 0.639, 0.5807, 0.533, 0.4901, 0.4471, 0.4202, 0.3894, 0.3668, 0.3531, 0.3278, 0.3199, 0.29, 0.2965, 0.2875, 0.2764, 0.276, 0.2655, 0.2524, 0.2495, 0.2474, 0.2404, 0.2394, 0.237, 0.2242];
D0 = [0.01 0.001]; %initial guess
fun = @(x,D,xdata) exp(-xdata*(D(1)-D(2))*x.^2);
fun_2 = @(D, xdata) exp(-xdata.*D(2)).*integral(@(x) fun(x,D,xdata),0,1,'ArrayValued',true);
D = lsqcurvefit(fun_2, D0, xdata, ydata);
D_1 = D(1)
D_2 = D(2)
semilogy(xdata, ydata,'ko', xdata,fun_2(D,xdata),'r-')
grid
.
2 Commenti
Più risposte (1)
Torsten
il 12 Apr 2022
Modificato: Torsten
il 13 Apr 2022
xdata = [10.30, 29.88, 59.64, 99.58, 149.66, 209.96, 280.44, 361.03, 451.87, 552.89, 664.10, 785.38, 916.94, 1058.68, 1210.48, 1372.58, 1544.86, 1727.33, 1919.81, 2122.64, 2335.65, 2558.64, 2792.01, 3035.55, 3289.29, 3552.97, 3827.05, 4111.33, 4405.52, 4710.15, 5024.96, 5349.96];
ydata = [1, 0.9825, 0.9389, 0.9003, 0.8492, 0.8011, 0.738, 0.6873, 0.639, 0.5807, 0.533, 0.4901, 0.4471, 0.4202, 0.3894, 0.3668, 0.3531, 0.3278, 0.3199, 0.29, 0.2965, 0.2875, 0.2764, 0.276, 0.2655, 0.2524, 0.2495, 0.2474, 0.2404, 0.2394, 0.237, 0.2242];
D0 = [0.01 0.01]; %initial guess
fun = @(x,D,xdata) exp(-xdata.*(D(2)+(D(1)-D(2)).*x.^2));
fun_2 = @(D,xdata) integral(@(x) fun(x,D,xdata),0,1,'ArrayValued',true);
D = lsqcurvefit(fun_2, D0, xdata, ydata)
D_1 = D(1);
D_2 = D(2);
semilogy(xdata, ydata,'ko', xdata,fun_2(D,xdata),'r-')
2 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!