Curve fitting using custom equation
19 次查看(过去 30 天)
显示 更早的评论
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1368319/image.jpeg)
7 个评论
Torsten
2023-4-28
Make sense. So how can I do it?
By getting experimental data for the range of interest (0 <= r <= 14e-6.*512).
采纳的回答
albara
2023-4-27
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!