Curve fitting using custom equation

I got a costum equation in the form of
OPCrtr = @(r) 1.5186e-08.*integral(5.9122e-6.*A.*exp(-5e-7.*a.^2).*(1-exp(-alpha.*10.*a.^2))./(4.*alpha.*a.^2).*besselj(0,r.*a).*a, 0,Inf);
and the experimental data of
y = [5.63519982407452, 4.38965221332586, 3.08480267517907, 2.53580063087177, 1.84559777892743, 1.23011321773770, 0.662503695933817, 0.300962951613869];
r = [0.861841642228739, 1.50647116324536, 2.08103225806452, 2.65559335288368, 3.25818181818182, 3.90281133919844, 4.61750928641251, 5.61248093841642];
with the boundary conditions of min(r) = 0, max(r) = 14e-6.*512, and the initial conditions of A = 0.129, alpha = 5.17e-7.
When I was trying to fit using the Curve ftting toolbox, it returns "The expression is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:".
How can I fit the data using the above custom equation please?

7 comentarios

Torsten
Torsten el 27 de Abr. de 2023
So you want to fit A and alpha and your vector y should be modelled as y(r) = OPCrtr(r) ? Why is min(r) = 0, max(r) = 14e-6.*512 while your r-vector doesn't satisfy these constraints ?
Jingtao
Jingtao el 27 de Abr. de 2023
- So you want to fit A and alpha and your vector y should be modelled as y(r) = OPCrtr(r) ?
- YES
- Why is min(r) = 0, max(r) = 14e-6.*512 while your r-vector doesn't satisfy these constraints ?
- r in the theoretical equation is within the range of [0, 14e-6.*512], but I only got discrete data points in the experiment.
Torsten
Torsten el 27 de Abr. de 2023
But the range for r of the theoretical model equation and of your data points should be identical. Otherwise, the model equation could not be used for your data.
Jingtao
Jingtao el 27 de Abr. de 2023
Think about a function like y(x) = A.*exp(-B.*x)+C and x=linspace(0,10,101). If I extract 9 discrete values of y at x = 0.1:0.1:0.9 as the experimental data, is it impossible to fit the experimental data using the equation y(x) = A.*exp(-B.*x)+C in the range of x=linspace(0,10,101)?
Torsten
Torsten el 28 de Abr. de 2023
You have experimental data for a range of r between 0.8 and 5.6. So you can fit coefficients A and alpha for the model function for a range of r between 0.8 and 5.6. It doesn't make sense trying to evaluate the model function in the range 0 <= r <= 14e-6 * 512 because you didn't have experimental data for this range.
Jingtao
Jingtao el 28 de Abr. de 2023
Make sense. So how can I do it?
Torsten
Torsten el 28 de Abr. de 2023
Make sense. So how can I do it?
By getting experimental data for the range of interest (0 <= r <= 14e-6.*512).

Iniciar sesión para comentar.

 Respuesta aceptada

albara
albara el 27 de Abr. de 2023
t seems that there might be some issues with your custom equation. Let's first rewrite the custom equation using a proper anonymous function definition and make sure it is properly defined. Then, we can use the lsqcurvefit function in MATLAB to fit the data. Here's a step-by-step guide:
1- Save this function as customEquation.m in your MATLAB working directory.
% customEquation.m
function OPCrtr = customEquation(params, r)
A = params(1);
alpha = params(2);
integrand = @(a, r) 1.5186e-08 .* 5.9122e-6 .* A .* exp(-5e-7 .* a .^ 2) .* (1 - exp(-alpha .* 10 .* a .^ 2)) ./ (4 .* alpha .* a .^ 2) .* besselj(0, r .* a) .* a;
OPCrtr = integral(@(a) integrand(a, r), 0, Inf, 'ArrayValued', true);
end
2-Save this script as main_script.m in your MATLAB working directory. Run the main_script.m script in MATLAB to perform curve fitting and plot the data with the fitted curve.
% main_script.m
% Define the data and initial conditions
y = [5.63519982407452, 4.38965221332586, 3.08480267517907, 2.53580063087177, 1.84559777892743, 1.23011321773770, 0.662503695933817, 0.300962951613869];
r = [0.861841642228739, 1.50647116324536, 2.08103225806452, 2.65559335288368, 3.25818181818182, 3.90281133919844, 4.61750928641251, 5.61248093841642];
% Set the initial conditions and bounds
initial_conditions = [0.129, 5.17e-7];
lb = [0, 0];
ub = [14e-6 * 512, Inf];
% Perform the curve fitting
fitted_params = lsqcurvefit(@customEquation, initial_conditions, r, y, lb, ub);
% Plot the data and fitted curve
r_fit = linspace(min(r), max(r), 100);
y_fit = customEquation(fitted_params, r_fit);
figure;
plot(r, y, 'ro', 'MarkerSize', 8, 'LineWidth', 2);
hold on;
plot(r_fit, y_fit, 'b-', 'LineWidth', 2);
xlabel('r');
ylabel('y');
legend('Data', 'Fit');
This should give you the fitted parameters and the plot of the custom equation fit to your data.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes

2 comentarios

Jingtao
Jingtao el 27 de Abr. de 2023
Hi Albara, Thanks for your answer. But below is what I got. :-(
Jingtao
Jingtao el 28 de Abr. de 2023
Problem solved. Nice! Thanks a lot!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 27 de Abr. de 2023

Comentada:

el 28 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by